#!/bin/bash
#
# configt
# Configure SIAM via http locally
#
# Invokes configTooland uses 
# links text-based web browser to 
# configure SIAM over a serial console
#

########################
# Variable definitions
########################
opts="-m"
DFLTLOG="/dev/null"
DBGLOG=configt.out
LOGFILE=$DFLTLOG
DELAY=20
CONFIGTOOL=$SIAM_HOME/utils/configTool


########################
# Function definitions #
########################

#
# printUsage
#
printUsage(){
echo
echo "Usage: `basename $0` [-d] [-s <delaySec>] [<configTool options>] "
echo " -d : send output to $DBGLOG [$DFLTLOG]"
echo " -s : start delay [$DELAY]"
echo
echo "use configTool (no arguments) to see options"
echo
}

# Start node configuration server and surf to configuration web page

while [ "$#" -ge 1 ]
do
case "$1" in
-d)
   echo setting logfile=$DBGLOG
   LOGFILE=$DBGLOG
   shift
;;
-s)
   shift
   echo setting sleep=$1
   DELAY=$1
   if [ "$DELAY" -lt 0 ]
   then
       echo
       echo "Invalid sleep interval"
       echo
       printUsage
       exit
   fi
   shift
;;
--help)
   printUsage
   exit 0
;;
*)
   # don't eat the args for configTool 
   # stop parsing as soon as an unrecognized 
   # option is encountered
   opts="$*"
   break
;;
esac
done

if [ "$#" -ge 1 ]
then
    opts=$*
else
    opts="-m"
fi

echo "starting $CONFIGTOOL, output to $LOGFILE delay=$DELAY"
echo "opts: $opts"

# Run configTool
$CONFIGTOOL $opts >& $LOGFILE &

# If user kills this script, be sure to kill servlet as well
trap "echo killing configTool; kill $!" SIGINT SIGTERM

echo "Have to wait a bit... standby..."
sleep $DELAY

links http://localhost:8181/config
