#! /bin/sh 

### BEGIN INIT INFO
# Provides:          prads
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start PRADS sensor.
# Description:       This script provides a packet level 
#                    passive host and service detecion system
#                    in a chroot environment
### END INIT INFO
#remember, for this to work out:
# groupadd -g 5505 prads
# useradd -g 5505 -u 5505 prads -h /var/run/prads -s /bin/false
# useradd -g 5505 -u 5505 prads -d /var/run/prads -s /bin/false

# Source function library
. /lib/lsb/init-functions

NAME=prads
DESC="Passive Realtime Asset Detection System"
PATH=/sbin:/bin:/usr/sbin:/usr/bin

USER=prads
GROUP=prads
DAEMON=/usr/bin/prads
LOGFILE=/var/log/prads-asset.log
RUNDIR=/var/run/prads
PIDFILE=$RUNDIR/$NAME.pid
HOME_NETS="10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,fe80::/64"
PIDNAME=$(echo $PIDFILE|sed "s@^$RUNDIR@@")

test -x $DAEMON || exit 0

mkdir -p $RUNDIR
chgrp $GROUP $RUNDIR
chmod g+w $RUNDIR

# Include prads defaults if available
if [ -f /etc/default/prads ] ; then
	. /etc/default/prads
fi
DAEMON_OPTS=${DAEMON_OPTS:--D -u $(id -u $USER) -g $(id -g $GROUP) -C $RUNDIR -l $LOGFILE -a $HOME_NETS -p $PIDNAME}

if [ -n "$INTERFACE" ]
    then
    DAEMON_OPTS="$DAEMON_OPTS -i $INTERFACE"
fi


case "$1" in
	start)
		output=$(/bin/tempfile -s.prads)
		log_daemon_msg "Starting $DESC" 
		log_progress_msg $NAME
		if start-stop-daemon \
			--start --quiet --pidfile ${PIDFILE} --exec ${DAEMON} -- \
			${DAEMON_OPTS} > ${output} 2>&1; then
			log_end_msg 0
		else
			log_end_msg 1
			cat $output
			exit 1
		fi
		rm $output
		;;
	stop)
		log_daemon_msg "Stopping $DESC"
		log_progress_msg $NAME
		if start-stop-daemon \
		   --stop --quiet --pidfile $PIDFILE --retry 10 \
		   --exec $DAEMON; then
			log_end_msg 0
		else
			log_end_msg 1
		fi
        	;;
	reload)
		log_daemon_msg "Reloading $DESC"
		log_progress_msg $NAME
		if :; then
			log_end_msg 0
		else
			log_end_msg 1
		fi
		;;
	status)
		status_of_proc -p "${PIDFILE}" "${DAEMON}" "${NAME}"
		;;
	restart|force-reload)
		$0 stop
		$0 start
        	;;
	*)
		log_success_msg "Usage: $0 {start|stop|restart|force-reload}"
		exit 1
        ;;
esac

exit 0
