#!/bin/sh
# Startup script for the SNAP Calculation Server

# Source function library.
. /etc/rc.d/init.d/functions

# The user to run the calculation server as.
#   This user must exist already and must have read access to the
#   SNAP installation it will use.
USER="snapuser"
# The path to your java executable
JAVA=/usr/local/j2sdk1.4.1_01/jre/bin/java
# The installation directory for the SNAP package.
#   This example is installed in the 'Snap' directory
#   of 'snapuser's home diretory.
SNAP="~$USER/Snap"

#
# NO USER CONFIGURATION BELOW HERE
#

# The display name to use for syslog messages
prog="SNAP Calculation Server"
# The main class of the server
CLASS="nrcsnap.SnapExecMon.SnapExecMon"
# The classpath to use for the server, using the SNAP variable above
CLASSPATH="$SNAP/lib/nrcsnap.jar"
# The complete command to execute
COMMAND="$JAVA -classpath \"$CLASSPATH\" $CLASS &"
# The name to use for pid and 
BASE="snapcalc"

getpid() {
	pid=`ps -efwwwww | \
		 grep -F "$CLASS" | \
		 egrep -v "grep|$$|start|stop" | \
		 awk '{print $2}' | \
		 head -1`
}

start() {
    echo -n "Starting $prog: "
    daemon --user=$USER --check=$BASE "$COMMAND"
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ] ; then
		touch /var/lock/subsys/$BASE
		getpid
		echo "$pid" > /var/run/$BASE.pid
	fi
    return $RETVAL
}

stop() {
	getpid
    if test "x$pid" != x; then
	echo -n $"Stopping $prog: "
	killproc $BASE
	echo
    fi
    RETVAL=$?
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$BASE && rm -f /var/run/$BASE.pid
    return $RETVAL
}

case "$1" in
	start)
	    start
	    ;;
	
	stop)
	    stop
	    ;;

    restart)
        stop
        start
        ;;

    condrestart)
        if test "x`pidof $BASE`" != x; then
        stop
        start
        fi
        ;;
	
	*)
	    echo $"Usage: $0 {start|stop|restart|condrestart}"
	    exit 1

esac

exit 0
