#!/bin/bash

DEBUG=yes
OPEN=no

export PATH=$PATH:$(dirname $0)
. $(which casting-helpers) || exit 1
for conf in /etc/casting ~/etc/casting /etc/$PGM /usr/local/etc/$PGM ~/etc/$PGM
do
    [ -f $conf ] && . $conf
done
[ "$MODE" = "client" ] && PGM=casting-client

editor=$(which z || which code || which atom || which gedit || which subl)
if [ "$3" ]
then
  cd $base/$1/$2/$3 || end 1 could not cd to $base/$1/$2/$3
elif [ "$2" ]
then
  cd $base/$1/$2 || end 1 could not cd to $base/$1/$2
  ls -1d casting*
  yesno "Créer un nouveau casting?" || end "no"
  cd $(newcasting $1 $2 | tail -1) || end 1 Erreur en créant le casting
elif [ "$1" ]
then
  echo $1 | grep -q : && end 0
  cd "$1" || end 1 "error with folder $1"
fi

pwd | egrep -q "^$base/.*/casting[0-9]*" || end 1 "Ce n'est pas un dossier de casting"
if [ "$createfolders" != "none" ]
then
  subfolder=$(echo $createfolders | sed "s/.* //")
  pwd | egrep -q "^$base/.*/casting[0-9]*/$subfolder" || cd $subfolder 2>/dev/null || end 2 "Dossier $subfolder manquant"
fi

SCREENTITLE=$(pwd | sed "s:^$base/*::" | cut -d / -f -3 | tr "/ " ":_")

open=$(which xdg-open || which gnome-open)

if [ ! "$SSH_CLIENT" ]
then
  if which xrandr
  then
    xrandr --query | grep [[:blank:]]connected | sed -e "s/.*connected //" -e "s/.*primary //"> $TMP.screens
    allscreens=$(cat $TMP.screens | wc -l)
    scrpos=$(cat $TMP.screens | cut -d+ -f2| cut -d " " -f 1 | tail -1)
    scrwidth=$(cat $TMP.screens | cut -dx -f1 | tail -1)
    # scrheigth=$(cat $TMP.screens | cut -dx -f2 | cut -d+ -f 1 | tail -1)
    winwidth=$(($scrwidth / 3))
    # winheight=$scrheigth
    echo "$PGM" | grep server \
    && winposx=$(($scrpos + $scrwidth - $winwidth)) \
    || winposx=$(($scrpos + $scrwidth - 2 * $winwidth))
    # winposy=0
    wmctrl -r :ACTIVE: -e 5,$winposx,-1,$winwidth,-1
    wmctrl -r :ACTIVE: -b add,maximized_vert
    wmctrl -r :ACTIVE: -N "$USER@$HOSTNAME $PGM $SCREENTITLE"
  fi

  echo "$PGM"
  if echo "$PGM" | grep -q "server"
  then
    remotehost=$(echo $remote | cut -d: -f1)
    remotedir=$(pwd | sed "s:^$HOME/::")
    echo "connecting to $remotehost"
    ssh -t $remotehost "cd $remotedir && $(which $PGM)" $@ \
    || end $? "remote command failed"
    end remote finished
  else
    mkdir -p browsercache
    thumbstable > browsercache/index.html
    $open browsercache
    # $open browsercache/index.html https://wrap.rocks/$client/$job/$casting
    touch liste ignore thumbs deleted roles aliases
    if ! grep . liste; then
      (
      echo "## roles:"
      cat ../casting*/liste | grep -v "^(#|//)" | grep "[a-z].*//.*[a-zA-Z]" | sed "s:.*// *::" | tr "," "\n" | sed "s:^:# // :" | sort -u
      echo "## formats:"
      echo "# 1   1-John Doe // role1"
      echo "# 5   2-Jane Foe (20 ans) // role2"
      echo "# 7   1-John, 2-Jane // role1, role2"
      ) > liste
    fi

    if [ "$OPEN" = "yes" ]; then
      $editor ./ liste ignore thumbs deleted roles aliases >/dev/null 2>/dev/null & >/dev/null 2>/dev/null
    fi
  fi
fi

screen -x $SCREENTITLE && end

SCREENRC=$(realpath $(dirname $0))/$PGM.screenrc
[ -e "$SCREENRC" ] || end 3 Missing $SCREENRC
screen -S $SCREENTITLE -c $SCREENRC
end
