#############################################################################
# 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/
#############################################################################

INSTANCE_HOME=$ZENHOME
export INSTANCE_HOME

SYBASE=$ZENHOME/sybase
export SYBASE

ZOPEHOME=$ZENHOME
if [ -d /usr/lib/zope ] ; then
   ZOPEHOME=/usr/lib/zope
fi

PYTHON=$ZOPEHOME/bin/python
PYTHONPATH=$ZENHOME/lib/python:$ZOPEHOME/lib/python
export PYTHONPATH

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

ZENPRODUCTS="ZenModel ZenHub ZenReports ZenWin ZenUtils SiteScopeParser ZenRelations ZenStatus SnmpCollector 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

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

EXEC="exec $SUDO"

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

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

stop() {
    if running; then
        PID=`cat $PIDFILE`
        echo stopping...
        kill $PID
        if [ $? -gt 0 ]; then
            rm -f $PIDFILE
            echo clearing pid file
        fi
    else
        echo already stopped
    fi
}

restart() {
    stop
    for i in 1 2 3 4 5 6 7 8 9 10
    do
       sleep 0.24 2>/dev/null || sleep 1
       test -f $PIDFILE || break
    done
    start "$@"
}

status() {
    if running; then
            echo program running\; pid=$PID
            exit 109
    else
        rm -f $PIDFILE
        echo not running
        exit 100
    fi
}

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


getPort()
{
   PORT=$1 ; shift
   name=$1 ; shift
   while [ $# -ge 1 ]; do
       eval "
       case $1 in
	   $name=*) PORT=`echo $1 | sed -e \"s/$name=//\"` ;;
	   $name) PORT=$2; shift;;
       esac
       "
       shift
   done
   echo $PORT
}

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


generic() {
    case "$CMD" in
      run)
	    run "$@"
	    ;;
      start)
	    start "$@"
	    ;;
      stop)
	    stop
	    ;;
      restart)
	    restart "$@"
	    ;;
      status)
	    status
	    ;;
      help)
	    help
	    ;;
      genconf)
	    genconf
	    ;;
      *)
	    cat - <<HELP
Usage: $0 {run|start|stop|restart|status|help|genconf} [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 

HELP
	    exit 1
    esac
    exit $?
}

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