
#!/bin/ksh
#
#*******************************************************************************
#
# Copyright 2008 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.
#

# Sanitize PATH
PATH=/usr/bin:/bin

#
## Create a startup application ( possibly critical ). This is done by adding
## a kioskapplaunch entry to $HOME/.dt/sessions/sessionetc
#
addDescToStartup()
{
 inModule=$1
 inDesc=$2
 inMode=$3
 inArgs=$4

#theSessionFile=$HOME/.dt/sessions/sessionetc (at last logically)
 theSessionFile=$KIOSK_AUTOAPPFILE_
 if [ ! -f "$theSessionFile" ] ; then
  # Absence of this file might indicate that a session file should not be built 
  # As this is currently not used, warn about this
  logWarning -m $inModule \
      -l "Autostart file '$theSessionFile' is missing - skipping entry"   
  return $KIOSK_RESULT_SUCCESS
 fi

 theArgs="$inDesc $inArgs"
 if [ $inMode = critical ] ; then
  theArgs="-c $theArgs"
 fi

# Note: the cam-compat session uses the format of these lines to check 
#       whether the app list is sane. 
 echo $KIOSKAPPLAUNCH $theArgs ' &' >> $theSessionFile

 if [ $? -ne 0 ] ; then
  logError -m $inModule \
      -l "Error: failed to append to autostart file '$theSessionFile'"
  return $KIOSK_RESULT_FAILED
 fi

 return $KIOSK_RESULT_SUCCESS
}

#
## Create a desktop menu entry for a specified application. This is
## done by appending to the user dtwmrc file
## The menu entries use kioskapplaunch to
## launch the relevant application.
#
addDescToDesktop()
{
 inModule=$1
 inDesc=$2
 inArgs=$3

 loadApplicationDescriptor $theModule "$inDesc"
 if [ $? -ne 0 ] ; then
  return $KIOSK_RESULT_FAILED
 fi

#theUserMenu=$HOME/.dt/dtwmrc (at least logically)
 theUserMenu=$KIOSK_DTMENUFILE_
 if [ ! -f "$theUserMenu" ] ; then
  # Absence of this file indicates that a menu should not be built 
  # (no-dtsession mode)   
  return $KIOSK_RESULT_SUCCESS
 fi

 theExecLine="f.exec \"$KIOSKAPPLAUNCH $inDesc $inArgs\"" 

 echo "\"$KIOSK_APP_LABEL\"\t\t $theExecLine" >> $theUserMenu
 if [ $? -ne 0 ] ; then
  logError -m $inModule -l "Error: failed to append to menu file '$theUserMenu'"
  return $KIOSK_RESULT_FAILED
 fi

 if [ ! -f "$KIOSK_DTLOCALIZEDMENU_" ] ; then
  # Absence of this file indicates that a localized menu should not be built 
  return $KIOSK_RESULT_SUCCESS
 fi

 theLocalizedMenu="$KIOSK_DTLOCALIZEDMENU_"
 theTextDomain="${KIOSK_APP_TEXTDOMAIN:-$FALLBACK_TEXTDOMAIN}"
 theTextDomainDir="${KIOSK_APP_TEXTDOMAINDIR:-$FALLBACK_TEXTDOMAINDIR}"
 theLocalizedLabel=`localize "$KIOSK_APP_LABEL" "$theTextDomain" "$theTextDomainDir"` 

 echo "\"$theLocalizedLabel\"\t\t $theExecLine" >> "$theLocalizedMenu"
 if [ $? -ne 0 ] ; then
  logWarning -m $inModule \
      -l "Failed to append to menu file '$theLocalizedMenu'"

  rm -f "$theLocalizedMenu"    
  if [ $? -ne 0 ] ; then
   logError -m $inModule \
	-l "Error: failed to discard incomplete menu file '$theLocalizedMenu'"
   return $KIOSK_RESULT_FAILED
  fi
 fi
 return $KIOSK_RESULT_SUCCESS
}

#
## Main
#
theModule=kiosk:cde-session:applauncher

KIOSKLIBDIR=/opt/SUNWkio/lib
KIOSKAPPLAUNCH=$KIOSKLIBDIR/kioskapplaunch
KIOSKDESCDIR=$HOME/.kiosk/desc

# Should be defined in the environment (from kiosk-appintegrate):
# KIOSK_AUTOAPPFILE_=$HOME/.dt/sessions/sessionetc
# KIOSK_DTMENUFILE_=$HOME/.dt/dtwmrc
# KIOSK_DTLOCALIZEDMENU_ (optional)

logger -i -p user.debug -t $theModule "adding cde session application"

#
## Load utilities and setup basic Kiosk Environment
#
theUtilsFile=$KIOSKLIBDIR/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

inType=$1
inAppName=$2
inMode=$3
inArgs=$4

#
## install application prototype if specified
#
if [ "$inType" = desc ] ; then
 loadApplicationDescriptor $theModule "$inAppName"
 if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
  logError -m $theModule \
           -l "Error: failed to load application descriptor '$inAppName'"
  exit $KIOSK_RESULT_FAILED
 fi
 if [ ! -z "$KIOSK_APP_PROTOTYPE" ] ; then
  theGroup=`/usr/xpg4/bin/id -g`
  installPrototype $theModule "$KIOSK_APP_PROTOTYPE" "$HOME" "$USER" "$theGroup"
  if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
   logError -m $theModule \
            -l "Error: failed to install prototype '$KIOSK_APP_PROTOTYPE'"
   exit $KIOSK_RESULT_FAILED
  fi
 fi
fi

#
## Create a descriptor for applications of type "exec"
#
case "$inType" in
 exec)
  theDesc="$KIOSKDESCDIR/kioskdesc$$.conf"
  execToApplicationDescriptor $theModule "$inAppName" "$theDesc"
  if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
   logError -m $theModule -l "Error: failed to create descriptor '$theDesc'"
   exit $KIOSK_RESULT_FAILED
  fi
  # ensure the binary name is not localized
  FALLBACK_TEXTDOMAIN=
  ;;

 desc)
  theDesc=$inAppName
  # Ensure localization is attempted with the session settings
  FALLBACK_TEXTDOMAIN=${KIOSK_SESSION_TEXTDOMAIN:-$KIOSK_TEXTDOMAIN}
  FALLBACK_TEXTDOMAINDIR=${KIOSK_SESSION_TEXTDOMAINDIR:-$KIOSK_TEXTDOMAINDIR}
  ;;

 *)
  logError -m $theModule -l "Error: invalid  application type '$inType' specified"
  exit $KIOSK_RESULT_FAILED
  ;;
esac

#
## Add the application as an auto start application or as a desktop menu entry
#
if [ "$inMode" = auto -o "$inMode" = critical ] ; then
 addDescToStartup $theModule "$theDesc" "$inMode" "$inArgs"
 if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
  exit $KIOSK_RESULT_FAILED
 fi
fi
if [ "$inMode" = auto -o "$inMode" = user ] ; then
 addDescToDesktop $theModule "$theDesc" "$inArgs"
 if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
  exit $KIOSK_RESULT_FAILED
 fi
fi
exit $KIOSK_RESULT_SUCCESS
