#!/bin/bash
# Execute SIAM node application
# $Revision: 1.17 $";

if [ ! -n "$1" ]
then
  echo "usage: `basename $0` portalHostName"
  exit 1
fi

if [ -n "$2" ]
then
    case $2 in \
      ALL | DEBUG | INFO | WARN | ERROR | FATAL | OFF | NULL ) \
	LOG_THRESHOLD=$2 ;;
      * ) echo "usage: `basename $0` portalHostName [ ALL | DEBUG | INFO | WARN | ERROR | FATAL | OFF | NULL ]" ;
          exit 1;;
    esac
else
  LOG_THRESHOLD=INFO
fi  

# Define the node log file name, if it isn't defined.
# Normally defined by startnode
DFLT_NODE_LOG=$SIAM_HOME/logs/`hostname`.`/bin/date +%Y%m%d%H%M`
SIAM_NODE_LOG=${SIAM_NODE_LOG:-"$DFLT_NODE_LOG"}

# define log4j config, if not defined
# Normally be defined by siamEnv
SIAM_LOG4J=${SIAM_LOG4J:="$SIAM_HOME/properties/siam.log4j"}

# Define the exit script, if it isn't defined.
# Should be defined in /etc/siam/siamEnv.
exitScript=${SIAM_EXIT_SCRIPT:="/etc/siam/postSIAM"}

# Remove the post SIAM script.
# If it is needed, it will be generated
# automatically by the SIAM app before
# it exits.
if [ -f "$exitScript" ]
then
    rm -f $exitScript
fi

echo "Starting at $(date) [ Log Level @ " $LOG_THRESHOLD " ]"
echo "siam.exit.script=$exitScript"

cd $SIAM_HOME

# Script executes moos.deployed.NodeTest.main()
$JAVA $JAVA_OPTIONS -cp $SIAM_CLASSPATH \
-Djava.security.policy=$SIAM_HOME/properties/policy \
-Djava.rmi.server.codebase=$SIAM_CODEBASE \
-Dsiam_home=$SIAM_HOME -Dlog4j.threshold=$LOG4J_THRESHOLD \
-Dsiam.exit.script=$exitScript \
-Dsiam.node.log=$SIAM_NODE_LOG \
-Dsiam.log4j=$SIAM_LOG4J \
moos.deployed.NodeTest $1

# If the SIAM app has created a
# script to be executed after shutdown,
# execute it now
if [ -f "$exitScript" ]
then
    chmod 4775 $exitScript
    echo "executing $exitScript as `whoami`"    
    export MANAGELOG_DIR=${MANAGELOG_HOME:-"/mnt/hda/logs"}
    $exitScript 1>& /etc/siam/postExit.out
fi



