#!/usr/bin/env bash
#
# panilux_agent        Startup script for Panilux Agent
#
# chkconfig: - 85 15
# processname: agent
# pidfile: /var/run/panilux_agent.pid
# description: Panilux Agent daemon
#
### BEGIN INIT INFO
# Provides: agent
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop agent
### END INIT INFO

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

prog_title='Panilux Agent'
prog_name=agent
prog_bin=/panilux/agent/agent
lockfile=/var/lock/subsys/panilux_agent
pidfile=/var/run/panilux_agent.pid
SLEEPMSEC=100000
RETVAL=0

start() {
    echo -n $"Starting $prog_title: "

    ${prog_bin} start >/dev/null 2>&1
    RETVAL=$?

    if [ ${RETVAL} = 0 ]; then
        touch ${lockfile}
        success $"$prog_title start"
    else
        failure $"$prog_title start"
    fi

    echo
    return ${RETVAL}
}

stop() {
    echo -n $"Stopping $prog_title: "

    ${prog_bin} stop >/dev/null 2>&1
    RETVAL=$?

    if [ ${RETVAL} = 0 ]; then
        rm -f ${lockfile} ${pidfile}
        success $"$prog_title stop"
    else
        failure $"$prog_title stop"
    fi

    echo
    return ${RETVAL}
}

reload() {
    echo -n $"Reloading $prog_title: "

    ${prog_bin} reload >/dev/null 2>&1
    RETVAL=$?

    if [ ${RETVAL} = 0 ]; then
        touch ${lockfile}
        success $"$prog_title reload"
    else
        failure $"$prog_title reload"
    fi

    echo
    return ${RETVAL}
}

rh_status() {
    ${prog_bin} status >/dev/null 2>&1
    return $?
}

case "$1" in
    start)
        rh_status >/dev/null 2>&1 && exit 0
        start
        ;;
    stop)
        stop
        ;;
    status)
        rh_status
        RETVAL=$?
        ;;
    restart)
        stop
        start
        ;;
    condrestart|try-restart)
        if rh_status >/dev/null 2>&1; then
            stop
            start
        fi
        ;;
    force-reload|reload)
        reload
        ;;
    *)

    echo $"Usage: service panilux_agent {start|stop|restart|condrestart|try-restart|force-reload|reload|status|help}"
    RETVAL=2
esac

exit ${RETVAL}
exit