#!/bin/ksh
# -----------------------------------------------------------------------------
# Copyright Dassault Systemes, 1999, 2010
# -----------------------------------------------------------------------------

# launch - Run a fiper program.
#
# This is called from each of the Fiper startup scripts as:
#       . "$dir/launch"
#
# Before this is called, the Fiper startup must source FIPER_HOME/bin/fiperenv
# and then set the following shell variables:
#
#    LaunchClasspath - complete class path for the program
#    LaunchArgs      - extra command-line arguments for the program
#    LaunchPgm       - class name of the Main class
#    LaunchPadClasspath - extra Jar files to add to the Class Path of the Launchpad.
#    skip_display_check - set to 1 for command-line programs that don't need $DISPLAY


########
# Check if X-Windows DISPLAY is set, but not for non-GUI programs

if [ -z "$DISPLAY" -a "${skip_display_check}" != "1" ]; then
    echo "The system environment variable 'DISPLAY' is not set." >&2
    echo "This variable must be set to allow Java programs to open windows." >&2
    echo "Please set this variable. For example, 'DISPLAY=:0.0; export DISPLAY'" >&2
    exit 1
fi

# Setup the the JVM parms for the Launchpad.
JVMParms="${JVMParms} '-Dfiper.launchpad.classpath=${LaunchClasspath}:${FIPER_ADDPATH}' \
'-Dfiper.launchpad.patchdir=$FIPER_HOME/docs/java/patch' ${FIPER_JVMPARMS}"
LaunchPadClasspath="$LaunchpadJar:${LaunchPadClasspath}"

# Actually run the program.
# The loop allows retrying after a logon failure.  We have to re-launch the JVM
# because the JNDI library holds on to the URL of the server.

while true ; do
    # NOTE: eval required so single-quotes inside parameter values will
	# actually quote the arguments.  Note doubled quotes on $@ to pass arguments through
    eval java ${JVMParms} -classpath "'${LaunchPadClasspath}'" com.engineous.system.launchpad.EsiLaunchPad ${LaunchPgm} ${LaunchArgs} '"$@"'

    # Trap return codes 42 and 49, otherwise break out
    # ------------------------------------------------
    #   return code 42 designates a Station Restart Command directed by ACS
    #   in response to an administrative request via the admin console.
    #
    #   LogonGUI returns code 49 to indicate a context creation failure
    #   resulting in a logon retry 
    retcode=$?
    if [ $retcode != 42 ] && [ $retcode != 49 ]; then break; fi;

    if [ $retcode -eq 42 ]; then
    	echo "station restart directed by server, restarting station ..." 
    else
    	echo "failed to connect to server, relaunching program..."
    fi    
done

# Get rid of FIPER_TEMPLIB.  Cannot be deleted from Java since it contains
# memory-mapped shared libraries.  Suppress all messages - I don't care!
if [ -n "$FIPER_TEMPLIB" -a -d "$FIPER_TEMPLIB" ]; then
	rm -rf "$FIPER_TEMPLIB" >/dev/null 2>&1
fi

# Hook to skip the exit in case the caller script needs to take further action
# Return code is returned in "$retcode"
if [ "${doNotExitLaunch}X" != "1X" ]; then
    exit $retcode
fi
