#!/bin/sh
#
#     SUSE system statup script for Hudson
#     Copyright (C) 2007  Pascal Bleser
#          
#     This library is free software; you can redistribute it and/or modify it
#     under the terms of the GNU Lesser General Public License as published by
#     the Free Software Foundation; either version 2.1 of the License, or (at
#     your option) any later version.
#			      
#     This library is distributed in the hope that it will be useful, but
#     WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#     Lesser General Public License for more details.
#      
#     You should have received a copy of the GNU Lesser General Public
#     License along with this library; if not, write to the Free Software
#     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
#     USA.
#
### BEGIN INIT INFO
# Provides:          hudson
# Required-Start:    $local_fs $remote_fs $network $time $named
# Should-Start: $time sendmail
# Required-Stop:     $local_fs $remote_fs $network $time $named
# Should-Stop: $time sendmail
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Hudson continuous build server
# Description:       Start the Hudson continuous build server
### END INIT INFO

# Check for missing binaries (stale symlinks should not happen)
HUDSON_WAR="/usr/lib/hudson/hudson.war"
test -r "$HUDSON_WAR" || { echo "$HUDSON_WAR not installed"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

# Check for existence of needed config file and read it
HUDSON_CONFIG=/etc/sysconfig/hudson
test -r "$HUDSON_CONFIG" || { echo "$HUDSON_CONFIG not existing";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }

HUDSON_PID_FILE="/var/run/hudson.pid"
HUDSON_USER="hudson"
HUDSON_GROUP="hudson"

# Read config	
. "$HUDSON_CONFIG"

. /etc/rc.status
rc_reset # Reset status of this service

# Set up environment accordingly to the configuration settings
[ -n "$HUDSON_HOME" ] || { echo "HUDSON_HOME not configured in $HUDSON_CONFIG";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }
[ -d "$HUDSON_HOME" ] || { echo "HUDSON_HOME directory does not exist: $HUDSON_HOME";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 1; fi; }
export HUDSON_HOME

if [ -z "$HUDSON_JAVA_HOME" ]; then
    . /etc/profile.d/alljava.sh
    [ -n "$JAVA_HOME" ] || { echo "Failed to determine JAVA_HOME, set HUDSON_JAVA_HOME in $HUDSON_CONFIG";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }
else
    JAVA_HOME="$HUDSON_JAVA_HOME"
fi
[ -d "$JAVA_HOME" ] || { echo "Invalid HUDSON_JAVA_HOME: directory does not exist: $JAVA_HOME";
    if [ "$1" = "stop" ]; then exit 0;
    else exit 6; fi; }
[ -e "$JAVA_HOME/bin/java" ] || { echo "Invalid HUDSON_JAVA_HOME: bin/java not found under $JAVA_HOME";
    if [ "$1" = "stop" ]; then exit 0;
    else exit 6; fi; }
export JAVA_HOME

JAVA_CMD="$JAVA_HOME/bin/java $HUDSON_JAVA_OPTIONS -jar $HUDSON_WAR"
PARAMS="--javaHome=$JAVA_HOME --logfile=/var/log/hudson/hudson.log"
[ -n "$HUDSON_PORT" ] && PARAMS="$PARAMS --httpPort=$HUDSON_PORT"
[ -n "$HUDSON_DEBUG_LEVEL" ] && PARAMS="$PARAMS --debug=$HUDSON_DEBUG_LEVEL"
[ -n "$HUDSON_HANDLER_STARTUP" ] && PARAMS="$PARAMS --handlerCountStartup=$HUDSON_HANDLER_STARTUP"
[ -n "$HUDSON_HANDLER_MAX" ] && PARAMS="$PARAMS --handlerCountMax=$HUDSON_HANDLER_MAX"
[ -n "$HUDSON_HANDLER_IDLE" ] && PARAMS="$PARAMS --handlerCountMaxIdle=$HUDSON_HANDLER_IDLE"

if [ "$HUDSON_ENABLE_ACCESS_LOG" = "yes" ]; then
    PARAMS="$PARAMS --accessLoggerClassName=winstone.accesslog.SimpleAccessLogger --simpleAccessLogger.format=combined --simpleAccessLogger.file=/var/log/hudson/access_log"
fi

case "$1" in
    start)
	echo -n "Starting Hudson "
	if /sbin/startproc -l /var/log/hudson.rc -u "$HUDSON_USER" -p "$HUDSON_PID_FILE" $JAVA_CMD $PARAMS; then
	    rc_status
	    # get own session ID
	    MY_SESSION_ID=`/bin/ps h -o sess -p $$`
	    # get PID
	    /bin/ps hww -u hudson -o sess,pid,cmd | \
		while read sess pid cmd; do [ "$sess" = "$MY_SESSION_ID" -a "$cmd" = "$JAVA_CMD $PARAMS" ] && echo $pid; done | \
		head -1 > "$HUDSON_PID_FILE"
	else
	    rc_failed 1
	fi
	rc_status -v
	;;
    stop)
	echo -n "Shutting down Hudson "
	PID=`cat "$HUDSON_PID_FILE" 2>/dev/null`
	if [ -n "$PID" ]; then
	    if /bin/kill -0 "$PID"; then
		# process exists
		/bin/kill -INT "$PID"
		rc=$?
		[ "$rc" = "0" ] && /bin/rm -f "$HUDSON_PID_FILE"
		rc_failed "$rc"
	    else
		rc_failed 7
	    fi
	else
	    rc_failed 1
	fi
	rc_status -v
	;;
    try-restart|condrestart)
	if test "$1" = "condrestart"; then
		echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
	fi
	$0 status
	if test $? = 0; then
		$0 restart
	else
		rc_reset	# Not running is not a failure.
	fi
	rc_status
	;;
    restart)
	$0 stop
	$0 start
	rc_status
	;;
    force-reload)
	echo -n "Reload service Hudson "
	$0 try-restart
	rc_status
	;;
    reload)
	rc_failed 3
	rc_status -v
	;;
    status)
	echo -n "Checking for service Hudson "
	/sbin/checkproc -p "$HUDSON_PID_FILE" "$JAVA_HOME/bin/java"
	rc_status -v
	;;
    probe)
	## Optional: Probe for the necessity of a reload, print out the
	## argument to this init script which is required for a reload.
	## Note: probe is not (yet) part of LSB (as of 1.9)

	test "$HUDSON_CONFIG" -nt "$HUDSON_PID_FILE" && echo reload
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
rc_exit
