#!/bin/sh
#
# chkconfig: 345 80 20
# description: hsqldbwebserver
# processname: hsqldbwebserver
# Go through the steps and edit the appropriate variables
# and place this file in /etc/init.d/
# Make sure only root can read this file
# Send any questions to fchoong@user.sourceforge.net
# -----------------------------------------------------


# Step 1: Specify your dbhome here
# eg. dbhome="/usr/hsqldb/"
dbhome="/usr/hsqldb/"

# Step 2: change this to point to your JDK directory.
# eg. jdkhome="/usr/java/j2sdk1.4.0/"
jdkhome="/usr/java/j2sdk1.4.0/"

# Step 3: change to the appropriate port, user and password
dbport="80"
dbuser="sa"
dbpassword=""

# Step 4(Optional): Set the MAX startup time and shutdown time.
# Increase these values with large databases
STARTUP_MAXSEC_DEFAULT=420
SHUTDOWN_MAXSEC_DEFAULT=420

# Step 5(Optional): Set user params
# eg. userparams="-database testdb"
userparams=""

# append on command line or ide.cfg, with -J prefix on each
jreflags=""

# location of the log files
STDOUTLOGFILE="$dbhome/data/webserverstdout.log"
STDERRLOGFILE="$dbhome/data/webserverstderr.log"

# location of the pid file
PIDFILE="$dbhome/data/hsqldbwebserver.pid"

# absolutize dbhome

dbhome=`cd ${dbhome}; pwd`

#
# bring in needed functions

. ${dbhome}/lib/functions

