#!/bin/sh

APP_NAME=@APP_NAME@
APP_MAIN=@APP_MAIN@

# Used to start and stop the application

APP_CANON=`readlink -f $0`
APP_HOME=`dirname $APP_CANON`/..

JAVA_OPTS=-client
#JAVA_OPTS="-server -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000"

cd $APP_HOME

APP_LIB=./lib
APP_LOG=./var/log
APP_RESOURCES=./etc
APP_PID=./var/run/${APP_NAME}.pid
APP_ENV=./etc/${APP_NAME}.env


APP_CLASSPATH=
for nm in `find $APP_LIB -name '*.jar'`; do APP_CLASSPATH=$APP_CLASSPATH:$nm; done

CLASSPATH=
CLASSPATH=$CLASSPATH:$APP_RESOURCES
CLASSPATH=$CLASSPATH:$APP_CLASSPATH

HOME_DEF=-D${APP_NAME}.home=$APP_HOME
JDEF="$HOME_DEF -Djava.endorsed.dirs=$APP_LIB"

if [ -f $APP_ENV ]; then source $APP_ENV; fi

export CLASSPATH
export JAVA_HOME

if [ ! -d "$JAVA_HOME" ]; then
	JAVA_HOME=/opt/jre1.6.0_02
fi

if [ -z "$JAVA_HOME" -o ! -d "$JAVA_HOME" ]; then
	JAVA_HOME=/opt/sun-jdk-1.5.0.08
fi

if [ ! -d "$JAVA_HOME" ]; then
	JAVA_HOME=/opt/jre1.5.0_09
fi

if [ ! -d "$JAVA_HOME" ]; then
	echo "unable to find a suitable JAVA_HOME"
	exit 1
fi

function init()
{
	if [ -e $APP_PID ]; then
		CURRENT_PID=`cat $APP_PID`
		PS_LINE=`ps -p $CURRENT_PID|grep $CURRENT_PID`
		if [ -z "$PS_LINE" ]; then
			echo "Removing stale PID file: $APP_PID"
			rm $APP_PID
		else
			echo "There is either a copy of the application running with pid $CURRENT_PID,"
			echo "or the process shut down unexpectedly. Please check and if necessary"
			echo "remove $APP_PID manually."
			exit 1
		fi
	fi

	touch $APP_PID

	if [ ! -w $APP_PID ]; then
		echo "Unable to create pid file $APP_PID."
		echo "Please resolve the problem in order to start the bridge"
		exit 1
	fi
}

function start()
{

	init

	echo "Starting Application..."
	echo "Java Home: " $JAVA_HOME
	echo "Application Home: " $APP_HOME
	echo "Command Line: " $*

	$JAVA_HOME/bin/java $JAVA_OPTS $JDEF -classpath $CLASSPATH $APP_MAIN $* 1>>$APP_LOG/$APP_NAME.stdout 2>>$APP_LOG/$APP_NAME.stderr &

	echo "$!" > $APP_PID
}


function run()
{
	init

	echo "Starting Application..."
	echo "Java Home: " $JAVA_HOME
	echo "Application Home: " $APP_HOME
	echo "Command Line: " $*

	$JAVA_HOME/bin/java $JAVA_OPTS $JDEF -classpath $CLASSPATH $APP_MAIN $*
		
	rm $APP_PID
}

function stop()
{
	if [ ! -r $APP_PID ]; then
		echo "Unable to read the pid from $APP_PID."
		echo "The process will not be requested to stop"
		exit 1
	fi

	CURRENT_PID=`cat $APP_PID`
	if [ -z "$CURRENT_PID" ]; then
		echo "The pid retieved from $APP_PID was blank"
		echo "The process will not be requested to stop"
		exit 1
	fi

#	COUNT=`ps p $CURRENT_PID|grep "^[[:space:]]*$CURRENT_PID"|wc -l|awk '{print $1}'`
#	if [ "$COUNT" != "1" ]; then
#		echo "The process $CURRENT_PID is not running"
#		echo "The process will not e requested to stop"
#		exit 1
#	fi

	if kill $CURRENT_PID; then
		rm $APP_PID
	else
		echo "Failed to send TERM signal to PID $CURRENT_PID"
		echo "Maybe $APP_PID is a stale pid file."
	fi

}

case $1 in
	start)
		shift 1
		start $*
	;;
	stop)
		shift 1
		stop $*
	;;
	run)
		shift 1
		run $*
	;;
	*) echo "Unknown action '$1'. Usage: $0 (start|stop|run)"
esac
