#!/bin/bash
#
# chkconfig: 345 90 90
# description: Start or stop the zope daemon
# processname: zope
#
# /etc/rc.d/init.d/zope
#
# by cyrille@ktaland.com 2001-03-15
#

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

# Variables
zopePath='/usr/local/Zope'

# Execute
case "$1" in
    start)
        [ -f ${zopePath}/var/Z2.pid ] && kill -0 `cat ${zopePath}/var/Z2.pid` >/dev/null 2>&1
        if [ -f ${zopePath}/var/Z2.pid -a "$?" != "0" ] ; then
            echo "Bad stop of zope daemon..."
            echo -n "Restoring zope file:"

            rm -f ${zopePath}/var/Z2.pid
            rm -f ${zopePath}/var/pcgi.soc
            rm -f ${zopePath}/var/Data.fs.lock
            rm -f ${zopePath}/var/zProcessManager.pid

            echo_success
            echo
        fi

        if [ ! -f ${zopePath}/var/Z2.pid ] ; then
            echo -n "Starting zope daemon: "
            #${zopePath}/start &> /dev/null
            # 2002-10-18 mad@ktaland.com
	    ###    ${zopePath}/start
            su --command=${zopePath}/start zope &> ${zopePath}/var/start-init.log &

            while : ; do [ -f ${zopePath}/var/Z2.pid ] && break
                sleep 1 && echo -n '.'
            done

            cat ${zopePath}/var/Z2.pid > /var/run/zope_$zopeName.pid
            touch /var/run/zope_$zopeName.pid

            echo_success
            echo
        else
            $0 status
        fi
    ;;
    stop)
        if [ -f ${zopePath}/var/Z2.pid ]; then
            echo -n "Stopping zope daemon: "
            # 2002-10-18 mad@ktaland.com
            #[ -f ${zopePath}/var/Z2.pid ] && kill `cat ${zopePath}/var/Z2.pid`
            su --command=${zopePath}/stop zope
            rm -f ${zopePath}/var/Z2.pid
            rm -f ${zopePath}/var/pcgi.soc
            rm -f ${zopePath}/var/Data.fs.lock
            rm -f ${zopePath}/var/zProcessManager.pid

            echo_success
            echo
        else
            echo "Zope is not running..."
        fi
    ;;
    reload|restart)
        $0 stop
        $0 start
    ;;
    status)
        if [ -f ${zopePath}/var/Z2.pid ]; then

            cat ${zopePath}/var/Z2.pid > /var/run/zope.pid
            touch /var/run/zope.pid

            echo "Zope (pid `head -1 /var/run/zope.pid`) is running..."
        else
            echo "Zope is not running..."
        fi
    ;;
    *)
        echo "Usage: /etc/rc.d/init.d/zope {start|stop|restart|reload|status}"
    exit 1
esac

exit 0
