#!/bin/bash
#
# This script is used to start the pcoip_agent application. The script can be
# called by another script that runs when a user logs in. The script accepts
# the following optional parameters.
#
# - DISPLAY environment variable
# - fully qualified path to the pcoip_agent executable to run
# - logfile containing the output from the pcoip_agent executable
# - pcoip_agent command line options
# - delay parameter that causes the script to wait the specified number of
#   seconds before starting the Host SW
# 

usage () {
    echo "Usage: pcoip_starthostsw [OPTION]"
    echo "Start the PCoIP Host SW executable (pcoip_agent)"
    echo ""
    echo "  -d   value for DISPLAY env variable       default = \":0\""
    echo "       (Note this setting is ignored if the DISPLAY variable is already initialized)"
    echo "  -e   /path/pcoip_agent                    default = /usr/bin/pcoip_agent"
    echo "  -x   XAUTHORITY cookie"
    echo "  -h   help"
    echo "  -o   \"pcoip_agent command line options\"   default = \"\""
    echo "  -w   number of seconds to wait before starting the Host SW"
    echo "       (Note this option is useful when using this script to start the host SW after logging in)"
    echo ""
    echo "Examples:"
    echo "  To start the pcoip_agent located in directory /public/bin with"
    echo "  the options '-drcs -t -ll=4' and pipe the output to the log file"
    echo "  pcoip_agent.log in the directory /tmp enter the following command:"
    echo ""
    echo "    pcoip_starthostsw -e /public/bin/pcoip_agent -l /tmp/pcoip_agent.log -o \"-drcs -t -ll=4\""
    echo ""
    echo "  To start the pcoip_agent in a shell with the DISPLAY environment variable"
    echo "  equal to ":2.0" enter the following command:"
    echo ""
    echo "    pcoip_starthostsw -d \":2.0\""
    echo ""
}

# process the command line arguments
#for var in "$@"
while [ -n "$1" ]
do
    case "$1" in
    -h)     usage
            exit 1;;
    -d)     shift
            DISPLAY_VAR="$1"
            shift;;
    -e)     shift
            HOSTSW_EXE="$1"
            shift;;
    -x)     shift
            XCOOKIE="$1"
            shift;;
    -o)     shift
            HOSTSW_CMD_LINE_OPTIONS="$HOSTSW_CMD_LINE_OPTIONS $1"
            shift;;
    -w)     shift
            START_DELAY="$1"
            shift;;
    *)      echo "pcoip_starthostsw: invalid option $1"
            echo "Try 'pcoip_starthostsw -h' for more information"
            exit 1;;
    esac
done

# configure default settings if the caller didn't specify values for each setting
if [ -z "${DISPLAY_VAR+xxx}" ]; then DISPLAY_VAR=":0"; fi
if [ -z "${HOSTSW_CMD_LINE_OPTIONS+xxx}" ]; then HOSTSW_CMD_LINE_OPTIONS=""; fi
if [ -z "${START_DELAY+xxx}" ]; then START_DELAY="0"; fi

if [ -z "${HOSTSW_EXE+xxx}" ]; 
then
    HOSTSW_EXE="/usr/bin/pcoip_agent";
fi

#echo "DISPLAY_VAR = $DISPLAY_VAR"
#echo "HOSTSW_EXE = $HOSTSW_EXE"
#echo "HOSTSW_CMD_LINE_OPTIONS = $HOSTSW_CMD_LINE_OPTIONS"
#echo "START_DELAY = $START_DELAY"

if [ -e $HOSTSW_EXE ]
then
    #echo "Starting Host SW: $HOSTSW_EXE DISPLAY = $DISPLAY Start delay = $START_DELAY seconds"
    sleep $START_DELAY

    # define and export the DISPLAY variable if it isn't defined
    if [ -z "$DISPLAY" ] 
    then
        #echo "Setting DISPLAY env var = $DISPLAY_VAR"
        DISPLAY=$DISPLAY_VAR
        export DISPLAY
    fi

    if [ -e $XCOOKIE ]
    then 
        XAUTHORITY=$XCOOKIE
        export XAUTHORITY
    fi

    # Kill any existing host SW instances that may have been started by a session manager
    # This is necessary in a default KDE environment.
    EXE_NAME=${HOSTSW_EXE/\/*\//}
    killall -q $EXE_NAME

    # start the Host SW
    $HOSTSW_EXE $HOSTSW_CMD_LINE_OPTIONS
else
    echo "ERROR: unable to find $HOSTSW_EXE executable"
fi
