#! /bin/sh
#
# /etc/init.d/novell-zmd
#
### BEGIN INIT INFO
# Provides:                   novell-zmd
# Required-Start:             $network
# Should-Start:               novell-zislnx $remote_fs
# Required-Stop:              $null
# Should-Start:               novell-zislnx $remote_fs
# Default-Start:              3 4 5
# Default-Stop:               0 1 2 6
# Description: ZMD, the ZENworks Management Daemon, allows users to manage \
#              software on their systems. \
#              Visit http://www.novell.com for more information.
### END INIT INFO

# $Id: zmd.init.lsb.in 89090 2008-12-04 12:19:04Z akalyanasundaram $

# Source SuSE config
PATH=/sbin:/bin:/usr/sbin:/usr/bin
		
. /etc/rc.status

if [ -f /etc/sysconfig/zmd ]; then
  . /etc/sysconfig/zmd
else
  ZMD_OPTIONS=
fi

ZMD_BIN=/usr/sbin/zmd

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - misc error
# 2 - invalid or excess args
# 3 - unimplemented feature (e.g. reload)
# 4 - insufficient privilege
# 5 - program not installed
# 6 - program not configured
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

SLEEP_INTERVAL=2
MAX_SLEEP_TIME=10

#Setting the $HOME variable to be used by libredcarpet for default keyring
HOME=`grep "^root:" /etc/passwd|cut -d ":" -f 6`
if [ $HOME == "" ] ; then
HOME="/root"
fi

function check_pid_file()
{
    test -f /var/run/zmd.pid
}

function check_process()
{
    #lame
    if test `id -u` = "0"; then \
	kill -0 `cat /var/run/zmd.pid` 2> /dev/null;
    else \
        ps -p `cat /var/run/zmd.pid` > /dev/null
    fi;
}

function check_running()
{
    check_pid_file && check_process
}

case "$1" in
    start)
        if ! check_running; then \
            if [ -f /var/run/zmd/zmd.sleeping ] ; then \
            # Non-Sense. If you killed a sleeping ZMD using kill command, then zmd.sleeping
            # file would be present. Clean the garbage before starting.
                rm /var/run/zmd/zmd.sleeping
            fi
            echo -n "Starting ZENworks Management Daemon"
            $ZMD_BIN $ZMD_OPTIONS
        fi

        # Remember status and be verbose
        rc_status -v
        ;;
    stop)
        echo -n "Shutting down ZENworks Management Daemon"

        if check_running; then \
            if [ -f /var/run/zmd/zmd.sleeping ] ; then \
                # ZMD is sleeping. So Mono runtime is not up. Only the thin zmd-bin binary is running.
                # There is no risk of using the signals. So using kill -TERM in stead of rug restart
                # so that the sleeping zmd does not wake up before shutting down.
                kill -TERM `cat /var/run/zmd.pid`;
                rm /var/run/zmd/zmd.sleeping
            else \
                # Not using kill -TERM because using signals with Mono is not a good idea. 
                # In stead using rug shutdown.
                if [ -f /usr/bin/rug ] ; then \
                    /usr/bin/rug shutdown >/dev/null &
                elif [ -f /opt/novell/zenworks/bin/rug ] ; then \
                    /opt/novell/zenworks/bin/rug shutdown >/dev/null &
                fi
            fi
           
            total_slept=0 
            while check_running; do \
                
                sleep $SLEEP_INTERVAL
                total_slept=`expr $total_slept + 1`

                if [ "$total_slept" -gt "$MAX_SLEEP_TIME" ]; then \
                    rc_failed
                    break
                fi
            done
        fi

        # Remember status and be verbose
        rc_status -v
        ;;
    restart)
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
        $0 stop
        $0 start

        # Remember status and be quiet
        rc_status
        ;;
    reload)
        rc_failed 3
        rc_status -v
        ;;
    status)
        echo -n "Checking for ZENworks Management Daemon: "
        if ! check_pid_file; then \
            rc_failed 3
        elif ! check_process; then \
            rc_failed 1
        fi

        rc_status -v
        ;;
    force-reload)
        rc_failed 3
        rc_status -v
        ;;
    try-restart)
        echo -n "Restarting ZENworks Management Daemon: "

        if check_running; then \
            if [ -f /var/run/zmd/zmd.sleeping ] ; then \
                # ZMD is sleeping. Dont wake it up by calling rug restart.
                # In stead restart by calling init script. So that it kills zmd and starts again.
                $0 restart
            else \
                # kill -HUP `cat /var/run/zmd.pid`
                # Not using kill -HUP because using signals with Mono is not a good idea. 
                # In stead using rug restart.
                if [ -f /usr/bin/rug ] ; then \
                    /usr/bin/rug restart >/dev/null &
                elif [ -f /opt/novell/zenworks/bin/rug ] ; then \
                    /opt/novell/zenworks/bin/rug restart >/dev/null &
                fi
            fi
        fi

        rc_status -v
        ;;
    showpid)
        if check_running; then \
            echo `cat /var/run/zmd.pid`
        fi
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart|try-restart|showpid}"
        exit 1
        ;;
esac
rc_exit

