#!/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.

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

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"
	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		


list=
list="$list $HOME/crm/jetty-crm.xml"
list="$list $HOME/tm/tm-1.0.xml"

export CLASSPATH
CLASSPATH=$CLASSPATH
$JETTY_HOME/bin/jetty.sh start $list admin.xml
	
