#! /bin/sh
#
# /etc/init.d/novell-zmd
#
### BEGIN INIT INFO
# Provides:                   novell-zmd
# Required-Start:             $network
# X-UnitedLinux-Should-Start: novell-zislnx $remote_fs
# Required-Stop:
# X-UnitedLinux-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 29585 2006-06-01 19:01:37Z james $

# 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

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 \
            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 \
            kill -TERM `cat /var/run/zmd.pid`;
           
            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 \
            kill -HUP `cat /var/run/zmd.pid`
        else
            $0 restart
        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

