#!/bin/sh
#
# /etc/init.d/helma1 -- startup script for the Helma web framework
#
# Written by Miquel van Smoorenburg <miquels@cistron.nl>.
# Modified for Debian GNU/Linux	by Ian Murdock <imurdock@gnu.ai.mit.edu>.
# Modified for Tomcat by Stefan Gybas <sgybas@debian.org>.
# Modified for Tomcat6 by Thierry Carrez <thierry.carrez@ubuntu.com>.
# Modified for Helma by Hannes Wallnoefer <hannes@helma.at>.
#
### BEGIN INIT INFO
# Provides:          helma1
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Should-Start:      $named
# Should-Stop:       $named
# Default-Start:     2 3 4 5
# Default-Stop:      1
# Short-Description: Start Helma web application framework.
# Description:       Start Helma web application framework.
### END INIT INFO

set -e

PATH=/bin:/usr/bin:/sbin:/usr/sbin
NAME=helma1
DESC="Helma web application framework"
DAEMON=/usr/bin/jsvc
HELMA_HOME=/usr/share/$NAME
DEFAULT=/etc/default/$NAME

if [ `id -u` -ne 0 ]; then
	echo "You need root privileges to run this script"
	exit 1
fi
 
# Make sure helma is started with system locale
if [ -r /etc/default/locale ]; then
	. /etc/default/locale
	export LANG
fi

. /lib/lsb/init-functions
. /etc/default/rcS


# The following variables can be overwritten in $DEFAULT

# Run Helma as this user ID
HELMA_USER=helma1

# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not
# defined in $DEFAULT)
JDK_DIRS="/usr/lib/jvm/java-6-openjdk /usr/lib/jvm/java-6-sun /usr/lib/jvm/java-1.5.0-sun /usr/lib/j2sdk1.5-sun /usr/lib/j2sdk1.5-ibm"

# Look for the right JVM to use
for jdir in $JDK_DIRS; do
    if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; then
	JAVA_HOME="$jdir"
    fi
done
export JAVA_HOME

# Directory for per-instance configuration files and webapps
HELMA_BASE=/var/lib/$NAME

# Default Java options
# Set java.awt.headless=true if JAVA_OPTS is not set so the
# Xalan XSL transformer can work without X11 display on JDK 1.4+
# It also looks like the default heap size of 64M is not enough for most cases
# so the maximum heap size is set to 128M
if [ -z "$JAVA_OPTS" ]; then
	JAVA_OPTS="-Djava.awt.headless=true -Xmx128M"
fi

# End of variables that can be overwritten in $DEFAULT

# overwrite settings from default file
if [ -f "$DEFAULT" ]; then
	. "$DEFAULT"
fi

if [ ! -f "$HELMA_HOME/launcher.jar" ]; then
	log_failure_msg "$NAME is not installed"
	exit 1
fi

if [ ! -f "$DAEMON" ]; then
	log_failure_msg "missing $DAEMON"
	exit 1
fi

# Set juli LogManager if logging.properties is provided
if [ -r "$HELMA_BASE"/logging.properties ]; then
  JAVA_OPTS="$JAVA_OPTS "-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" "-Djava.util.logging.config.file="$HELMA_BASE/logging.properties"
fi

# Define other required variables
HELMA_PID="/var/run/$NAME.pid"
BOOTSTRAP_CLASS=helma.main.launcher.Main
JSVC_CLASSPATH="/usr/share/java/commons-daemon.jar:$HELMA_HOME/launcher.jar"

case "$1" in
  start)
	if [ -z "$JAVA_HOME" ]; then
		log_failure_msg "no JDK found - please set JAVA_HOME"
		exit 1
	fi

	if [ ! -d "$HELMA_BASE" ]; then
		log_failure_msg "invalid HELMA_BASE: $HELMA_BASE"
		exit 1
	fi

	log_daemon_msg "Starting $DESC" "$NAME"
	if start-stop-daemon --test --start --pidfile "$HELMA_PID" \
		--user $HELMA_USER --startas "$JAVA_HOME/bin/java" \
		>/dev/null; then

		cd "$HELMA_BASE"

		$DAEMON -user "$HELMA_USER" -cp "$JSVC_CLASSPATH" \
		    -outfile SYSLOG -errfile SYSLOG \
		    -pidfile "$HELMA_PID" $JAVA_OPTS "$BOOTSTRAP_CLASS" \
		    -h "$HELMA_BASE"

		sleep 1
        	if start-stop-daemon --test --start --pidfile "$HELMA_PID" \
			--user $HELMA_USER --startas "$JAVA_HOME/bin/java" \
			>/dev/null; then
			log_end_msg 1
		else
			log_end_msg 0
		fi
	else
	        log_progress_msg "(already running)"
		log_end_msg 0
	fi
	;;
  stop)
	log_daemon_msg "Stopping $DESC" "$NAME"
        if start-stop-daemon --test --start --pidfile "$HELMA_PID" \
		--user "$HELMA_USER" --startas "$JAVA_HOME/bin/java" \
		>/dev/null; then
		log_progress_msg "(not running)"
	else
		$DAEMON -cp "$JSVC_CLASSPATH" -pidfile "$HELMA_PID" \
		     -stop "$BOOTSTRAP_CLASS"
	fi
	log_end_msg 0
	;;
   status)
        if start-stop-daemon --test --start --pidfile "$HELMA_PID" \
		--user $HELMA_USER --startas "$JAVA_HOME/bin/java" \
		>/dev/null; then

		if [ -f "$HELMA_PID" ]; then
		    log_success_msg "$DESC is not running, but pid file exists."
			exit 1
		else
		    log_success_msg "$DESC is not running."
			exit 3
		fi
	else
		log_success_msg "$DESC is running with pid `cat $HELMA_PID`"
	fi
        ;;
  restart|force-reload)
        if start-stop-daemon --test --stop --pidfile "$HELMA_PID" \
		--user $HELMA_USER --startas "$JAVA_HOME/bin/java" \
		>/dev/null; then
		$0 stop
		sleep 1
	fi
	$0 start
	;;
  try-restart)
        if start-stop-daemon --test --start --pidfile "$HELMA_PID" \
		--user $HELMA_USER --startas "$JAVA_HOME/bin/java" \
		>/dev/null; then
		$0 start
	fi
        ;;
  *)
	log_success_msg "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
	exit 1
	;;
esac

exit 0

