#!/bin/sh
#
#     SUSE system statup script for Jenkins Enterprise HA monitor
#     Copyright (C) 2007  Pascal Bleser, CloudBees, Inc.
#          
#     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:          jenkins-ha-monitor
# Required-Start:    $local_fs $remote_fs $network $time $named
# Required-Stop:     $local_fs $remote_fs $network $time $named
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Jenkins Enterprise High-Availability monitor
# Description:       Start Jenkins Enterprise High-Availability monitor
### END INIT INFO

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

# Check for existence of needed config file and read it
JENKINS_HAMON_CONFIG=/etc/sysconfig/jenkins-ha-monitor
test -e "$JENKINS_HAMON_CONFIG" || { echo "$JENKINS_HAMON_CONFIG not existing";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }
test -r "$JENKINS_HAMON_CONFIG" || { echo "$JENKINS_HAMON_CONFIG not readable. Perhaps you forgot 'sudo'?";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }

JENKINS_HAMON_PID_FILE="/var/run/jenkins-ha-monitor.pid"

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

# Read config	
[ -f "$JENKINS_HAMON_CONFIG" ] && . "$JENKINS_HAMON_CONFIG"

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

if [ -z "$JAVA_HOME" ]; then

    for candidate in /etc/alternatives/java /usr/lib/jvm/java-1.8.0/bin/java /usr/lib/jvm/jre-1.8.0/bin/java /usr/lib/jvm/java-1.7.0/bin/java /usr/lib/jvm/jre-1.7.0/bin/java /usr/bin/java
    do
      [ -x "$JENKINS_JAVA_CMD" ] && break
      JENKINS_JAVA_CMD="$candidate"
    done
else
    JENKINS_JAVA_CMD="$JAVA_HOME/bin/java"
fi

JAVA_CMD="$JENKINS_JAVA_CMD $JENKINS_HAMON_JAVA_OPTIONS -jar $JENKINS_HAMON_JAR"
PARAMS="-home $JENKINS_HOME -daemon -pidfile $JENKINS_HAMON_PID_FILE"
[ -n "$JENKINS_HAMON_ARGS" ] && PARAMS="$PARAMS $JENKINS_HAMON_ARGS"
[ -n "$JENKINS_HAMON_LOG" ]  && PARAMS="$PARAMS -log $JENKINS_HAMON_LOG"

case "$1" in
    start)
	echo -n "Starting Jenkins Enterprise HA monitor"
	daemon --pidfile "$JENKINS_HAMON_PID_FILE" $JAVA_CMD $PARAMS > /dev/null
	RETVAL=$?
	if [ $RETVAL = 0 ]; then
	    success
	else
	    failure
	fi
	echo
	;;

    stop)
	echo -n "Shutting down Jenkins Enterprise HA monitor"
	killproc -p $JENKINS_HAMON_PID_FILE jenkins-ha-monitor
	RETVAL=$?
	echo
	;;

    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
		: # Not running is not a failure.
	fi
	;;

    restart)
	$0 stop
	$0 start
	;;

    force-reload)
	echo -n "Reload service Jenkins Enterprise HA monitor"
	$0 try-restart
	;;

    reload)
    	$0 restart
	;;

    status)
    	status jenkins-ha-monitor
	RETVAL=$?
	;;

    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 "$JENKINS_HAMON_CONFIG" -nt "$JENKINS_HAMON_PID_FILE" && echo reload
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
exit $RETVAL
