#!/bin/sh
#
# template script for starting supervised processes
#
# ari edelkind (03/07/2000)
# last modified 03/07/2000


# subdir from $SUPERVISE_DIR
PROG=pub-httpd

# space separated list of named programs to kill before starting
# services and after stopping them.
SVC_KILL=""

# if you want to wait for programs to exit before releasing kill_procs(),
# enter their names here (space separated list).
WAIT_PID=""

# directory under which supervised programs are set up
# SYS_TYPE must be either SYSV or BSD
# this is used only to see how to call ps(1).
# SYSV: solaris, irix
# BSD:  linux, *bsd, bsdi
SYS_TYPE=SYSV

# if you want the output of this program logged to syslog, enter the
# tag name here; otherwise, leave null, or use a single '.' to quell
# all output.
LOG_TAG=httpd

# numeric syslog facility of the output; null string means to use the
# splogger default.  (codes are usually in /usr/include/sys/syslog.h)
# quick notes:
#  LOG_KERN   0
#  LOG_USER   1
#  LOG_MAIL   2
#  LOG_DAEMON 3
#  LOG_AUTH   4
LOG_FAC=3

SUPERVISE_DIR="/var/supervise"
SPLOGGER="/var/qmail/bin/wrap_splogger"


####################################################################
### you probably don't need to change anything below this marker ###
####################################################################


PATH="/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/bin:/usr/local/sbin"
export PATH

KILL_SIGNAL=15

SPROG_DIR="$PROG/supervise"

getpid_BSD() {
	PID=`/bin/ps axc |awk '{print $1" "$5}' | \
		egrep " ${1}[ ]?\$" |awk '{print $1}'`
}

getpid_SYSV() {
	PID=`/bin/ps -e -o pid -o comm | \
		egrep " (/[^ ]*/)?${1}[ ]?\$" |awk '{print $1}'`
}

kill_procs() {
	for proc in $SVC_KILL; do
		eval getpid_$SYS_TYPE $proc

		if [ ! -z "$PID" ]; then
			# $PID is outside of quotes in case of multiple PIDs
			echo "killing $proc ("$PID")..."
			kill -$KILL_SIGNAL $PID
		fi

	done
	for proc in $WAIT_PID; do
		eval getpid_$SYS_TYPE $proc

		if [ ! -z "$PID" ]; then
			echo "waiting for $WAIT_PID ("$PID")..."
			for pid in $PID; do
				wait $pid
			done
		fi
	done
}

do_start() {
	if [ -f "$SPROG_DIR/lock" ]; then
		#echo "$SPROG_DIR/lock exists."
		STATUS=`svstat "$PROG"`
		SSTAT=`echo $STATUS |sed 's/not running//'`
		if [ "$SSTAT" = "$STATUS" ]; then
			# supervise is running
			echo "supervise is already running:  $STATUS"
			exit 1
		#else
			# supervise isn't actually running...
			#echo "$SPROG_DIR/lock is stale... starting anyway."
		fi
	fi

	kill_procs
	echo "starting $PROG supervise..."
	if [ ! -z "$LOG_TAG" ]; then
		if [ "$LOG_TAG" = "." ]; then
			csh -cf "supervise \"$PROG\" >& /dev/null &"
		else
			csh -cf "supervise \"$PROG\" |& $SPLOGGER $LOG_TAG $LOG_FAC &"
		fi
	else
		csh -cf "supervise \"$PROG\" &"
	fi
}

do_stop() {
	STATUS=`svstat "$PROG"`
	SSTAT=`echo $STATUS |sed 's/not running//'`
	if [ "$SSTAT" = "$STATUS" ]; then
		echo "status: $STATUS"
		# supervise is running
		echo "stopping $PROG supervise..."
		svc -dx "$PROG"
	else
		echo "$STATUS"
	fi

	# just in case...
	kill_procs
}

case "$1" in
start|stop|restart)
	cd "$SUPERVISE_DIR" || exit 1
	if [ ! -d "$PROG" ]; then
		echo "$PROG is not supervised."
		exit 1
	fi
	;;
*)
	echo "usage: $0 (start|stop|restart)"
	exit 1
	;;
esac


case "$1" in

start)
	do_start
	;;
stop)
	do_stop
	;;
restart)
	do_stop
	do_start
	;;
*)
	echo "this is odd... you shouldn't get here."
	exit 1
	;;
esac
exit 0
