#!/bin/sh
#
# ident "$Id$ SMI"
#
#*******************************************************************************
#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#*******************************************************************************
#

theModule=kiosk:initsession

#
## Some functions used to ensure that the Kiosk Session Starter,
## /opt/SUNWkio/lib/Xsession is invoked by the current login manager.
##
## login manager specific mechanisms are used to make this happen correctly.
## pam_kiosk makes the pam service name available as an environment variable,
## KIOSK_PAM_SERVICE. We use this name to identify which login manager
## specific mechanism should be used to setup the Kiosk Seession Starter.
##
setSessionStarter()
{
 case "$KIOSK_PAM_SERVICE" in
  dtlogin*) setDTLOGINSessionStarter;;
  gdm*)     setGDMSessionStarter;;
  *) logDebug -m $theModule -l "Error: Unknown pam service '$KIOSK_PAM_SERVICE'"
     return $KIOSK_RESULT_FAILED;;
 esac
}
setGDMSessionStarter()
{
 _theDMRCFile=$HOME/.dmrc
 _theXsessionFile=$HOME/.xsession
 #
 ## First create the .dmrc file
 #
 rm -f $_theDMRCFile && cat << ! > $_theDMRCFile
  [Desktop]
  Session=custom
!
 if [ $? -ne 0 ] ; then
  logDebug -m $theModule -l "Error: failed to create file '$_theDMRCFile'"
  return $KIOSK_RESULT_FAILED
 fi
 chmod 600 $_theDMRCFile
 if [ $? -ne 0 ] ; then
  logDebug -m $theModule -l "Error: failed to chmod file '$_theDMRCFile'"
  return $KIOSK_RESULT_FAILED
 fi

 #
 ## Next create the .xsession file
 #
 rm -f $_theXsessionFile &&echo $KIOSK_PREFIX/lib/Xsession > $_theXsessionFile
 if [ $? -ne 0 ] ; then
  logDebug -m $theModule -l "Error: failed to create file '$_theXsessionFile'"
  return $KIOSK_RESULT_FAILED
 fi
 chmod 744 $_theXsessionFile
 if [ $? -ne 0 ] ; then
  logDebug -m $theModule -l "Error: failed to chmod file '$_theXsessionFile'"
  return $KIOSK_RESULT_FAILED
 fi

 chown $USER:$GROUP $_theDMRCFile $_theXsessionFile
 if [ $? -ne 0 ] ; then
  logDebug -m $theModule \
           -l "Error: failed to chown $_theDMRCFile or $_theXsessionFile"
  return $KIOSK_RESULT_FAILED
 fi

 return $KIOSK_RESULT_SUCCESS
}
setDTLOGINSessionStarter()
{
 _theDtDir=$HOME/.dt
 _theSessionDir=$_theDtDir/sessions
 _theLastSessionFile=$_theSessionDir/lastsession

 #
 ## First create the dtlogin sessions directory
 #
 mkdir -p $_theSessionDir
 if [ $? -ne 0 ] ; then
  logDebug -m $theModule \
           -l "Error: failed to create directory '$HOME/.dt/sessions'"
  return $KIOSK_RESULT_FAILED
 fi
 chmod 755 $_theDtDir $_theSessionDir
 if [ $? -ne 0 ] ; then
  logDebug -m $theModule \
           -l "Error: failed to chmod $_theDtDir or $_theSessionDir"
  return $KIOSK_RESULT_FAILED
 fi

 #
 ## Next create the lastsession file
 #
 echo "$KIOSK_PREFIX/lib/Xsession\c" > $_theLastSessionFile
 if [ $? -ne 0 ] ; then
  logDebug -m $theModule \
           -l "Error: failed to create file '$_theLastSessionFile'"
  return $KIOSK_RESULT_FAILED
 fi
 chmod 644 $_theLastSessionFile
 if [ $? -ne 0 ] ; then
  logDebug -m $theModule \
           -l "Error: failed to chmod file '$_theLastSessionFile'"
  return $KIOSK_RESULT_FAILED
 fi

 #
 ## Finally ensure correct ownership of all of the above
 #
 chown $USER:$GROUP $_theDtDir $_theSessionDir $_theLastSessionFile
 if [ $? -ne 0 ] ; then
  logDebug -m $theModule \
           -l "Error: failed to chown $_theDtDir, $_theSessionDir or $_theLastSessionFile"
  return $KIOSK_RESULT_FAILED
 fi

 return $KIOSK_RESULT_SUCCESS
}

