#!/bin/sh
#
#*******************************************************************************
#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#*******************************************************************************
#

theModule=kiosk:finishsession

## Main
##
## The sequence of events is 
##
## 1. load the Kiosk common utils /opt/SUNWkio/lib/utils.sh
## 2. ensure the environment is sane
## 3. stop the Critical Applications Monitor
## 4. execute the primary session post-session script if configured
## 5. do a full cleanup for the user 
##
## Note: where possible, this script continues after detecting errors in an
##       attempt to do as much cleanup as possible
#

# set up preliminary priorities for error logging
log_err_prio=${KIOSK_LOG_FACILITY:-user}.${KIOSK_LOGPRI_ERROR:-error}
log_dbg_prio=${KIOSK_LOG_FACILITY:-user}.debug

logger -i -p $log_dbg_prio -t $theModule "tearing down Kiosk session"

#
## Load utilities and setup basic Kiosk Environment
#
theUtilsFile=/opt/SUNWkio/lib/utils.sh
if [ ! -r $theUtilsFile ] ; then
 logger -i -p $log_err_prio -t $theModule \
        "Error: can't read Kiosk utils file '$theUtilsFile'"
 exit 1
else
 . $theUtilsFile
 if [ $? -ne 0 ] ; then
  logger -i -p $log_err_prio -t $theModule \
         "Error: failed to load Kiosk utils file '$theUtilsFile'"
  exit 1
 fi
fi

#
## Ensure we have a sane environment
#
if [ -z "$DISPLAY" -o -z "$USER" -o -z "$HOME"  ] ; then
 logError -m $theModule -l "Error: \$DISPLAY, \$USER or \$HOME not specified"
 exit $KIOSK_RESULT_FAILED
fi

theRC=$KIOSK_RESULT_SUCCESS

#
## Check if there is an active session to stop
#
theSessionUserDynDir=$KIOSK_DYN_DIR/sessions/$USER
if [ -d "theSessionUserDynDir" ]
then
 #
 ## Stop critical applications monitor
 #
 stopCriticalAppsMonitor $theModule
 if [ $? -ne 0 ] ; then
  theRC=$KIOSK_RESULT_FAILED
  logError -m $theModule \
	   -l "Error: failed to stop Critical Applications Monitor"
 fi

 #
 ## Execute primary session post-session script, if specified
 #
 loadSessionConfiguration $theModule 
 if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
  theRC=$KIOSK_RESULT_FAILED
  logError -m $theModule -l "Error: failed to load Session Descriptor"
 else
  if [ ! -z "$KIOSK_SESSION_POST" ] ; then
   if [ ! -x "$KIOSK_SESSION_POST" ] ; then
    theRC=$KIOSK_RESULT_FAILED
    logError -m $theModule \
	     -l "Error: session post-session script '$KIOSK_SESSION_POST' is not executable"
   else
    "$KIOSK_SESSION_POST"
    if [ $? -ne 0 ] ; then
     theRC=$KIOSK_RESULT_FAILED
     logError -m $theModule \
	      -l "Error: session post-session script '$KIOSK_SESSION_POST' failed"
    fi
   fi
  fi
 fi
else
 logDebug -m $theModule \
	  -l "No session to tear down for user '$USER'"
fi	  

#
## Do a full cleanup for the user
#
cleanup $theModule $USER "$HOME"
if [ $? -ne 0 ] ; then
 theRC=$KIOSK_RESULT_FAILED
 logError -m $theModule \
          -l "Error: failed to clean up after Kiosk session for user '$USER'"
fi

#
## Remove the dynamic data directory
removeDynDir $theModule $USER
if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
 theRC=$KIOSK_RESULT_FAILED
 logError -m $theModule \
       -l "Error: failed to remove the dynamic data directory for user '$USER'"
fi

if [ $theRC -eq $KIOSK_RESULT_SUCCESS ] ; then
 logDebug -m $theModule -l "Success: Kiosk session teardown complete"
else
 logDebug -m $theModule -l "Error: Kiosk session teardown failed"
fi

exit $theRC
