#!/bin/sh

### BEGIN INIT INFO
# Provides:          {{.Common.Identifier}}
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
### END INIT INFO

set -e
NAME="{{.Common.Identifier}}"
DAEMON="{{.Common.Path}}"
DAEMON_OPTS="{{ StringsJoin .Common.Flags " \\\n" }}"

{{- range $key, $value := .Common.Environment }}
{{$key}}={{$value}}
export {{$key}}
{{- end }}

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

is_running() {
    start-stop-daemon --status --exec $DAEMON
}
case "$1" in
  start)
        echo "Starting daemon: "$NAME
        start-stop-daemon --start --quiet --background --exec $DAEMON -- $DAEMON_OPTS
        ;;
  stop)
        echo "Stopping daemon: "$NAME
        start-stop-daemon --stop --quiet --oknodo --exec $DAEMON
        ;;
  restart)
        echo "Restarting daemon: "$NAME
        start-stop-daemon --stop --quiet --oknodo --retry 30 --exec $DAEMON
        start-stop-daemon --start --quiet --background --exec $DAEMON -- $DAEMON_OPTS
        ;;
  status)
    if is_running; then
        echo "Running"
    else
        echo "Stopped"
        exit 1
    fi
    ;;
  *)
        echo "Usage: "$1" {start|stop|restart|status}"
        exit 1
esac

exit 0
