#!/bin/sh
#
#*******************************************************************************
#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#*******************************************************************************
#
# ident "uttsc.sh $Rev: 154 $ $Date: 2007-06-06 08:34:07 -0700 (Wed, 06 Jun 2007) $ SMI"
#

#
## WARNING: This file must not be altered in any way.
##          Please consult your Kiosk Session Service documentation for details
##          on building your own Kiosk Sessions.
#          

theModule=kiosk:uttsc

theTimeExec=$KIOSK_SESSION_DIR/uttsctime
theUTTSCRoot=`/etc/opt/SUNWut/basedir/lib/utprodinfo -r SUNWuttsc`
if [ $? -ne 0 ] ; then
 logger -i -p user.error -t $theModule \
	    "Error: SUNWuttsc is not correctly installed"
 exit 2
fi
theUTTSCExec="$theUTTSCRoot/SUNWuttsc/bin/uttsc"

#
## Sanity check
#
if [ ! -x $theUTTSCExec ] ; then
 logger -i -p user.error -t $theModule \
	    "Error: '$theUTTSCExec' doesn't exist or isn't executable"
 exit 2
fi

# 
## Helper to detect, if the Sun Ray Session is connected
#
theSUNWUTRoot=`/etc/opt/SUNWut/basedir/lib/utprodinfo -r SUNWuto`
theSUNWUTBin=${theSUNWUTRoot:-/opt}/SUNWut/bin
isConnected()
{
	theConnectinfo=`$theSUNWUTBin/utaction -i -e \
				  -d "echo disconnected" \
				  -c "echo connected" `
	test "$theConnectinfo" = "connected"
}

#
## uttsc arguments will be passed directly to uttsc. By default, we pass
## -m ( fullscreen ) and -m ( disable pulldown header )
##
## non uttsc arguments are handled by this script. Currently this script only
## handles the -t <timeout> (loop timeout) option. A default timeout of 1800
## seconds is used.
#
theTimeout=1800
theMinDelay=10
while getopts t: inOpt ; do
 case $inOpt in
  t) theTimeout=`expr $OPTARG + 0`
     if [ $? -ne 0 -a $? -ne 1 ] ; then
      logger -i -p user.error -t $theModule \
		"Error: invalid timeout '$OPTARG' specified"
      exit 2
     fi;;

  #
  ## if this is the first argument, we'll simply assume that all arguments
  ## are uttsc arguments and the '--' separator was omitted
  #
  \?) if [ $OPTIND -eq 2 ] ; then
       OPTIND=1
       break
      else
       logger -i -p user.error -t $theModule \
		  "Error: invalid option specified"
       exit 2
      fi;;
 esac
done
shift `expr $OPTIND - 1`
theUTTSCDefaultArgs="-m -b"

# Marker file for uttsc failures
theUTTSCFailure="$HOME/uttsc-failure"

#
## Now loop until timeout, restarting the login screen
#	  
theStartTime=`$theTimeExec`
if [ $? -ne 0 ] ; then
 logger -i -p user.error -t $theModule "Error: failed to query initial time"
 exit 2
fi

thePreviousTime=$theStartTime
while isConnected ; do
# Now run uttsc with the given arguments
# Forward uttsc output to syslog
# Use a marker file, to detect a non-zero exit code 
 rm -f $theUTTSCFailure
 { 	
  $theUTTSCExec $theUTTSCDefaultArgs "$@" 2>&1 ||
  	echo $? > $theUTTSCFailure 

 } | logger -i -p user.notice -t $theModule 

 if [ -f $theUTTSCFailure ] ; then
  # if uttsc fails, that is not a mere login timeout -> restart
  theResult=`cat $theUTTSCFailure`
  logger -i -p user.error -t $theModule \
             "$theUTTSCExec exited with error code $theResult - exiting"
  exit $theResult
 fi

 theEndTime=`$theTimeExec`
 if [ $? -ne 0 ] ; then
  logger -i -p user.error -t $theModule "Error: failed to query current time - exiting"
  exit 3
 fi
 theDifference=`expr $theEndTime - $theStartTime`
 if [ $theDifference -ge $theTimeout ] ; then
  logger -i -p user.debug -t $theModule \
	    "Timeout '$theTimeout' has elapsed - exiting."
  exit 0
 fi

 theDelay=`expr $theEndTime - $thePreviousTime`
 if [ $theDelay -lt $theMinDelay ] ; then
  logger -i -p user.notice -t $theModule \
             "$theUTTSCExec exited in less than $theMinDelay sec - exiting"
  exit 3	     
 fi

 thePreviousTime=$theEndTime
done

logger -i -p user.info -t $theModule \
            "Detected disconnected session - exiting"
exit 0
  
