#!/bin/sh
#
# zope          Start/Stop the Zope web-application server.
#
# chkconfig: 2345 72 72
# description: zope is a web server specifically for handling \
#              HTTP requests to the Zope web-application service.
# probe: true

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

# Extracted from 'functions' to augment it to obtain Zope's
# watchdog and server PIDs from the ${INSTANCE_HOME}/var/Z2.pid
# file, where both the parent and child processes are recorded
# in the _same_ file. <sigh>

# A function to find the pid of a program.
pidofproc() {
	# Test syntax.
	if [ $# = 0 ] ; then
		echo "Usage: pidofproc {program}"
		return 1
	fi

	# First try the "$INSTANCE_HOME/var/Z2.pid" file
	if [ -f ${INSTANCE_HOME}/var/Z2.pid ] ; then
	        if [ "$1" = "zwatchdog" ] ; then
	                pid=`sed -e 's/^\([0-9]\+\) [0-9]\+/\1/' ${INSTANCE_HOME}/var/Z2.pid`
		else
		        if [ "$1" = "zserver" ] ; then
		                pid=`sed -e 's/^[0-9]\+ \([0-9]\+\)/\1/' ${INSTANCE_HOME}/var/Z2.pid`
			fi
	        fi

		if [ "$pid" != "" ] ; then
			echo $pid
			return 0
		fi
	fi

	# Next try "/var/run/*.pid" files
	if [ -f /var/run/$1.pid ] ; then
	        pid=`head -1 /var/run/$1.pid`
	        if [ "$pid" != "" ] ; then
	                echo $pid
	                return 0
	        fi
	fi

	# Next try "pidof"
	pid=`pidof -o $$ -o $PPID -o %PPID -x $1`
	if [ "$pid" != "" ] ; then
	        echo $pid
	        return 0
	fi
}

# Extracted from 'functions' to fix a tiny bug where it uses 'pidof'
# but should be using 'pidofproc'.
status() {
	# Test syntax.
	if [ $# = 0 ] ; then
		echo "Usage: status {program}"
		return 1
	fi

	# First try "pidofproc"
	pid=`pidofproc $1`
	if [ "$pid" != "" ] && ps h $pid >/dev/null 2>&1 ; then
	        echo "$1 (pid $pid) is running..."
	        return 0
        else
		pid=`pidof -o $$ -o $PPID -o %PPID -x $1`
		if [ "$pid" != "" ] ; then
		        echo "$1 (pid $pid) is running..."
		        return 0
		fi
	fi

	# Next try "/var/run/*.pid" files
	if [ -f /var/run/$1.pid ] ; then
	        pid=`head -1 /var/run/$1.pid`
	        if [ "$pid" != "" ] ; then
	                echo "$1 dead but pid file exists"
	                return 1
	        fi
	fi

	# See if /var/lock/subsys/$1 exists
	if [ -f /var/lock/subsys/$1 ]; then
		echo "$1 dead but subsys locked"
		return 2
	fi
	echo "$1 is stopped"
	return 3
}

INSTANCE_HOME=/usr/local/zope
INSTANCE_NAME=`basename ${INSTANCE_HOME}`

# make sure starter script exists
[ -f ${INSTANCE_HOME}/start ] || exit 0

RETVAL=0

# See how we were called.
case "$1" in
  start)
	echo -n "Starting zope: "
  	cd ${INSTANCE_HOME}

	# See if it's already running.
	pid=`pidofproc zwatchdog`
	[ -n "$pid" ] && ps h $pid >/dev/null 2>&1 && echo && exit $RETVAL

	pid=`pidofproc zserver`
	[ -n "$pid" ] && ps h $pid >/dev/null 2>&1 && echo && exit $RETVAL

	rm -f ${INSTANCE_HOME}/var/Z2.pid
	daemon ${INSTANCE_HOME}/start &
	RETVAL=$?
  	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/${INSTANCE_NAME}
  	;;
  stop)
	echo -n "Shutting down zope: "
	killproc zwatchdog
	killproc zserver
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/${INSTANCE_NAME} ${INSTANCE_HOME}/var/Z2.pid
	;;
  restart|reload)
	$0 stop
	$0 start
	RETVAL=$?
	;;
  status)
	status zwatchdog
	[ $RETVAL -ne 0 ] && RETVAL=$?
	status zserver
	[ $RETVAL -ne 0 ] && RETVAL=$?
	;;
  probe)
	# echo command to start/restart zope, only if NOT already running...
	# (this is a linuxconf convention, using the probe keyword above)
	if [ ! -f /var/lock/subsys/${INSTANCE_NAME} ] ; then
		echo start; exit 0
	fi

	pid=`pidofproc zwatchdog`
	[ -n "$pid" ] && ps h $pid >/dev/null 2>&1 && exit $RETVAL

	pid=`pidofproc zserver`
	[ -n "$pid" ] && ps h $pid >/dev/null 2>&1 && exit $RETVAL

	echo restart; exit 0
	;;
  *)
	echo "Usage: zope {start|stop|restart|status}"
	exit 1
esac

exit $RETVAL