# Source function library.
if [ -x /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
fi

failout() {
	echo "Script Failure: $1" >> $STDERRLOGFILE
	echo_failure
	exit 1
}

start()
{
	echo -n "Starting hsqldbwebserver: "
	[ -n "$DPID" ] && {
    	echo -n -e "\nDaemon already running (with pid $DPID)\n" 1>&2
	failout startup
        }
        [ -f $STDOUTLOGFILE ] && age "$STDOUTLOGFILE"
	[ -f $STDERRLOGFILE ] && age "$STDERRLOGFILE"
	pre_main

	#
	# let's go
	#
    	cd $dbhome/data

	localfuncparams=""
	localfuncparams="$userparams"

	if [ "$dbport" != "80" -a ! -z "$dbport" ]; then
		localfuncparams="$localfuncparams -port $dbport"
	fi # if [ "$dbport" != "80" -a ! -z "$dbport" ]; then

	nohup sh -c 'echo $$'" > $PIDFILE; cd $dbhome/data; exec \"$jdkhome/bin/java\" $thread_flag -classpath \"$cp\" $jargs org.hsqldb.WebServer $localfuncparams $@ <&- > $STDOUTLOGFILE 2> $STDERRLOGFILE" > /dev/null 2>&1 &

	ctr=0
	while [ $ctr -lt $STARTUP_MAXSEC ]; do
		let ctr=ctr+1
		grep "^HSQLDB web server .* is running$" $STDOUTLOGFILE \
		> /dev/null 2>&1 && echo_success && echo && echo "startup took $ctr sec" >> $STDOUTLOGFILE && return 0
		echo -n
		sleep 1
	done
	echo "STARTUP_MAXSEC of $STARTUP_MAXSEC elapsed" >> $STDOUTLOGFILE
	failout startup
	return 1
} # start()

stop()
{
	echo -n "Stopping hsqldbwebserver: "
	[ -n "$DPID" ] || {
	echo -n -e '\nUnable to determine pid of process\n' 1>&2
	failout stopping
        }
	pre_main

	#
	# let's go
	#
    	cd $dbhome/data

	localfuncparams=""
	localfuncparams="$userparams"

	if [ "$dbport" != "80" -a ! -z "$dbport" ]; then
		localfuncparams="$localfuncparams -port $dbport"
	fi # if [ "$dbport" != "80" -a ! -z "$dbport" ]; then

	if [ ! -z "$dbuser" ]; then
		localfuncparams="$localfuncparams -user $dbuser"
	fi # if [ ! -z "$dbuser" ]; then

	if [ ! -z "$dbpassword" ]; then
		localfuncparams="$localfuncparams -password $dbpassword"
	fi # if [ ! -z "$dbpassword" ]; then

    		exec "$jdkhome/bin/java" $thread_flag -classpath "$cp" $jargs org.hsqldb.util.ShutdownServer -webserver true $localfuncparams <&- >> $STDOUTLOGFILE 2>> $STDERRLOGFILE &

	ctr=0
	while [ $ctr -lt $SHUTDOWN_MAXSEC ]; do
		let ctr=ctr+1
		grep "SHUTDOWN : System.exit() is called next$" $STDOUTLOGFILE \
		> /dev/null 2>&1 && echo_success && echo && echo "shutdown took $ctr sec" >> $STDOUTLOGFILE && rm -f $PIDFILE && return 0
		echo -n
		sleep 1
	done
	echo "SHUTDOWN_MAXSEC of $SHUTDOWN_MAXSEC elapsed." >> $STDOUTLOGFILE
	echo_warning
	echo "SHUTDOWN_MAXSEC of $SHUTDOWN_MAXSEC elapsed, maybe you should try to kill the process instead."
	return 1
} # stop()

restart()
{
    stop
    start
} # restart()

killprocess()
{
    echo -n "Killing hsqldbwebserver: "
    [ -n "$DPID" ] || {
	echo -n -e '\nUnable to determine pid of process' 1>&2
	failout killing
    }
    kill -HUP $DPID 2>&- || {
	echo -n -e '\nKill -HUP failed' 1>&2
	failout killing
    }
    for cnt in 1 2 3 4 5; do
    	kill -0 $DPID 2>&- || break
	sleep 1
    done
    kill -0 $DPID 2>&- && {
    	echo '   Daemon is really being stubborn.  Sending KILL signal...'
	kill -KILL $DPID 2>&- || {
	    echo -n -e '\nKill -KILL failed' 1>&2
	    failout shutdown
    	}
    }
    for cnt in 1 2 3 4 5; do
    	kill -0 $DPID 2>&- || break
	sleep 1
    done
    kill -0 $DPID 2>&- && {
	echo -n -e "Daemon is really stuck bad.  You're on your own..." 1>&2
	failout shutdown
    }
    echo_success
    rm -f $PIDFILE
    echo
    exit 0
} # killprocess()

checkstatus()
{
    if [ -n "$DPID" ]; then
	exec ps -lp $DPID
    else
    	echo 'Not running'
	exit 1
    fi
} # checkstatus()


[ -n "$STARTUP_MAXSEC" ] || STARTUP_MAXSEC=$STARTUP_MAXSEC_DEFAULT
[ -n "$SHUTDOWN_MAXSEC" ] || SHUTDOWN_MAXSEC=$SHUTDOWN_MAXSEC_DEFAULT


if [ -r $PIDFILE ]; then DPID="`< $PIDFILE`"
else DPID=; fi

case "$DPID" in ''|[0-9]*);; *)
    echo "Bad Pid File" >> $STDOUTLOGFILE
    echo -e "\nBad Pid File" 1>&2
    failout "DPID"
esac
case "$DPID" in ''|*[0-9]);; *)
    echo "Bad Pid File" >> $STDOUTLOGFILE
    echo -e "\nBad Pid File" 1>&2
    failout "DPID"
esac

[ -n "$DPID" ] && {
    kill -0 $DPID >&- || {
    	echo -e "\nPid $DPID is not running.  Removing pid file." 1>&2
	DPID=
	rm -f $PIDFILE
    }
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        sleep 5
        start
        ;;
  kill)
	killprocess
  	;;
  status)
	checkstatus
    ;;
  *)
        echo "Usage: hsqldbwebserver {start|stop|restart|kill|status}"
        exit 1
esac

exit $RETVAL
