#!/bin/bash
while [ 1 ] ; do
    test ! -d /var/log/cisco && mkdir /var/log/cisco
    chmod 777 /var/log/cisco


    THINPRO=`dpkg -l | grep thinpro-base | cut -d ' ' -f3`
    if [ $THINPRO ]; then
        touch /tmp/gnome-session-established
        export DISPLAY=":0.0"
        export USER=`who | grep "($DISPLAY" |head -n1 | cut -d ' ' -f1`
        export DISPLAY=:0
    fi
    while [ ! -e /tmp/gnome-session-established ] ; do
      sleep 0.5
    done

    if [ $THINPRO ]; then
        fsunlock
    fi
    /usr/bin/vdi-citrix-config.sh jvdi add
	
    #if not ThinPro image 
    if [ -z $THINPRO ]; then
		test ! -d /opt/Citrix/ICAClient/ && mkdir -p /opt/Citrix/ICAClient/
        ln -fs /usr/lib/ICAClient/VDCISCO.DLL /opt/Citrix/ICAClient/VDCISCO.DLL

        while [ -z $USER ] || [ -z $DISPLAY ] ; do
           ACTIVE_TTY=`cat /sys/class/tty/tty0/active`
	       LOCAL_ACC=`w -h | grep $ACTIVE_TTY`
           XSERVER_OPT=`ps aux | grep $ACTIVE_TTY | grep -v grep | grep /var/run/lightdm/root/`
           if [ $? -eq 0 ] && [ "$LOCAL_ACC" ]; then
               export DISPLAY=`echo $XSERVER_OPT | sed -e 's#.*/var/run/lightdm/root/##g' | cut -d ' ' -f1`
           else
               DISPLAY_NUM=`w -h $USER | awk '$3 ~ /:[0-9.]*/{print $3}'`
               if [ -n "$DISPLAY_NUM" ]; then
                   export DISPLAY=$DISPLAY_NUM
               else
                   export DISPLAY=`ps -u $(id -u $USER) -o pid= | xargs -I{} cat /proc/{}/environ 2>/dev/null | tr '\0' '\n' | grep -m1 '^DISPLAY=' | cut -d '=' -f 2`
               fi
           fi
	       export USER=`who | grep "$DISPLAY" |head -n1 | cut -d ' ' -f1`
        done
    fi
    /usr/bin/vxc-stop > /dev/null 2> /dev/null
#    sleep 10
    date >> /var/log/cisco/vxcUpStart.log
    echo "About to start vxc-start for USER=$USER DISPLAY=$DISPLAY" >> /var/log/cisco/vxcUpStart.log
    /usr/bin/vxc-start
    RESTART=`ps aux | grep pidrun | grep run_vxc | cut -d ' ' -f1`
    while [ $RESTART ] ; do
      ACTIVE_TTY=`cat /sys/class/tty/tty0/active`
      LOCAL_ACC=`w -h | grep $ACTIVE_TTY`
      XSERVER_OPT=`ps aux | grep $ACTIVE_TTY | grep -v grep | grep /var/run/lightdm/root/`
      if [ $? -eq 0 ] && [ "$LOCAL_ACC" ]; then
          ACTIVE_DISPLAY=`echo $XSERVER_OPT | sed -e 's#.*/var/run/lightdm/root/##g' | cut -d ' ' -f1`
      else
          ACTIVE_DISPLAY=`w -h $USER | awk '$3 ~ /:[0-9.]*/{print $3}'`
          if [ -z "$ACTIVE_DISPLAY" ]; then
              ACTIVE_DISPLAY=`ps -u $(id -u $USER) -o pid= | xargs -I{} cat /proc/{}/environ 2>/dev/null | tr '\0' '\n' | grep -m1 '^DISPLAY=' | cut -d '=' -f 2`
          fi
      fi
      GNOME_USER=`who | grep "$ACTIVE_DISPLAY" |head -n1 | cut -d ' ' -f1`
      ANY_GNOME_STARTED=`ps aux | grep gnome-session | head -n1 | cut -d ' ' -f1`
      if [ -z $THINPRO ] && [ -z $ANY_GNOME_STARTED ]; then
          rm /tmp/gnome-session-established
          /usr/bin/vxc-stop
          date >> /var/log/cisco/vxcUpStart.log
          echo "No Gnome session, restaring vxme" >> /var/log/cisco/vxcUpStart.log
      elif [ -z $USER ]; then
          /usr/bin/vxc-stop
          date >> /var/log/cisco/vxcUpStart.log
          echo "USER empty, restarting vxme" >> /var/log/cisco/vxcUpStart.log
      elif [ -z $THINPRO ] && [ `who | head -n1 | cut -d ' ' -f1` ] && [ "$GNOME_USER" != "$USER" ]; then
	  /usr/bin/vxc-stop 
          date >> /var/log/cisco/vxcUpStart.log
          echo "GNOME user changed, restarting vxme" >> /var/log/cisco/vxcUpStart.log
      elif [ -z $THINPRO ] && [ $ACTIVE_DISPLAY ] && [ $ACTIVE_DISPLAY != $DISPLAY ]; then
          /usr/bin/vxc-stop 
          date >> /var/log/cisco/vxcUpStart.log
          echo "TTY changed, restarting vxme" >> /var/log/cisco/vxcUpStart.log
      fi
      RESTART=`ps aux | grep pidrun | grep run_vxc | cut -d ' ' -f1`
      sleep 5
    done
    rm -f /dev/shm/VXME_*
    rm -f /dev/shm/F57FFF45*
    rm -f /tmp/vxme_watchdog_vdi_disconnected
    date >> /var/log/cisco/vxcUpStart.log
    echo "Starting new loop\n" >> /var/log/cisco/vxcUpStart.log
    unset RESTART
    unset USER
    unset DISPLAY
done
