#!/bin/sh -f

cmd=$0          # for convenience of usage() fn

sim_scenario=$1 # prefix

base_name=cameracontrol

idea_not_fast_ext="_g"
idea_fast_ext="_o"

modules_not_fast="modules.cfg"
modules_fast="modules_fast.cfg"

post_init_modules_not_fast="post_init_modules.cfg"
post_init_modules_fast="post_init_modules_fast.cfg"

plasma_prefix="plasma_"

#	Directories
simscript_dir=$PWD/Scenarios
ipc_dir=$PWD/../../ipc
idea_dev_bin_dir=$PWD/../../IDEA-DEV/bin
convert2_path=$PWD/../../NewPlan/ModelLoader/converter/convert2
plasma_dir=$PWD/../../PLASMA

#	Timing service
pool_name="TimingService"

warping=true
timescale=1

#	Other
output_level="medium"

fast_option="not-fast"

if [ -e xml ]; then
    xslt_dir=xml
else
    echo "Creating xml link: $idea_dev_bin_dir"
    ln -s $idea_dev_bin_dir xml
fi

if [ ! -e convert2 ] ; then
    echo "Creating convert2 link: $convert2_path"
    ln -s $convert2_path convert2
fi

if [ ! -e plasma ] ; then
    echo "Creating PLASMA link: $plasma_dir"
    ln -s $plasma_dir plasma
fi

# helper fn
scenarios ()
{
    echo "Available scenarios:"
    ls $simscript_dir | grep '\.script' | sed -e 's/^/ /g' -e 's/.script//g'
    echo " "
}

# helper fn

usage ()
{
  echo " "
  echo "Usage: "
  echo "     $cmd  <script_base_name> <mode = 1 for slave only, 2 for slave and master> <TEMPEST plan file name> <fast_option> <no_convert_marker>"
  echo " "
  echo "Examples: "
  echo "     $cmd nominal 1 plan.rcl"
  echo "     $cmd nominal 1 plan.rcl fast x : run in fast mode and do not rebuild the model, init state and heuristics"
  echo ""
  echo "Defaults:"
  echo "     output_level = $output_level"
  echo "     warping      = $warping"
  echo "     code compiled= $fast_option"
  scenarios
}

# usage check
if [ "$1" = '-h' -o "$1" = '--help' ] ; then
    echo "******* Asking for usgae $1"
    usage 
    exit
elif [ $# -lt 1 ] ; then
    echo " "
    echo "******* Error: Not enough arguments!"
    usage
    exit
elif [ $# -gt 5 ] ; then
    echo " "
    echo "******* Error: Too many arguments!"
    usage
    exit
fi

if [ -r Scenarios/$1.slave ]
then
  slave=$1.slave
else
   echo "Slave simulation scenario not found"
   echo " "
   exit
fi

if [ $2 ]; then
    echo "running in fast mode"
    fast_option=$2
fi

# Get environment
os=`uname -s`
osver=`uname -r`
if [ $os = 'SunOS' ] ; then
    if ( echo $r | grep '5\..*' > /dev/null ) ; then o="Solaris" ; fi
fi

if [ $os = 'Linux' ] ; then
  # cut off patch level, keep only major and minor OS version numbers.
    osver=`perl -e '$$_ = shift @ARGV; ($$major,$$minor,$$patchlevel) = split(/\./,$$_,3); print "$$major.$$minor\n";' $osver`
fi

./clean-sandbox

if [ ! $3 ] ; then
# Convert Model
    echo " "
    echo "Converting model ..."
    echo "xml/xiddl2plasma-so $base_name.xiddl $base_name.init $base_name.heuristics $fast_option"
    xml/xiddl2plasma-so $base_name.xiddl $base_name.init $base_name.heuristics $fast_option debug
fi

# Instantiate fast option parameters
if [ $fast_option = 'not-fast' ] ; then
    idea_ext=$idea_not_fast_ext
    modules=$modules_not_fast
else
    idea_ext=$idea_fast_ext
    modules=$modules_fast
    fast=-fast    #file name suffix
fi

# Catch errors from here on down
# Signal 0 is normal exit (no signal) ??
# Signal 2 is SIGINT, keyboard interrupt
# Signal 3 is SIGQUIT, keyboard quit
# Signal 9 is SIGKILL
# Signal 15 is SIGTERM, termination signal (default for 'kill')
trap 'echo; echo "** Stopping agent **"; kill -9 $sim_pid; sleep 1; kill -INT $central_pid; kill -INT $timing_service_pid' 0 2 3 9 15

# Make sim directory
log_dir=$PWD/Logs/$1
if [ ! -e $log_dir ] ; then
    echo "Making log directory $log_dir"
    mkdir -p $log_dir
fi

# Start central
# Specify a unique port for central
central_port="`date +3%V%S`"
echo " "
echo "Starting IPC central ..."
central_cmd="$ipc_dir/bin/$os-$osver/central"
central_log=$log_dir/$base_name-ipc.log
echo $central_cmd 
echo "IPC Central log: $central_log"
$central_cmd -u 1> $central_log 2>&1 &
central_pid=$!
echo IPC central PID is $central_pid
echo

sleep 1

# Start Timing Service
echo " "
echo "Starting timing service ..."

ts_cmd="xml/timingservice_sh$idea_ext -w $warping -s $timescale -a 1 -b 1"
ts_log=$log_dir/$base_name-ts.log

echo $ts_cmd
echo "Timing service log: $ts_log"
$ts_cmd 1> $ts_log 2>&1 &
timing_service_pid=$!
echo Timing service PID is $timing_service_pid
echo " "

sleep 1

# Start IPC simulator
sim_cmd="xml/simulator_sh$idea_ext -n SIMULATOR -s Scenarios/$1.slave -p TimingService"
sim_log=$log_dir/$base_name-sim.log
echo "Starting $sim_cmd"

$sim_cmd 1> $sim_log 2>&1 &

sleep 1

# Start the Agent
library='./'$plasma_prefix$base_name$idea_ext

echo " "
echo "Starting the agent ..."
start_agent_cmd="xml/agentrunner_sh$idea_ext \
   -s $modules \
   -i $plasma_prefix$base_name.init.nddl.xml \
   -m $plasma_prefix$base_name.xiddl \
   -l $library \
   -r $plasma_prefix$base_name.heuristics.xml \
   -b $plasma_prefix$base_name.heuristics.no_branch \
   -p $pool_name \
   -o $output_level \
   -f $base_name.output \
   -x post-init-modules$fast.cfg"

agent_log=$log_dir/$base_name.log

echo $start_agent_cmd
echo "Agent log: $agent_log"
$start_agent_cmd 1> $agent_log 2>&1 
agent_pid=$!
echo "Agent PID is $agent_pid"
echo " "

#wait $mission_agent_pid
echo "Cleaning up all shared memory bindings"
./clean-sandbox

if [ "$central_pid" != "" ]
then
  echo Stopping central
  kill -9 $central_pid
fi

exit

# clean-sandbox now lives in Idea3/TestTools, so resist the temptation
# of renaming this as "./clean-standbx"
./clean-sandbox

#  EOF
