#!/bin/sh
#
#
####
# This init-script tries to be LSB conform but platform independent.
# 
# Therefore check the following two variables to fit to your requests:
# HAVP_BIN HAVP_CONFIG PIDFILE
# Any configuration of HAVP is done in havp.config
# Type havp --help for help and read havp.config you should have received.

HAVP_BIN=/usr/local/sbin/havp
HAVP_CONFIG=/usr/local/etc/havp/havp.config
PIDFILE=/var/run/havp/havp.pid

# Return values acc. to LSB for all commands but status:
# 1       generic or unspecified error (current practice)
# 2       invalid or excess argument(s)
# 3       unimplemented feature (for example, "reload")
# 4       user had insufficient privilege
# 5       program is not installed
# 6       program is not configured
# 7       program is not running
# 8-99    reserved for future LSB use
# 100-149 reserved for distribution use
# 150-199 reserved for application use
# 200-254 reserved
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signaling is not supported) are
# considered a success.

reload_havp()
{
	echo "Reloading HAVP ..."
	PID="`cat $PIDFILE`"
	if [ "$PID" != "" ]; then
		kill -HUP "$PID" >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "Error: HAVP not running"
			exit 1
		fi
	else
		echo "Error: HAVP not running or PIDFILE not readable"
		exit 1
	fi
	exit 0
}

case "$1" in
	start)
		echo "Starting HAVP ..."
		if [ ! -f $HAVP_BIN ]; then
			echo "Error: $HAVP_BIN not found"
			exit 5
		fi
		$HAVP_BIN -c $HAVP_CONFIG
		exit $?
		;;

	stop)
		echo "Shutting down HAVP ..."
		if [ ! -f "$PIDFILE" ]; then
		  echo "Error: HAVP not running or PIDFILE unreadable"
		  exit 1
		fi
		PID="`cat $PIDFILE`"
		if [ "$PID" != "" ]; then
			kill -TERM "$PID" >/dev/null 2>&1
			if [ $? -ne 0 ]; then
				echo "Error: HAVP not running"
				exit 1
			fi
		else
			echo "Error: HAVP not running or PIDFILE unreadable"
			exit 1
		fi
		sleep 2
		exit 0
		;;

	restart)
		echo "Shutting down HAVP ..."
		$0 stop >/dev/null 2>&1
		$0 start
		exit $?
		;;

	reload-lists)
		reload_havp
		;;

	force-reload)
		reload_havp
		;;

	reload)
		reload_havp
		;;

	status)
		echo "Checking for service HAVP ..."
		exit 4
		;;

	*)
		echo "Usage: $0 {start|stop|status|restart|force-reload|reload|reload-lists}"
		exit 0
		;;
esac
