#!/bin/sh

### BEGIN INIT INFO
# Provides:          olad
# Required-Start:    $remote_fs $syslog $network
# Required-Stop:     $remote_fs $syslog $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: OLA daemon
# Description:       Open Lighting Architecture daemon
### END INIT INFO

PATH=/usr/local/bin:/bin:/usr/bin
NAME=olad
DAEMON=/usr/bin/$NAME
PIDFILE=/var/run/$NAME.pid
DESC="OLA daemon"
USER=olad
LOG_LEVEL=3
CONFIG_DIR="/var/lib/ola/conf"

# Reads config file (will override defaults above)
[ -r /etc/default/ola ] && . /etc/default/ola

if [ "$RUN_DAEMON" = "true" ] || [ "$RUN_DAEMON" = "yes" ] ; then
  DAEMON_ARGS="--syslog --log-level $LOG_LEVEL --config-dir $CONFIG_DIR"
elif [ "$1" = "start" ] || [ "$1" = "stop" ] ; then
  echo "The init script is currently inactive;\nuse \"dpkg-reconfigure ola\" to change this." >&2
fi

[ -x "$DAEMON" ] || exit 0

. /lib/lsb/init-functions

case "$1" in
  start)
    # master switch
    if [ -n "$DAEMON_ARGS" ] ; then
      log_daemon_msg "Starting $DESC" "$NAME"
      /sbin/start-stop-daemon --start --background --make-pidfile --pidfile $PIDFILE --umask 0002 --chuid $USER --exec $DAEMON -- $DAEMON_ARGS
      log_end_msg $?
    fi
    ;;
  stop)
    # master switch
    if [ "$RUN_DAEMON" = "true" ] || [ "$RUN_DAEMON" = "yes" ] ; then
      log_daemon_msg "Stopping $DESC" "$NAME"
      /sbin/start-stop-daemon --stop --pidfile $PIDFILE --chuid $USER --exec $DAEMON --retry 10
      /bin/rm -f $PIDFILE
      log_end_msg $?
    fi
    ;;
  reload|force-reload|restart)
    $0 stop && $0 start
    ;;
  status)
    status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
    ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|reload|restart|force-reload|status}" >&2
    exit 1
    ;;
esac

exit 0
