#!/bin/sh
#
# chkconfig: - 86 14
# description: Autoscale agent daemon
# processname: autoscale_agentd
# config: /onapp/autoscale/autoscale_agentd.conf
# user: autoscale
#

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

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

if [ -x /onapp/autoscale/autoscale_agentd ]; then
    exec=/onapp/autoscale/autoscale_agentd
else
    exit 5
fi

prog=${exec##*/}
conf=/onapp/autoscale/autoscale_agentd.conf
pidfile=$(grep -e "^PidFile=.*$" $conf | cut -d= -f2 | tr -d '\r')
timeout=10

lockfile=/onapp/autoscale/lockfile

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

stop()
{
    echo -n $"Shutting down Autoscale 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
