#!/bin/sh
#
# intermapperd        Start/Stop the InterMapper daemon
#
#                     Usually, you will copy this file into /etc/init.d and
#                     create links to this file in /etc/rc3.d/S97intermapperd
#                     and /etc/rc6.d/K03intermapperd.
#
# chkconfig:          345 97 3
# description:        intermapperd is a network monitoring and alerting program. \
#                     See intermapperd(1) for more information.
#
# processname:        intermapperd
# config:             /usr/local/etc/intermapperd.conf

### BEGIN INIT INFO
# Provides:           intermapperd
# Required-Start:     $remote_fs $network $named $time $syslog
# Required-Stop:      $remote_fs $network $named $time $syslog
# Default-Start:      2 3 4 5
# Default-Stop:       0 1 6
# Short-Description:  Start InterMapper at boot time
# Description:        intermapperd is a network monitoring and alerting program.
#                     See intermapperd(1) for more information.
### END INIT INFO

PREFIX="/usr/local"
PROGNAME="intermapperd"
PROGPATH="$PREFIX/bin/$PROGNAME"

CONFPATH="$PREFIX/etc/intermapperd.conf"
RELAUNCHPATH="$PREFIX/share/intermapper/relaunch"
LOCKPATH="/var/lock/subsys/$PROGNAME"

PROGOPTS="-f $CONFPATH"
LAUNCHER="$PROGPATH"

# Load RedHat's library of shell functions

. /etc/rc.d/init.d/functions

RETVAL=0

# A function to stop a program; override the broken version shipped by RedHat
# to not wait the full timeout if the daemon exits sooner

killproc() {

	local RC killlevel= base pid pid_file= delay delayseconds

	RC=0; delay=3

	if [ "$#" -eq 0 ]; then
		echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
		return 1
	fi
	if [ "$1" = "-p" ]; then
		pid_file=$2
		shift 2
	fi
	if [ "$1" = "-d" ]; then
		delay=$2
		shift 2
	fi

	# Check for second arg to be kill level

	[ -n "${2:-}" ] && killlevel=$2

	base=${1##*/}

	# Find pid

	__pids_var_run "$1" "$pid_file"
	if [ -z "$pid_file" -a -z "$pid" ]; then
		pid="$(__pids_pidof "$1")"
	fi

	# Kill it.

	if [ -n "$pid" ] ; then
		[ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base "
		if [ -z "$killlevel" ]; then
			if checkpid $pid 2>&1; then

				# TERM first, then KILL if not dead

				kill -TERM $pid >/dev/null 2>&1
				usleep 100000
				delayseconds=0

				while [ $delayseconds -lt $delay ] && checkpid $pid ; do 
					delayseconds=`expr $delayseconds + 1`
					sleep 1
				done

				if checkpid $pid ; then
					kill -KILL $pid >/dev/null 2>&1
					usleep 100000
				fi
			fi

			checkpid $pid
			RC=$?
			[ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
			RC=$((! $RC))

		else
			if checkpid $pid; then
				kill $killlevel $pid >/dev/null 2>&1
				RC=$?
				[ "$RC" -eq 0 ] && success $"$base $killlevel" || failure $"$base $killlevel"
			elif [ -n "${LSB:-}" ]; then
				RC=7 # Program is not running
			fi
		fi
	else
		if [ -n "${LSB:-}" -a -n "$killlevel" ]; then
			RC=7 # Program is not running
		else
			failure $"$base shutdown"
			RC=0
		fi
	fi

	# Remove pid file if any.

	if [ -z "$killlevel" ]; then
		rm -f "${pid_file:-/var/run/$base.pid}"
	fi

	return $RC
}

start() {
	echo -n "Starting InterMapper: "
	daemon $LAUNCHER $PROGOPTS
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch "$LOCKPATH"
	return $RETVAL
}

stop() {
	echo -n "Stopping InterMapper: "
	# The launcher will exit automatically if intermapperd exits normally
	killproc -d 600 "$PROGPATH"
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f "$LOCKPATH"
	return $RETVAL
}

restart() {
	stop
	start
}

test -x "$PROGPATH" || exit 5

# Read settings from intermapperd.conf

if [ -f "$CONFPATH" ]; then
	SETTINGS_DIR=`grep "^SettingsFolder" "$CONFPATH" | sed -e "s/^SettingsFolder[ \t]*\"\{0,1\}//;s/\"\{0,1\}[ \t]*$//"`
fi

if [ -z "$SETTINGS_DIR" ]; then
	SETTINGS_DIR="/var/local/InterMapper_Settings"
fi

# If the relaunch script is available, use it to launch IM

if [ -x "$RELAUNCHPATH" ]; then
	LAUNCHER="$RELAUNCHPATH"
	PROGOPTS="--start $PROGPATH $PROGOPTS --no-daemonize"
fi

case "$1" in
start)
	start
	;;
stop)
	stop
	;;
status)
	status "$PROGNAME"
	RETVAL=$?
	;;
restart)
	restart
	;;
condrestart)
	[ -e "$LOCKPATH" ] && restart
	;;
reload)
	echo -n "Reloading InterMapper: "
	killproc "$PROGPATH" -HUP
	RETVAL=$?
	echo
	;;
--install)
	trap "" 1
	. "$PREFIX/share/intermapper/Extension.sh"
	shift
	do_install "$@"
	;;
--uninstall)
	trap "" 1
	. "$PREFIX/share/intermapper/Extension.sh"
	shift
	do_uninstall "$@"
	;;
*)
	echo "Usage: /etc/init.d/intermapperd (start|stop|status|restart|condrestart|reload|--install EXTENSION|--uninstall EXTENSION)" >&2
	exit 1
	;;
esac

exit $RETVAL
