#!/bin/sh
#
# /usr/sbin/zopectl
#
# Written by Gregor Hoffleit <flight@debian.org> for the Debian Zope package.
#
# License: Feel free to use, share and modify this code as you like.
#
#
# This is a comfortable replacement for Zope's start and stop scripts
# It let's you start, stop, restart and examine a Zope process managed
# by a zdaemon.
#

#
# You might want to modify the following settings (the defaults are
# valid for Debian's Zope installation):
#
ZOPE_HOME=/usr/lib/zope
INSTANCE_HOME=/var/lib/zope

if [ "$1" = "-I" ]
then
    shift
    if [ -z "$1" ]; then
        echo >&1 "usage: zopectl [ -I <instance home> ]"
        exit 1
    fi
    INSTANCE_HOME="$1"
    shift
fi

zdaemon_pidfile=$INSTANCE_HOME/var/zProcessManager.pid
zdaemon_logfile=$INSTANCE_HOME/var/Z2_debug.log
zope_pidfile=$INSTANCE_HOME/var/Z2.pid
zope_logfile=$INSTANCE_HOME/var/Z2.log

OPTION=$1
shift


export INSTANCE_HOME

test_zdaemon() {
    if [ -f $zdaemon_pidfile ]; then
        zdaemon_pid=`cat $zdaemon_pidfile`
	if [ ! "x$zdaemon_pid" = "x" ] && kill -0 $zdaemon_pid 2>/dev/null; then
	    zdaemon=1
	    true
	else
	    zdaemon=-1
	    rm -f $zdaemon_pidfile
	    false
	fi
    else
	zdaemon=0
	false
    fi
}

test_zope() {
    if [ -f $zope_pidfile ]; then
        zope_pid=`cat $zope_pidfile | cut -d' ' -f2`
	if [ ! "x$zope_pid" = "x" ] && kill -0 $zope_pid 2>/dev/null; then
	    zope=1
	    true
	else
	    zope=-1
	    false
	fi
    else
        zope=0
	false
    fi
}

waitfor_zope() {
    timeout=60
    test_zdaemon
    test_zope
    while [ $timeout -gt 0 -a ! $zdaemon -eq 1 ]; do
        sleep 1
        echo -n '.'
        test_zdaemon
        timeout=`expr $timeout - 1`
    done
    if [ $zdaemon -eq 1 ]; then
	test_zope
	while [ $timeout -gt 0 -a ! $zope -eq 1 ]; do
	    sleep 1
	    echo -n '.'
	    test_zope
	    timeout=`expr $timeout - 1`
	done
    fi
    if [ $zope -eq 1 ]; then
        true
    else
        false
    fi
}

start_zdaemon() {
#    /usr/sbin/zope-z2 -D $@ \
#            >>$zdaemon_logfile 2>>$zdaemon_logfile &
    /usr/sbin/zope-z2 $@
}


stop_zdaemon() {
    test_zdaemon
    if [ $zdaemon -eq 1 ]; then
        kill $zdaemon_pid 2>/dev/null && true
        test_zdaemon
        if [ $zdaemon -eq -1 ]; then
            rm -f $zdaemon_pidfile
        fi
    else
        if [ $zdaemon -eq -1 ]; then
            rm -f $zdaemon_pidfile
	    echo "Removed stale zdaemon pidfile (pid $zdaemon_pid)."
        fi
    fi
}

stop_zope() {
    test_zope
    if [ $zope -eq 1 ]; then
        kill $zope_pid 2>/dev/null && true
        test_zope
        if [ $zope -eq -1 ]; then
            rm -f $zope_pidfile
        fi
    else
        if [ $zope -eq -1 ]; then
            rm -f $zope_pidfile
	    echo "Removed stale z2 pidfile (pid $zope_pid)."
        fi
    fi
}



case $OPTION in

status)
    test_zdaemon
    test_zope
    if [ $zdaemon -eq 1 ]; then
        if [ $zope -eq 1 ]; then
            echo "Zope running (zdaemon pid=$zdaemon_pid, z2 pid=$zope_pid)."
	else
	    echo "Zope (re)starting (zdaemon pid=$zdaemon_pid, no z2)."
	fi
    else
        if [ $zope -eq 1 ]; then
	    echo "Problem: Found z2 process, but no zdaemon (z2 pid=$zope_pid, no zdaemon). Please stop Zope and restart."
	else
	    echo "Zope not running."
	fi
    fi
    ;;

start)
    test_zdaemon
    test_zope
    echo -n "Starting Zope..."
    if [ ! $zdaemon -eq 1 ] && [ ! $zope -eq 1 ]; then
        start_zdaemon $@
        if waitfor_zope; then
            echo " done."
        else
            echo " failed."
            false
        fi
    else
        if [ $zdaemon -eq 1 ] && [ $zope -eq 1 ]; then
	    echo " done (Zope already running)."
	else
	    if [ $zdaemon -eq 1 ]; then
	        echo " Zope is currently restarting."
	    else
	        echo "Only z2 running! This should not happen. Please stop and restart Zope!"
	    fi
	    false
	fi
    fi

    ;;

stop)
    echo -n "Stopping Zope..."
    test_zdaemon
    test_zope
    if [ $zdaemon -eq 0 ] && [ $zdaemon -eq 0 ]; then
        echo " done (Zope was not running)."
	not_running=1
    else
        not_running=0
    fi
    if [ $zdaemon -eq 1 ]; then
        stop_zdaemon
	#echo -n " [zdaemon process killed (pid $zdaemon_pid)]"
    else
        if [ $zdaemon -eq -1 ]; then
	    rm -f $zdaemon_pidfile
	    echo -n " [removed stale zdaemon pidfile (pid $zdaemon_pid)]"
	fi
    fi
    test_zope
    if [ $zope -eq 1 ]; then
        stop_zope
	#echo -n " [z2 process killed (pid $zope_pid)]"
    else
        if [ $zope -eq -1 ]; then
	    rm -f $zope_pidfile
	    echo -n " [removed stale z2 pidfile (pid $zope_pid)]"
	fi
    fi
    sleep 2
    if [ $not_running -eq 0 ]; then
        echo " done."
    fi
    ;;

restart)
    test_zdaemon
    test_zope
    if [ ! $zdaemon -eq 1 ] && [ ! $zope -eq 1 ]; then
	 echo -n "Starting Zope..."
         start_zdaemon
         waitfor_zope
         echo " done."
    else
        if [ $zdaemon -eq 1 ] && [ $zope -eq 1 ]; then
	    echo -n "Restarting Zope..."
	    stop_zope
            sleep 3
            waitfor_zope
            echo " done."
	else
	    echo "Zope is currently restarting (or something's very strange)."
	fi
    fi
    ;;

force-reload)
    /usr/sbin/zopectl stop || true
    /usr/sbin/zopectl start $@ || true
    ;;

*)
    echo "Usage: $0 (start|stop|restart|force-reload|status)"
    ;;

esac
