#!/bin/sh
#
# This script will start jetty if it not started or its PID
# doesn't indicate that it is running.
# It will check if $HOME/.jettystopped exist. If it does,
# it needs option '-f' to remove the file and start.
# (This allow admin to stop jetty without stopping the cron.) 
#
# set -x

# 
# $HOME/.jettyrc defines many jetty env variables
# We need JETTY_HOME and JETTY_PID.

addTornadoSupport(){
        TORNADO_HOME=/home/ctu/Tornado/IRMS3.0

        export LD_LIBRARY_PATH=$TORNADO_HOME/lib/jni/:$TORNADO_HOME/lib/dvl/extx:$LD_LIBRARY_PATH
export DMC_TABLE_PATH=$TORNADO_HOME/lib/dvl/extx/base2
}

# process options
for arg
do
case $arg in
   -f) /bin/rm -f $HOME/.jettystopped
	;;
   *) ;;  
esac
done

export LANG="zh_TW.big5"
export LC_ALL="zh_TW.big5"

if [ -f "$HOME/.jettystopped" ]
then

	echo "Warning: please run jettystart with -f option"
	exit 2
fi
 
if [ ! -f "$HOME/.jettyrc" ]
then
	echo "Error: missing $HOME/.jettyrc"
	exit 1
fi

. $HOME/.jettyrc

if [ ! -d "$JETTY_HOME" ]
then
	echo "Error: missing definition of JETTY_HOME"
	exit 1
fi

if [ -z "$JETTY_PID" ]
then
	echo "Error: missing definition of JETTY_PID"
	#$JETTY_HOME/bin/jetty.sh start $HOME/crm/jetty-crm.xml
	exit 1
fi
 

pid=`cat $JETTY_PID 2>/dev/null`
if [ "$pid" != "" ] 
then 
	# check if pid exists and is java
	fname=`ps --no-headers --pid $pid -o fname` 
	if [ "$fname" != "java" ] 
	then 
		# pid file is out-of-date
		echo "Jetty PID $pid is out-of-date, removing $JETTY_PID"
		rm -f $JETTY_PID
	fi
fi		


#nohup $JAVA_HOME/bin/rmiregistry &
addTornadoSupport
#Jetty 4.1.3
#$JETTY_HOME/bin/jetty.sh start $HOME/crm/jetty-crm.xml $HOME/tm/tm-1.0.xml $HOME/tm-oracle/tm-1.0.xml $HOME/tm-mssql/tm-1.0.xml $HOME/tm-tornado/tm-1.0.xml
$JETTY_HOME/bin/jetty.sh start $HOME/tm/tm-1.0.xml
#Jetty 6.x
#$JETTY_HOME/bin/jetty.sh start $HOME/tm/jetty6.xml $HOME/tm-mssql/jetty6.xml $HOME/tm-bpm-agentflow/jetty6.xml
#$JETTY_HOME/bin/jetty.sh start $HOME/tm/jetty6.xml
