#!/bin/bash
#
# Script to run the 'node' application 
#

#source ops bashrc so SIAM environement is setup
. /home/ops/.bashrc


# check to see if you want auto start or not
if [ "$SIAM_AUTOSTART" ] && [ $SIAM_AUTOSTART == FALSE ]
then
  echo "skipping SIAM auto start"
  echo "SIAM_AUTOSTART = FALSE"
  exit 0
fi

# Turn on comm link so that we can watch as system starts
if [ -n "$SIAM_PPP_SCRIPT" ]
then
  echo "Starting $SIAM_PPP_SCRIPT ppp link"
  $SIAM_HOME/utils/startPPP &
else
  # Try to synchronize clock with portal
  ntpdate -b $SIAM_PORTAL
fi


# swtich to SIAM_HOME dir
cd $SIAM_HOME

if [ ! -e logs ]; then
  echo Creating log directory $SIAM_HOME/logs
  mkdir logs
fi

#start node or nodeTest depending on wheter the SIAM_PORTAL env var is defined
if [ -n "$SIAM_PORTAL" ] 
then 
./utils/node $SIAM_PORTAL >& ./logs/node.`/bin/date +%Y%m%d%H%M` &
else
#issue warning if SIAM_PORTAL is not defined
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
echo "!             Warning SIAM_PORTAL not defined                 !"
echo "!             Starting node without portal                    !"
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
#make sure stdout gets flushed so the message shows up
sync
./utils/nodeTest >& ./logs/node.`/bin/date +%Y%m%d%H%M` &
fi

