#!/bin/ksh
#
# ident "@(#)$Id$ Oracle"
#
# Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
#

theModule=kiosk:Xsession

applyLimit()
{
 inOpt=$1
 inLimit=$2

 if [ ! -z "$inLimit" ] ; then
  ulimit $inOpt "$inLimit"
  if [ $? -ne 0 ] ; then
   logDebug -m $theModule -l "Error: failed to execute 'ulimit $inOpt $inLimit'"
   exit $KIOSK_RESULT_FAILED
  fi
 fi
}

logger -i -p user.debug -t $theModule "starting 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

#
## Load the session descriptor to figure out the primary session exec.
#
loadSessionConfiguration $theModule 
if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
 logDebug -m $theModule -l "Error: failed to load Session Descriptor"
 exit $KIOSK_RESULT_FAILED
fi

#
## Ensure KIOSK_SESSION_EXEC is specified for in Session Descriptor
#
if [ -z "$KIOSK_SESSION_EXEC" ] ; then
 logDebug -m $theModule -l "Error: \$KIOSK_SESSION_EXEC not specified"
 exit $KIOSK_RESULT_FAILED
fi

#
## Apply any configured resource limits
#
applyLimit -n "$KIOSK_SESSION_LIMIT_DESCRIPTORS"
#
## Note: on Linux, filesize is given in 1024b blocks whereas on Solaris
##       512b blocks are used
#
if [ ! -z "$KIOSK_SESSION_LIMIT_FILESIZE" ] ; then
 theOS=`uname`
 if [ "$theOS" = "Linux" -a "$KIOSK_SESSION_LIMIT_FILESIZE" != "unlimited" ]
 then
  KIOSK_SESSION_LIMIT_FILESIZE=`expr "$KIOSK_SESSION_LIMIT_FILESIZE" \* 2` 
  if [ $? -ne 0 ] ; then
   logDebug -m $theModule \
            -l "Error: failed to evaluate KIOSK_SESSION_LIMIT_FILESIZE"
   exit $KIOSK_RESULT_FAILED
  fi
 fi
fi
applyLimit -f "$KIOSK_SESSION_LIMIT_FILESIZE"
applyLimit -v "$KIOSK_SESSION_LIMIT_VMSIZE"
applyLimit -t "$KIOSK_SESSION_LIMIT_CPU"

#
## Apply KIOSK_SESSION_LOCALE if configured
#
if [ ! -z "${KIOSK_SESSION_LOCALE}" ] ; then
 LANG=$KIOSK_SESSION_LOCALE
 LC_ALL=$KIOSK_SESSION_LOCALE
 export LANG LC_ALL
fi

# 
## Find session scripts
#
if [ -z "$KIOSK_SESSION_SCRIPT_FILES" ] ; then
 if [ -z "$KIOSK_SESSION_SCRIPT_DIRS" ] ; then
  case "$KIOSK_PAM_SERVICE" in
   dtlogin*) 
    _dt_script_dir="dt/config/Xsession.d"
    KIOSK_SESSION_SCRIPT_DIRS="/etc/$_dt_script_dir /usr/$_dt_script_dir"

    # Setup environment expected by some Solaris Xsession.d scripts
    DTSTARTIMS=${KIOSK_SESSION_DTSTARTIMS:-True}
    if [[ $DTSTARTIMS = "False" ]] ; then
     unset DTSTARTIMS
    fi 
    if  [ -n "$KIOSK_SESSION_DTSESSIONTYPE" ] ; then
     SESSIONTYPE=$KIOSK_SESSION_DTSESSIONTYPE
     export SESSIONTYPE
    fi
    ;;
   gdm*)
    # No predefined script dirs for gdm, atm
    # standard xinit.d scripts are sourced *before* we get invoked
    ;;
   *)
    logDebug \
        -m $theModule \
        -l "Unknown login service '$KIOSK_PAM_SERVICE' - not sourcing session scripts"
    ;; 
  esac
 fi  
 if [ -n "$KIOSK_SESSION_SCRIPT_DIRS" ] ; then
  for sd in $KIOSK_SESSION_SCRIPT_DIRS ; do
   if [ -d "$sd" ] ; then
# This is modeled after the code in Solaris /usr/dt/bin/Xsession
    for sf in `ls $sd` ; do 
     if [ -x "$sd/$sf" -a \( ! -d "$sd/$sf" \) ] ; then
       KIOSK_SESSION_SCRIPT_FILES="$KIOSK_SESSION_SCRIPT_FILES $sd/$sf"
     fi
    done
   fi
  done
 fi
fi

# 
## Source session scripts
#
if [ -n "$KIOSK_SESSION_SCRIPT_FILES" ] ; then
 for sf in $KIOSK_SESSION_SCRIPT_FILES ; do
  if [ -x "$sf" -a \( ! -d "$sf" \) ] ; then
   logDebug -m $theModule -l "Sourcing session script $sf."
   . "$sf"
  fi 
 done
 logDebug -m $theModule -l "Done sourcing session scripts."
else
 logDebug -m $theModule -l "No session scripts to source."
fi

#
## Set up session environment
#
if [ -z "$KIOSK_SESSION_DIR" ] ; then
 KIOSK_SESSION_DIR="$KIOSK_SESSIONS_DIR/$KIOSK_SESSION"
fi
export KIOSK_SESSION_DIR

#
## Export requested parameters into the session
## Do this late, as it could change PATH and similar things
#
if [ -n "$KIOSK_SESSION_ENV" ] ; then
  exportSessionConfiguration $theModule $KIOSK_SESSION_ENV
  if [ $? -ne 0 ] ; then
   logDebug -m $theModule \
            -l "Error: failed to evaluate KIOSK_SESSION_ENV"
   exit $KIOSK_RESULT_FAILED
  fi
fi

#
## Finally, start the primary session
#
eval "launchCriticalApp $theModule '$KIOSK_SESSION_EXEC' $KIOSK_SESSION_ARGS"
theRC=$?
logDebug -m $theModule \
         -l "Kiosk session '$KIOSK_SESSION_EXEC $KIOSK_SESSION_ARGS' exited with return code $theRC"
exit $theRC
