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

#
## 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.
#

#
## CDE Kiosk Session script
#
## Supported session args (options)
##   "-no-dtsession" - Run a compatibility session with the application list,
##                     but no dtlogin
##   Possible future options
##   "-dtsession"    - Run a CDE session with the application list (default)
##   "-cde=full"     - Do full CDE startup 
##   "-cde=basic"    - Do simplified (faster) CDE startup (default)
## 
#
theModule=kiosk:cde-session

#debug
# set -x
# exec > /var/tmp/cde-kiosk-$$.log 2>&1

#
## Main
##
## 1. create an empty desktop. 
##    This done entirely by the cde prototype now, but extra processing  
##    could be added here.
## 2. process the application list
## 3. launch dtsession
#

logger -i -p user.info -t $theModule "Starting CDE Kiosk session on display '$DISPLAY'"

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

# some globals
KIOSK_COMPATSESSION=$KIOSK_SESSION_DIR/cam-compat-session
KIOSKAPPHELPER="$KIOSK_SESSION_DIR/kiosk-appintegrate" 

# A good candidate for a new parameter
# Only takes effect when $useDtsession="yes"
fullCdeSupport="no"

# 
## Evaluate parameters
#
useDtsession="yes"
while [  $# -ge 1 ] ; do
    case "$1" in
    "-no-dtsession")  useDtsession="no"  ;;
#   "-dtsession")     useDtsession="yes"  ;;
#   "-cde=full")      fullCdeSupport="yes"  ;;
#   "-cde=basic")     fullCdeSupport="no"  ;;
    *) 
	logError -m "$theModule" -l "Unrecognized session parameter '$1'"
	exit $KIOSK_RESULT_FAILED
	;;
    esac
    shift
done

#
## 1. Prepare empty desktop
#

#
## 2. Process applications
#

# load helper
if [ ! -x "$KIOSKAPPHELPER" ] ; then 
    logError -m "$inModule" \
	-l "Cannot execute the application integration helper '$KIOSKAPPHELPER'"
    exit $KIOSK_RESULT_FAILED
fi
. "$KIOSKAPPHELPER"

# Run the applist building

logDebug -m "$theModule" -l "Starting application integration"
prepareAppIntegration "$theModule" "$useDtsession" &&
buildAppIntegration "$theModule" &&
finishAppIntegration "$theModule" 

if [ $? -ne 0 ] ; then
    exit $KIOSK_RESULT_FAILED
fi

#
## 3. Launch the session
#

# 
# Prepare for session startup
# 
if [ $useDtsession = "yes" ] ; then
  # CDE-specific session preparation
  # Default is "no" for things that are typically not needed in a kiosk session
  useDtAppgather="$fullCdeSupport" # needed, if Application Manager is used,
                                   # see dtappgather(1)
  useDtdbcache="$fullCdeSupport"   # accelerates some parts of CDE, e.g. dtfile
  useDsdm="$fullCdeSupport"        # needed if DnD between OPENLOOK and Motif 
                                   # apps is needed - see dsdm(1X)
  useTooltalk="yes"                # needed by many CDE applications
                                   # see ttsession(1)

  logDebug -m "$theModule" -l "Setting up CDE environment"

  # CDE session setup actions done by /usr/dt/bin/Xsession
  DT_BINPATH=/usr/dt/bin
  $DT_BINPATH/dtsession_res -load -system -xdefaults
  eval `$DT_BINPATH/dtsearchpath`

  if [ $useDtAppgather = "yes" ] ; then
    $DT_BINPATH/dtappgather & 
  fi  
  if [ $useDsdm = "yes" ] ; then
    $DT_BINPATH/dsdm          
  fi  
  if [ $useDtdbcache = "yes" ] ; then
    $DT_BINPATH/dtdbcache -init & 
  fi  
  if [ $useTooltalk = "yes" ] ; then
    PATH=${DT_BINPATH}:${PATH}:/usr/openwin/bin \
	 $DT_BINPATH/ttsession 
  fi  
fi

#
## Select dtsession or its non-dtsession replacement
#
if [ "$useDtsession" != "no" ] ; then 
    theSession="$DT_BINPATH/dtsession"
    # Cannot use "-norestore" - suppresses ~/.dt/sessions/sessionetc execution
    theSessionArgs=
else
    # Pass the session autostart file as parameter
    # this var is set in the kiosk-appintegrate helper
    theSession="$KIOSK_COMPATSESSION"
    theSessionArgs="$KIOSK_AUTOAPPFILE_"
fi
logDebug -m "$theModule" -l "Launching session: $theSession $theSessionArgs"
exec $theSession $theSessionArgs

# if we got here, exec failed
logError -m "$theModule" -l "exec for session '$theSession' failed"
return $KIOSK_RESULT_FAILED
