#!/bin/bash
#
# $Cambridge: hermes/src/prayer/files/etc.hermes/rc.prayer,v 1.9 2008/09/16 09:59:56 dpc22 Exp $
#
### BEGIN INIT INFO
# Provides:       prayer
# Required-Start: $remote_fs $syslog $named $time
# Required-Stop:  $remote_fs $syslog
# Default-Start:  2 3 5
# Default-Stop:   0 1 6
# Description:    Prayer Webmail system
### END INIT INFO

# First reset status of this service
. /etc/rc.status
rc_reset

[ -f /opt/prayer/sbin/prayer ] || exit 0

RETVAL=0
prog="prayer"

start() {
	# Start daemons.

	echo -n $"Starting $prog: "

	# Remove SSL session cache file
	rm -f /var/spool/prayer/ssl_scache/*

	# "daemon" disables coredumps. For the time being, we _want_ prayer
	# to dump core so that we can see what is going wrong
        (ulimit -c unlimited; /opt/prayer/sbin/prayer $OPTS)
        # daemon /usr/local/prayer/sbin/prayer $OPTS
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/prayer
	return $RETVAL
}

stop() {
	# Stop daemons.
	echo -n $"Shutting down $prog: "
        # Shut down master process to stop prefork storm
	kill `cat /var/spool/prayer/pid/prayer-session.pid`
	kill `cat /var/spool/prayer/pid/prayer.pid`
	killproc prayer-session
	killproc prayer
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/prayer
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  graceful)
	# restart without breaking old sessions, per apache
	# first kill master servers
	kill `cat /var/spool/prayer/pid/prayer-session.pid`
	kill `cat /var/spool/prayer/pid/prayer.pid`
	# kill slave servers hanging off listening sockets
	ps axo pid,command |
		awk '/^ *[0-9]* prayer: / { print "kill " $1 }' |
		sh
	# Remove SSL session cache file
	rm -f /var/spool/prayer/ssl_scache/*

	# restart
        (ulimit -c unlimited; /opt/prayer/sbin/prayer $OPTS)
	RETVAL=$?
	;;
  restart|reload)
	stop
	start
	RETVAL=$?
	;;
  condrestart)
	if [ -f /var/lock/subsys/prayer ]; then
	    stop
	    start
	    RETVAL=$?
	fi
	;;
  status)
	status prayer
	RETVAL=$?
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|condrestart|status}"
	exit 1
esac

exit $RETVAL
