#!/bin/bash
#chkconfig: 2345 80 05
#description: zagent Daemon
# Check for missing binaries
BAR_BIN=/usr/local/bin/zcloudagent
pidfile=/var/lock/subsys/zcloudagent
RETVAL=0
. /etc/rc.d/init.d/functions
genconfig()
{
test -x $BAR_BIN || { echo "$BAR_BIN not installed";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 5; fi; }

# Check for existence of needed config file and read it
BAR_CONFIG=/etc/zcloudagent.conf
test -r $BAR_CONFIG || { echo "$BAR_CONFIG not existing";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 6; fi; }
}
start()
{
        echo -n $"Starting zCloud Agent: "
        daemon $BAR_BIN
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch $pidfile
        return $RETVAL
}

stop()
{
        echo -n $"Shutting down zCloud Agent: "
        killproc $BAR_BIN
        RETVAL=$?
        echo
        rm -f $pidfile
        return $RETVAL
}

case "$1" in
        start)
                genconfig
                start
                ;;
         stop)
                stop
                ;;
         restart)
                stop
                genconfig
                start
                ;;
         status)
                status $BAR_BIN
                RETVAL=$?
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart|status}"
                RETVAL=3
esac
