#############################################################################
# This program is part of Zenoss Core, an open source monitoring platform.
# Copyright (C) 2007, Zenoss Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published by
# the Free Software Foundation.
#
# For complete information please visit: http://www.zenoss.com/oss/
#############################################################################

# Bitrock installer sets the following variables properly for the stack
# in setenv.sh.  Avoid undoing that goodness.
# NB: This should probably be refactored per MT's idea of generalizing
# setenv.sh for all artifacts.

if [ -z "$ZENOSS_BITROCK_STACK" ];then
   INSTANCE_HOME=$ZENHOME
   export INSTANCE_HOME
   if [ -d /usr/lib/zope ] ; then
      # appliance
      ZOPEHOME=/usr/lib/zope
      PYTHONPATH=$ZENHOME/lib/python:$ZOPEHOME/lib/python
   else
      # rpm / source build
      ZOPEHOME=$ZENHOME
      PYTHONPATH=$ZENHOME/lib/python
   fi
   export PYTHONPATH
fi
PYTHON=$ZENHOME/bin/python
SYBASE=$ZENHOME/sybase
export SYBASE

VARDIR=$ZENHOME/var
CFGDIR=$ZENHOME/etc

ZENPRODUCTS="ZenModel ZenHub ZenReports ZenWin ZenUtils  ZenRelations ZenStatus ZenRRD DataCollector ZenWidgets ZenEvents"

SYS=`uname`
PS="ps -eo pid"

if [ $SYS = "Darwin" -o $SYS = "FreeBSD" ]; then
    PS="ps -axo pid"
fi

CMD=$1
shift

pidFile() {
   if [ -z "$PIDFILE" ] ; then
       MONITOR=`grep '^[ 	]*monitor' $1 2>/dev/null | tr -d '\r' | tail -1 | awk '{ print $2 }'`
       if [ -z "$MONITOR" ] ; then MONITOR=localhost ; fi
       PIDFILE="$VARDIR/`basename $PRGNAME .py`-$MONITOR.pid"
   fi
   echo "$PIDFILE"
}

running() {
    PIDFILE=`pidFile $CFGFILE`
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE 2>/dev/null`
	kill -0 $PID 2>/dev/null || $PS | grep -q "^ *$PID$"
	return $?
    fi
    return 1
}

start() {
    PIDFILE=`pidFile $CFGFILE`
    if running; then    
        echo is already running
    else
        echo starting...
        if [ "$CFGFILE" = "" ]; then
            exec $PYTHON $PRGHOME/$PRGNAME --cycle --daemon "$@"
        else
            exec $PYTHON $PRGHOME/$PRGNAME --configfile $CFGFILE \
                --cycle --daemon "$@"
        fi
    fi
}

run() {
    if [ "$CFGFILE" = "" ]; then
        exec $PYTHON $PRGHOME/$PRGNAME "$@"
    else        
        exec $PYTHON $PRGHOME/$PRGNAME --configfile $CFGFILE "$@"
    fi
}

stop() {
    PIDFILE=`pidFile $CFGFILE`
    WATCHDOGPID=`dirname $PIDFILE`/watchdog-`basename $PIDFILE`
    KILLED=1
    echo stopping...
    # first, kill any watchdog
    if [ -f $WATCHDOGPID ] ; then
	kill -TERM `cat $WATCHDOGPID`
	KILLED=$?
	rm -f $WATCHDOGPID
    fi
    if running; then
        # kill the daemon
        PID=`cat $PIDFILE 2>/dev/null`
	if [ x"$PID" != x ] ; then
	    kill -TERM $PID
	    if [ $? -gt 0 ]; then
		rm -f $PIDFILE
		echo clearing pid file
	    fi
        fi
    else
	# probably stopped by the watchdog
        if [ "$KILLED" -ne 0 ] ; then
	   echo already stopped
        fi
    fi
}

restart() {
    PIDFILE=`pidFile $CFGFILE`
    stop
    for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    do
       sleep 0.24 2>/dev/null || sleep 1
       test -f $PIDFILE || break
    done
    start "$@"
}

status() {
    PIDFILE=`pidFile $CFGFILE`
    if running; then
            echo program running\; pid=$PID
            exit 0
    else
        echo not running
        if [ -f $PIDFILE ]; then
            rm -f $PIDFILE
            exit 1
        else
            exit 3
        fi
    fi
}

help() {
    exec $PYTHON $PRGHOME/$PRGNAME -h
}


genconf() {
    #
    # Create a suitable default configuration file
    #
    exec $PYTHON $PRGHOME/$PRGNAME --genconf
}

genxmlconfigs() {
    #
    # Create a suitable default configuration file
    #
    exec $PYTHON $PRGHOME/$PRGNAME --genxmlconfigs
}

debug() {
    PIDFILE=`pidFile $CFGFILE`
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE 2>/dev/null`
        echo "Sending SIGUSR1 to $PID"
        kill -s USR1 $PID 2>/dev/null || $PS | grep -q "^ *$PID$"
        return $?
    else
        echo "Unable to find process to send SIGUSR1 signal"
    fi

}

stats() {
    PIDFILE=`pidFile $CFGFILE`
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE 2>/dev/null`
        echo "Sending SIGUSR2 to $PID"
        kill -s USR2 $PID 2>/dev/null || $PS | grep -q "^ *$PID$"
        return $?
    else
        echo "Unable to find process to send SIGUSR2 signal"
    fi

}

generic() {
    case "$CMD" in
      run)
	    run "$@"
	    ;;
      start)
	    start "$@"
	    ;;
      stop)
	    stop
	    ;;
      restart)
	    restart "$@"
	    ;;
      status)
	    status
	    ;;
      help)
	    help
	    ;;
      genconf)
	    genconf
	    ;;
      genxmlconfigs)
	    genxmlconfigs
	    ;;
      debug)
	    debug "$@"
	    ;;
      stats)
            stats "$@"
            ;;
      *)
	    cat - <<HELP
Usage: $0 {run|start|stop|restart|status|help|genconf|genxmlconfigs|debug|stats} [options]

  where the commands are:

    run     - start the program but don't put it in the background.
              NB: This mode is good for debugging.

    start   - start the program in daemon mode -- running in the background,
              detached from the shell

    stop    - stop the program

    restart - stop and then start the program
              NB: Sometimes the start command will run before the daemon
                  has terminated.  If this happens just re-run the command.

    status  - Check the status of a daemon.  This will print the current
              process nuber if it is running.

    help    - display the options available for the daemon

    genconf - create an example configuration file with default settings 

    genxmlconfigs - create an XML file with default settings

    debug   - toggle the logging of daemons between Debug level and the default

    stats   - display detailed statistics of the deamon

HELP
	    exit 1
    esac
    exit $?
}

notdaemon() {
    case "$CMD" in
      run)
	    run "$@"
	    ;;
      start)
	    run "$@"
	    ;;
      help)
	    help
	    ;;
      *)
	    echo $"Usage: $0 {run|help}"
	    exit 1
    esac
    exit $?
}

