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

theModule=kiosk:kioskapplaunch

usage()
{
    cat <<!
kioskapplaunch: launch kiosk application according to an application descriptor
Usage:
      kioskapplaunch [-c] <application descriptor> [<arg1> <arg2> ... <argn>]
!
}


#
## Main
#

#
## Load utilities and setup basic CAM 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

if [ $# -lt 1 ] ; then
 usage
 exit $KIOSK_RESULT_FAILED
fi

#
## parse the arguments
#
if [ $1 = "-c" ] ; then
 if [ $# -lt 2 ]  ; then
  usage
  exit $KIOSK_RESULT_FAILED
 fi
 isCritical=1
 theDescriptor=$2
 shift
else
 isCritical=0
 theDescriptor=$1
fi
shift
theArgs=$@

#
## Next verify that KIOSK_APP_EXEC is specified and is executable
## Also verify the KIOSK_APP_PRE & KIOSK_APP_POST are executable if specified
#
loadApplicationDescriptor $theModule "$theDescriptor"
if [ $? -ne $KIOSK_RESULT_SUCCESS ] ; then
 exit $KIOSK_RESULT_FAILED
fi

if [ -z "$KIOSK_APP_EXEC" ] ; then
 logDebug -m $theModule -l "Error: \$KIOSK_APP_EXEC is not specified"
 exit $KIOSK_RESULT_FAILED
fi
if [ ! -x "$KIOSK_APP_EXEC" ] ; then
 logDebug -m $theModule -l "Error: '$KIOSK_APP_EXEC' is not executable"
 exit $KIOSK_RESULT_FAILED
fi

runPre=0
runPost=0
if [ ! -z "$KIOSK_APP_PRE" ] ; then
 if [ ! -x "$KIOSK_APP_PRE" ] ; then
  logDebug -m $theModule \
           -l "Error: application pre-application script '$KIOSK_APP_PRE' is not executable"
  exit $KIOSK_RESULT_FAILED
 fi
 runPre=1
fi

if [ ! -z "$KIOSK_APP_POST" ] ; then
 if [ ! -x "$KIOSK_APP_POST" ] ; then
  logDebug -m $theModule \
        -l "Error: application pre-application script '$KIOSK_APP_POST' is not executable"
  exit $KIOSK_RESULT_FAILED
 fi
 runPost=1
fi


#
## Execute KIOSK_APP_PRE if specified
#
if [ $runPre -eq 1 ] ; then
 $KIOSK_APP_PRE
 if [ $? -ne 0 ] ; then
  logDebug -m $theModule \
           -l "Error: application pre-application script '$KIOSK_APP_PRE' failed"
   exit $KIOSK_RESULT_FAILED
 fi
fi

#
## Execute KIOSK_EXEC
#
if [ -z "$theArgs" ] ; then
 theArgs="$KIOSK_APP_ARGS"
fi
if [ $isCritical -eq 1 ] ; then
 eval "launchCriticalApp '$theModule' '$KIOSK_APP_EXEC' $theArgs"
 theRC=$?
else
 logDebug -m $theModule -l "starting application '$KIOSK_APP_EXEC $theArgs'"
 eval "'$KIOSK_APP_EXEC' $theArgs"
 if [ $? -ne 0 ] ; then
  theRC=$KIOSK_RESULT_FAILED
 else
  theRC=$KIOSK_RESULT_SUCCESS
 fi 
fi 
if [ $theRC -ne 0 ] ; then
 logDebug -m $theModule -l "Error: '$KIOSK_APP_EXEC $theArgs' with return code $?"
fi

#
## Finally, execute KIOSK_APP_POST if specified
#
if [ $runPost -eq 1 ] ; then
 $KIOSK_APP_POST
 if [ $? -ne 0 ] ; then
  logDebug -m $theModule \
           -l "Error: application pre-application script '$KIOSK_APP_POST' failed"
   exit $KIOSK_RESULT_FAILED
 fi
fi
exit $theRC
