#!/bin/sh
#
#*******************************************************************************
#
# Copyright 2006 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
#

logger -i -p user.debug -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 user.error -t $theModule \
        "Error: can't read Kiosk utils file '$theUtilsFile'"
 exit 1
else
 . $theUtilsFile
 if [ $? -ne 0 ] ; then
  logger -i -p user.error -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
 logDebug -m $theModule -l "Error: \$DISPLAY, \$USER or \$HOME not specified"
 exit $KIOSK_RESULT_FAILED
fi

theRC=$KIOSK_RESULT_SUCCESS

#
## Stop critical applications monitor
#
stopCriticalAppsMonitor $theModule
if [ $? -ne 0 ] ; then
 theRC=$KIOSK_RESULT_FAILED
 logDebug -m $theModule \
          -l "Error: failed to stop Critical Applications Monitor"
fi

#
## Execute primary session post-session script, if specified
#
loadSessionConfiguration $theModule $DISPLAY
if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
 theRC=$KIOSK_RESULT_FAILED
 logDebug -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
   logDebug -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
    logDebug -m $theModule \
             -l "Error: session post-session script '$KIOSK_SESSION_POST' failed"
   fi
  fi
 fi
fi

#
## Do a full cleanup for the user
#
cleanup $theModule $USER "$HOME"
if [ $? -ne 0 ] ; then
 theRC=$KIOSK_RESULT_FAILED
 logDebug -m $theModule \
          -l "Error: failed to clean up after Kiosk session 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

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

exit $theRC
