#!/bin/bash


cd /opt/euro-beta

TITLE='Mozilla Firefox'

{

  FFOX_BIN=`which firefox`
  if [ -z "$FFOX_BIN" ]
  then
    UBUNTU=`lsb_release -a 2>/dev/null | grep -qi "Ubuntu" && echo "1"`
    if [ "$UBUNTU" ]
    then
      TMPRUN=`mktemp`
      echo "#!/bin/bash" > $TMPRUN
      echo "apt install firefox wmctrl -y" >> $TMPRUN
      chmod +x $TMPRUN

      ./show_process_output -T "Aggiornamento del repository" apt update
      ./show_process_output -T "Installazione del browser" $TMPRUN

      rm -f $TMPRUN
    fi
  fi

  FFOX_BIN=`which firefox`
  if [ -n "$FFOX_BIN" ]
  then
    CURR_DESKTOP=`wmctrl -d | grep '*' | cut -d' ' -f 1`
    CURR_DESKTOP=$(( $CURR_DESKTOP + 0 ))
    wmctrl -s 2
    PARM=""
    if [ -n "$1" ]
    then
      PARM="'"$1"'"
    fi
    su euro3g -c "unset XDG_RUNTIME_DIR; firefox -UILocale it-IT -contentLocale IT --new-instance $PARM" &

    FF=$!
    for ((i=0; i<20; i++))
    do
      sleep 0.1
      wmctrl -r "$TITLE" -t 2
      wmctrl -a "$TITLE"
    done
    wait $FF
    wmctrl -s $CURR_DESKTOP
  fi
} &
