#!/usr/bin/env bash
#
# zenoss        This shell script takes care of starting and stopping zenmon.
#               
# chkconfig: 2345 80 30
# description: Zenoss is a monitoring program written by Zenoss, Inc.
# 


#############################################################################
# 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/
#############################################################################
# Attempt to set ZENHOME if it is not already set, or bogus
# by calculating it relative to the executing command
if [ -z "$ZENHOME" -o ! -d "$ZENHOME" ] ; then
    # Resolve links: $0 may be a link to zenoss's home.
    PRG="$0"
    while [ -h "$PRG" ] ; do
        ls=`ls -ld "$PRG"`
        link=`expr "$ls" : '.*-> \(.*\)$'`
        if expr "$link" : '/.*' > /dev/null; then
            PRG="$link"
        else
            PRG=`dirname "$PRG"`"/$link"
        fi
    done
    SAVED="`pwd`"
    cd "`dirname \"$PRG\"`/.."
    ZENHOME="`pwd -P`"
    export ZENHOME
    cd "$SAVED"
fi

if [ `id -u` -eq 0 ] ; then
   USERNAME=`ls -l $ZENHOME/var/Data.fs | awk '{ print $3 }'`
   if [ "$USERNAME" = "root" -o -z "$USERNAME" ]  ; then 
       echo Unable to determine the right user to run zenoss
       exit 1
   fi
   chown -R $USERNAME $ZENHOME/var
   chown -R $USERNAME $ZENHOME/log
   exec su - $USERNAME $0 $@
fi

. $ZENHOME/bin/zenfunctions
C=
if [ ! -f $ZENHOME/etc/DAEMONS_TXT_ONLY ] ; then
	C="$C zeoctl"
	C="$C zopectl"
	C="$C zenhub"
	C="$C zenjobs"
	C="$C zenping"
	C="$C zensyslog"
	C="$C zenstatus"
	C="$C zenactions"
	C="$C zentrap"
	C="$C zenmodeler"
	#C="$C zenrender"
	C="$C zenperfsnmp"
	C="$C zencommand"
	C="$C zenprocess"
	#C="$C zenmail"
	$ZENHOME/bin/python -c 'import pysamba' 2>/dev/null
	if [ $? -eq 0 ]
	then
	    C="$C zenwin"
	    C="$C zeneventlog"
	fi

	ZPD=`$ZENHOME/bin/zenpackdaemons --list`
	C="$C $ZPD"
fi
if [ -f "$ZENHOME/etc/daemons.txt" ]
then
    D=`cat $ZENHOME/etc/daemons.txt`
    C="$C $D"
fi

CMDS=$C

reverse() {
    args=
    for i
    do
       args="$i $args"
       shift
    done
    echo $args
}

FUNC=$CMD

issue() {
    # To properly conform to LSB standards and be used under Linux-HA or RHCS
    # we need to exit with a proper non-zero code when there is a problem with
    # the service. We'll take the naive approach of responding with the highest
    # and most likely worse condition of any of the configured daemons.
    HIGHESTCODE=0
    for cmd in $CMDS
    do
        echo -n "Daemon: $cmd "
        
        # Special status handling for zeoctl and zopectl because the stock
        # scripts exit with a code of 0 no matter what state they're in.
        if [ $FUNC == "status" -a \( $cmd == "zeoctl" -o $cmd == "zopectl" \) ]
        then
            PIDFILE="${ZENHOME}/var/Z2.pid"
            [ $cmd == 'zeoctl' ] && PIDFILE="${ZENHOME}/var/ZEO.pid"

            if running; then
                echo program running\; pid=$PID
                CODE=0
            else
                echo not running
                if [ -f $PIDFILE ]; then
                    rm -f $PIDFILE
                    CODE=1
                else
                    CODE=3
                fi
            fi
        else
            $ZENHOME/bin/$cmd "$FUNC"
            CODE=$?
        fi
        if [ $CODE -gt $HIGHESTCODE ]; then
            HIGHESTCODE=$CODE
        fi
    done
    return $HIGHESTCODE
}

EXITCODE=4

case "$FUNC" in
  start)
        issue
        EXITCODE=$?
        ;;
  stop)
        CMDS=`reverse $CMDS`
        issue
        EXITCODE=$?
	rm -f $ZENHOME/var/.*watchdog-*
        ;;
  restart)
        CMDS=`reverse $CMDS`
	FUNC=stop
	issue
        CMDS=`reverse $CMDS`
	FUNC=start
        issue
        EXITCODE=$?
        ;;
  status)
        issue
        EXITCODE=$?
        ;;
  list)
	for i in $CMDS
	do
	    echo $i
	    EXITCODE=0
	done
	;;
  *)
        echo $"Usage: $0 {start|stop|restart|status|list}"
        exit 1
esac
exit $EXITCODE
