#!/bin/sh
# Login daemon for PCDM
# This starts the main PCDM UI appropriately
#------------------------------------
#echo "[PCDM] starting daemon..."
PCDMCONF="/usr/local/etc/pcdm.conf"
BASEPCDMAUTHFILE="/var/tmp/.PCDMAuth"
STOPFILE="/var/tmp/.PCDMstop"
if [ -e "$STOPFILE" ] ; then rm $STOPFILE; fi

#Quick check that this is not the "stop" command
if [ "${1}" = "stop" ]; then
  touch ${STOPFILE}
  exit
elif  [ -f ${STOPFILE} ]; then
  rm ${STOPFILE}
fi

XOPTS=""
grep -q "^ENABLE_TCP_LISTEN=TRUE" ${PCDMCONF}
if [ $? -ne 0 ]; then
  XOPTS="-nolisten tcp" 
else
  XOPTS="-listen tcp"
fi

#See if VNC support is enabled for logging in to this system
grep -q "^ALLOW_REMOTE_LOGIN=TRUE" ${PCDMCONF}
if [ $? -eq 0 ]; then
   # Setup default vnc server opts
   vncopts="-forever "
   grep -q "^REMOTE_SHARED_SCREEN=TRUE" ${PCDMCONF}
   if [ $? -eq 0 ] ; then 
     vncopts="-shared "
   else
     vncopts="-nevershared "
   fi
   if [ -e "/usr/local/etc/vncpass" ] ; then
     vnccmd="x11vnc -display :0 $vncopts -passwdfile /usr/local/etc/vncpass -loop"
   else
     echo "WARNING: VNC enabled but no password in /usr/local/etc/vncpass!"
     sleep 5
   fi

fi


# Remove a stale shutdown update trigger
if [ -e "/var/db/pc-updatemanager/.doingShutdownUpdate" ] ; then
  rm /var/db/pc-updatemanager/.doingShutdownUpdate
fi

# Clear out a failed marker
if [ -e "/var/tmp/.pcdm-x-failed" ] ; then
  rm /var/tmp/.pcdm-x-failed
fi

# Now start the X Server
#echo "  [PCDM] Starting Xorg init"
touch /var/tmp/.PCDMAutoLogin # Allow Auto-Login the first time PCDM starts
while [ ! -e /var/run/nologin -a ! -e ${STOPFILE} ]; do
  if [ ! -e /etc/machine-id ] || [ ! -e /var/db/dbus/machine-id ]; then
    #detect/prevent Qt from crashing if the DBUS id file is non existant
    dbus-uuidgen --ensure >/dev/null 2>/dev/null
  fi
  if [ -e /usr/local/bin/pc-checkxdisplay ]; then
     /usr/local/bin/pc-checkxdisplay
  fi
  # Randomize the authorization file location
  PCDMAUTHFILE=${BASEPCDMAUTHFILE}-`openssl rand -hex 10`
  # Make sure the authorization file does not exist
  if [ -f ${PCDMAUTHFILE} ]; then
    rm ${PCDMAUTHFILE}
  fi
#  echo "  [PCDM] Generated auth file: ${PCDMAUTHFILE}"
  export XAUTHORITY=${PCDMAUTHFILE} #Set the auth file location
  touch ${PCDMAUTHFILE} #Create the (empty) authorization file
  xauth -f ${PCDMAUTHFILE} add :0 MIT-MAGIC-COOKIE-1 `openssl rand -hex 64` #Add a default entry to the file (randomized)
  if [ -n "$vnccmd" ] ; then
    # Start VNC
    (sleep 10 ; $vnccmd >/var/log/vncserver 2>/var/log/vncserver) &
  fi

  # Remove our X success marker
  if [ -e "/var/tmp/.pcdm-x-started" ] ; then rm /var/tmp/.pcdm-x-started; fi
#  echo "  [PCDM] Starting PCDM-session"
  xinit /usr/local/bin/PCDM-session -once -- :0 -auth ${PCDMAUTHFILE} $XOPTS vt9 2>/dev/null
     if [ $? != 0 -a  ! -e "/var/tmp/.pcdm-x-started"] ; then
        # Looks like X failed to start...
        if [ ! -e "/var/tmp/.pcdm-x-failed" ] ; then
          echo " [PCDM] xinit failure 1 - set the failed flag and try again"
          # Failure 1: Lets move xorg.conf away, and try again
          touch /var/tmp/.pcdm-x-failed
          mv /etc/X11/xorg.conf /etc/X11/xorg.conf.prevFailed 2>/dev/null
        else
          echo "[PCDM] xinit failure 2 - move xorg.conf away."
          # Failure 2: Wow, we really failed here, lets try running X wizard
          touch ${STOPFILE} #debuging break out here
          #mv /etc/X11/xorg.con.prevFailedf /etc/X11/xorg.conf.prev 2>/dev/null
          touch /var/.runxsetup
          rm /var/tmp/.pcdm-x-failed
        fi
     fi

  #Now remove the authorization file
  rm ${PCDMAUTHFILE}

  # We are shutting down
  if [ -e "/var/db/pc-updatemanager/.doingShutdownUpdate" ] ; then
     rm /var/db/pc-updatemanager/.doingShutdownUpdate
     touch ${STOPFILE}
  fi
done

rm ${STOPFILE} 2>/dev/null

return 0
