#!/bin/sh
#
# chkconfig: - 86 14
# description: perseusZ agent 2 daemon
# processname: perseusZ_agent2
# config: /usr/local/lwops/etc/lwops_agentd.conf
#

### BEGIN INIT INFO
# Provides: perseusZ-agent2
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Should-Start: perseusZ perseusZ-proxy
# Should-Stop: perseusZ perseusZ-proxy
# Default-Start:
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: Start and stop perseusZ agent 2
# Description: perseusZ agent 2
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

if [ -x /usr/local/lwops/sbin/lwops_agentd ]; then
    exec=/usr/local/lwops/sbin/lwops_agentd
else
    exit 5
fi

prog=${exec##*/}
conf=/usr/local/lwops/etc/lwops_agentd.conf
pidfile=/usr/local/lwops/logs/lwops_agentd.pid
timeout=10
perseusZ_AGENT_USER=
if [ -f /etc/sysconfig/perseusZ-agent2 ]; then
    . /etc/sysconfig/perseusZ-agent2
fi

if [ -n "$perseusZ_AGENT_USER" ]; then
    user_conf="--user=$perseusZ_AGENT_USER"
else
    user_conf=
fi

lockfile=/var/lock/subsys/lwops-agentd

start()
{
    echo -n $"Starting lwops agent : "
    daemon $user_conf $exec -c $conf &
    rv=$?
    echo
    [ $rv -eq 0 ] && touch $lockfile
    return $rv
}

stop()
{
    echo -n $"Shutting down lwops agent : "
    killproc -p $pidfile -d $timeout $prog
    rv=$?
    echo
    [ $rv -eq 0 ] && rm -f $lockfile
    return $rv
}

restart()
{
    stop
    start
}

case "$1" in
    start|stop|restart)
        $1
        ;;
    force-reload)
        restart
        ;;
    status)
        status -p $pidfile $prog 
        ;;
    try-restart|condrestart)
        if status $prog >/dev/null ; then
            restart
        fi
        ;;
    reload)
        action $"Service ${0##*/} does not support the reload action: " /bin/false
        exit 3
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"
        exit 2
        ;;
esac