#!/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/.gnome2/session.
#
addDescToStartup()
{
 inModule=$1
 inDesc=$2
 inMode=$3
 inArgs=$4

 theTmpFile="$HOME/.gnome2/session.tmp"
 theSessionFile="$HOME/.gnome2/session"

 mkdir -p "$HOME/.gnome2" &&
 echo "[Default]" > $theTmpFile
 if [ $? -ne 0 ] ; then
  logDebug -m $inModule -l "Error: failed to create file '$theTmpFile'"
  return $KIOSK_RESULT_FAILED
 fi

 theNumClients=`grep '^num_clients=' "$theSessionFile" |
                sed 's/num_clients=//g'`
 theNumClients=`expr $theNumClients + 1`
 grep -v '^\[Default\]'  "$theSessionFile" |
 grep -v '^num_clients=' >> $theTmpFile
 theIndex=`expr $theNumClients - 1`
 theArgs="$inDesc $inArgs"
 if [ "$inMode" = critical ] ; then
  theArgs="-c $theArgs"
 fi
 cat << ! >> $theTmpFile
$theIndex,id=default$theIndex
$theIndex,Priority=50
$theIndex,RestartCommand=$KIOSKAPPLAUNCH $theArgs
num_clients=$theNumClients
!
 if [ $? -ne 0 ] ; then
  logError -m $inModule -l "Error: failed to create file '$theTmpFile'"
  return $KIOSK_RESULT_FAILED
 fi

 mv $theTmpFile $theSessionFile
 if [ $? -ne 0 ] ; then
  logError -m $inModule -l "Error: failed to create file '$theSessionFile'"
  return $KIOSK_RESULT_FAILED
 fi
 return $KIOSK_RESULT_SUCCESS
}

#
## Create a desktop launcher and menu entry for a specified application. This is
## done by creating .desktop files in $HOME/Desktop and
## $HOME/.kiosk/menus/applications. The .desktop files 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

 menulauncher_=$KIOSKAPPMENUDIR/kioskapplauncher$$.desktop
 dtlauncher_=$HOME/Desktop/kioskapplauncher$$.desktop
 
 theTextDomain="${KIOSK_APP_TEXTDOMAIN:-$FALLBACK_TEXTDOMAIN}"
 theTextDomainDir="${KIOSK_APP_TEXTDOMAINDIR:-$FALLBACK_TEXTDOMAINDIR}"

 mkdir -p $HOME/Desktop &&
 cat << ! > $dtlauncher_
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Exec=$KIOSKAPPLAUNCH $inDesc $inArgs
TryExec=
Icon=$KIOSK_APP_ICON
X-GNOME-DocPath=
Terminal=false
Name=$(localize "$KIOSK_APP_LABEL" "$theTextDomain" "$theTextDomainDir")
GenericName=$(localize "$KIOSK_APP_LABEL" "$theTextDomain" "$theTextDomainDir")
Comment=$(localize "$KIOSK_APP_DESCRIPTION" "$theTextDomain" "$theTextDomainDir")
!
 if [ $? -ne 0 ] ; then
  logError -m $inModule -l "Error: failed to create file '$dtlauncher_'"
  return $KIOSK_RESULT_FAILED
 fi

 cp $dtlauncher_ $menulauncher_
 if [ $? -ne 0 ] ; then
  logError -m $inModule -l "Error: failed to create file '$menulauncher_'"
  return $KIOSK_RESULT_FAILED
 fi
 return $KIOSK_RESULT_SUCCESS
}

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

KIOSKLIBDIR=/opt/SUNWkio/lib
KIOSKAPPLAUNCH=$KIOSKLIBDIR/kioskapplaunch

KIOSKMENUDIR=$HOME/.kiosk/menus
KIOSKAPPMENUDIR=$KIOSKMENUDIR/applications
KIOSKDESCDIR=$HOME/.kiosk/desc

logger -i -p user.debug -t $theModule "adding jds3 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 $inModule -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 $inModule -l "Error: invalid  application type '$inType' specified"
  exit $KIOSK_RESULT_FAILED
  ;;
esac

#
## Add the application as an auto start application or as a desktop launcher
## and 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