#
## Main
##
## The sequence of events is
##
## 1. load the Kiosk common utils /opt/SUNWkio/lib/utils.sh
## 2. ensure the environment is sane
## 3. do a full cleanup for the user
## 4. create the user home directory and install the user prototype
## 5. install the primary session prototype
## 6. execute the primary session pre-session script if configured
## 7. setup the Kiosk Session Starter
## 8. start the Critical Applications Monitor
#
logger -i -p user.debug -t $theModule "setting up 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 "$KIOSK_PAM_SERVICE" -o -z "$DISPLAY" -o -z "$USER" -o \
     -z "$GROUP" -o -z "$HOME" -o -z "$KIOSK_PROTOTYPE" ] ; then
 logDebug -m $theModule \
          -l "Error: \$KIOSK_PAM_SERVICE, \$DISPLAY, \$USER, \$GROUP, \$DISPLAY or \$KIOSK_PROTOTYPE not specified"
 exit $KIOSK_RESULT_FAILED
fi

#
## Do a cleanup for the user before proceeding
#
cleanup $theModule $USER "$HOME"
if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
 logDebug -m $theModule \
          -l "Error: failed to clean up after previous Kiosk session for user '$USER'"
 exit $KIOSK_RESULT_FAILED
fi

#
## Create the dynamic data and user home directories
#
makeDynDir $theModule $USER
if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
 logDebug -m $theModule \
          -l "Error: failed to create dynamic data directory for user '$USER'"
 exit $KIOSK_RESULT_FAILED
fi

createHome $theModule $USER $GROUP "$HOME" "$KIOSK_PROTOTYPE"
if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
 logDebug -m $theModule \
          -l "Error: failed to set up home directory '$HOME' for user '$USER'"
 cleanup $theModule $USER "$HOME"
 exit $KIOSK_RESULT_FAILED
fi

#
## Install primary session prototype
#
loadSessionConfiguration $theModule $DISPLAY
if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
 logDebug -m $theModule -l "Error: failed to load Session Descriptor"
 cleanup $theModule $USER "$HOME"
 exit $KIOSK_RESULT_FAILED
fi
if [ ! -z "$KIOSK_SESSION_PROTOTYPE" ] ; then
 installPrototype $theModule "$KIOSK_SESSION_PROTOTYPE" $HOME $USER $GROUP
 if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
  logDebug -m $theModule -l "Error: failed to install session prototype"
  cleanup $theModule $USER "$HOME"
  exit $KIOSK_RESULT_FAILED
 fi
fi

#
## Execute primary session pre-session script, if specified
#
if [ ! -z "$KIOSK_SESSION_PRE" ] ; then
 if [ ! -x "$KIOSK_SESSION_PRE" ] ; then
  logDebug -m $theModule \
           -l "Error: session pre-session script '$KIOSK_SESSION_PRE' is not executable"
  cleanup $theModule $USER "$HOME"
  exit $KIOSK_RESULT_FAILED
 fi
 "$KIOSK_SESSION_PRE"
 if [ $? -ne 0 ] ; then
  logDebug -m $theModule \
           -l "Error: session pre-session script '$KIOSK_SESSION_PRE' failed"
  cleanup $theModule $USER "$HOME"
  exit $KIOSK_RESULT_FAILED
 fi
fi

#
## Setup the Kiosk Session Starter
#
setSessionStarter
if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
 logDebug -m $theModule -l "Error: failed to set correct session starter"
 cleanup $theModule $USER "$HOME"
 exit $KIOSK_RESULT_FAILED
fi

#
## Start critical applications monitor
#
startCriticalAppsMonitor $theModule
if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
 logDebug -m $theModule \
          -l "Error: failed to start Critical Applications Monitor"
 cleanup $theModule $USER "$HOME"
 exit $KIOSK_RESULT_FAILED
fi

logDebug -m $theModule \
         -l "Success: Kiosk session setup complete for display '$DISPLAY'"
exit $KIOSK_RESULT_SUCCESS
