#!/bin/bash
### BEGIN INIT INFO
#
# Provides:		dump1090-fa
# Required-Start:	$remote_fs
# Required-Stop:	$remote_fs
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	dump1090-fa initscript
#
### END INIT INFO

. /lib/lsb/init-functions

DAEMON=/usr/bin/dump1090-fa
PIDFILE=/run/dump1090-fa.pid
WORKDIR=/run/dump1090-fa
RUNAS=dump1090

test -x $DAEMON || exit 5

. /etc/default/dump1090-fa

if [ -f /var/cache/piaware/location.env ]
then
    . /var/cache/piaware/location.env
fi

PROG_ARGS="$RECEIVER_OPTIONS $DECODER_OPTIONS $NET_OPTIONS $JSON_OPTIONS $PIAWARE_DUMP1090_LOCATION_OPTIONS \
 --write-json /run/dump1090-fa --quiet"

start() {
    log_daemon_msg "Starting dump1090-fa server" "dump1090-fa"
    mkdir -p $WORKDIR
    chown $RUNAS $WORKDIR
    /sbin/start-stop-daemon --start --oknodo --background --make-pidfile --pidfile $PIDFILE --user $RUNAS --chuid $RUNAS --exec $DAEMON -- $PROG_ARGS
    status=$?
    log_begin_msg $status
    return
}

stop() {
    log_daemon_msg "Stopping dump1090-fa server" "dump1090-fa"
    /sbin/start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE --user $RUNAS
    log_end_msg $?
    rm -f $PIDFILE
    return
}

status() {
    echo "no status yet"
    return
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
	status
        ;;
    restart|force-reload)
        stop && sleep 2 && start
        ;;
    reload)
	exit 3
	;;
    status)
	status_of_proc $DAEMON "dump1090-fa server"
	;;
    *)
	echo "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
	exit 2
	;;
esac
