#!/bin/sh
#
# Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
#

theModule=kiosk:vdm

#################### USER CONFIGURABLE OPTIONS ################################
theJavaExec=/etc/opt/SUNWut/jre/bin/java

# For SSL connections:
# Set the location of the Java key store. If you are using a self signed
# certificate it must be imported into the keystore using keytool.
javaKeyStore=$KIOSK_SESSION_DIR/keystore
javaKeyStorePass=

USER_SCRIPT=$KIOSK_SESSION_DIR/vdm-user.sh

####################### BEGIN SESSION SCRIPT###################################

theClient=$KIOSK_SESSION_DIR/vdmclient.jar
theUser=`$USER_SCRIPT`

theUTTSCScript=/opt/SUNWuttsc/bin/uttsc

if [ -n "$theUser" ]; then
    if [ "$theUser" != "$SUN_SUNRAY_TOKEN" ]; then
        userNameArg="$theUser"
    fi
fi

xset fp+ /usr/openwin/lib/X11/fonts/TTbitmaps

errLog=/tmp/vdm.$$
trap "rm -f $errLog; exit" INT TERM EXIT

# Connect to the VDM Connection server, request a VM, and open an RDP connection to it
if [ -n "$userNameArg" ]; then
	result=`$theJavaExec -client -Djavax.net.ssl.trustStore=$javaKeyStore -Djavax.net.ssl.trustStorePassword=$javaKeyStorePass -jar $theClient -uttsc $theUTTSCScript -m "$userNameArg" "$@" > $errLog 2>&1`
else
	result=`$theJavaExec -client -Djavax.net.ssl.trustStore=$javaKeyStore -Djavax.net.ssl.trustStorePassword=$javaKeyStorePass -jar $theClient -uttsc $theUTTSCScript "$@" > $errLog 2>&1`
fi
retCode=$?
cat $errLog | logger -i -p user.error -t $theModule

if [ $retCode -gt 0 ]; then
    logger -i -p user.error -t $theModule \
       "Error: Could not locate Virtual Machine for '$theUser': exit code: $retCode $result"
fi
exit $retCode
