#! /bin/sh
### BEGIN INIT INFO
# Provides:          autoscale-agent
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start autoscale-agent daemon
### END INIT INFO

set -e

NAME=autoscale_agentd
DAEMON=/onapp/autoscale/autoscale_agentd
DESC="Autoscale agent"
CONF=/onapp/autoscale/autoscale_agentd.conf

test -x $DAEMON || exit 0

DIR=/onapp/autoscale/
PID=/onapp/autoscale/pidfile # $(grep -e "^PidFile=.*$" /onapp/autoscale/autoscale_agentd.conf | cut -d= -f2 | tr -d '\r')
RETRY=15

if test ! -d "$DIR"; then
  mkdir "$DIR"
  chown -R autoscale:autoscale "$DIR"
fi

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin:/onapp/autoscale/"

# define LSB log_* functions.
. /lib/lsb/init-functions

case "$1" in
  start)
    log_daemon_msg "Starting $DESC" "$NAME"
  start-stop-daemon --oknodo --start --user autoscale --chuid autoscale --pidfile $PID --exec $DAEMON -- --config $CONF 2>&1
    case "$?" in
        0) log_end_msg 0 ;;
        *) log_end_msg 1; exit 1 ;;
    esac
  ;;
  stop)
    log_daemon_msg "Stopping $DESC" "$NAME"
  start-stop-daemon --oknodo --stop --user autoscale --chuid autoscale --pidfile $PID --exec $DAEMON --retry $RETRY -- --config $CONF
    case "$?" in
        0) log_end_msg 0 ;;
        *) log_end_msg 1; exit 1 ;;
    esac
  ;;
  status)
   status_of_proc -p $PID $DAEMON $NAME && exit 0 || exit $?
   ;;
  restart|force-reload)
  $0 stop
  $0 start
  ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}" >&2
  exit 1
  ;;
esac

exit 0