#!/bin/sh

### BEGIN INIT INFO
# Provides:          prads
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $network $local_fs $remote_fs
# Default-Start:     S 2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Prads Realtime Asset Detection System
# Description:       Prads is a server that listens on your network, and
#                    logs the presence of any servers and clients
### END INIT INFO

#
# Author:       Stig Sandbeck Mathisen <ssm@debian.org>
#

DESC="Prads Realtime Asset Detection System"
NAME="prads"
DAEMON="/usr/bin/${NAME}"

test -f $DAEMON || exit 0

# Read LSB init functions
. /lib/lsb/init-functions

# Read DAEMON_OPTS from defaults file, if it exists
if [ -r /etc/default/$NAME -a -f /etc/default/$NAME ]
then
    . /etc/default/$NAME
fi

USER="prads"
GROUP="prads"
CHROOT="/run/prads"
PIDFILE="prads.pid"

DAEMON_OPTS="-D -u $USER -g $GROUP -C $CHROOT -p $PIDFILE ${DAEMON_OPTS:-}"

create_chroot() {
    install -o $USER -g $GROUP -d $CHROOT
}

start_service() {
    log_begin_msg "Starting $DESC..."
    [ ! -d $CHROOT ] && create_chroot
    start-stop-daemon --start --quiet --pidfile $CHROOT/$PIDFILE \
        --umask "007" \
        --exec $DAEMON -- $DAEMON_OPTS
    log_end_msg $?
}

stop_service() {
    log_begin_msg "Stopping $DESC..."
    start-stop-daemon --stop --quiet --oknodo --retry 60 \
        --pidfile $CHROOT/$PIDFILE
    log_end_msg $?
}

status_service() {
    status_of_proc $DAEMON $NAME
}

restart_service() {
    $0 stop && $0 start
}

fail() {
    echo "usage: $0 <start|stop|restart|status>"
    exit 1
}

case "$1" in
    start)   start_service;;
    stop)    stop_service;;
    status)  status_service;;
    restart) restart_service;;
    force-reload) restart_service;;
    *) fail;;
esac
