#! /bin/sh
#
# Startup script for psad
#
# description: The Port Scan Attack Detector (psad)
# processname: psad
# pidfile: /var/run/psad/psad.pid
# config: /etc/psad/psad.conf

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/psad
PIDDIR=/var/run/psad
NAME=psad
DESC="Port Scan Attack Detector"

test -f $DAEMON || exit 0

set -e

start() {
	RUN=0
	for conffile in  /etc/syslog.conf /etc/syslog-ng/syslog-ng.conf; do
		if [ -r $conffile ] && [ ! -z "`grep -v ^# $conffile | grep psadfifo `" ] ;then
			RUN=1
		fi
	done
	if [ ! -f /etc/psad/psad.conf ] ; then
		echo " ERR: You are missing the configuration file .. failed."
		exit 1
	fi
	if [ $RUN -eq 1 ] 
	then
	echo -n "Starting $DESC and associated daemons: "
		start-stop-daemon --start --quiet --pidfile $PIDDIR/$NAME.pid \
			--exec $DAEMON -- -c /etc/psad/psad.conf \
#			--signatures /etc/psad/signatures \
#			--snort-rdir /etc/psad/snort_rules/ \
#			-a /etc/psad/auto_dl
		RETVAL=$?
	        if [ $RETVAL -eq 0 ]; then
		            touch /var/run/psad.lock
		fi
		echo "$NAME."

	else
           echo "ERR: Syslog has not been configured to send messages to"
           echo "/var/lib/psad/psadfifo. Please configure it as described in psad(8)."
	fi
}

stop() {
# This  can be done also with psad --Kill
	echo -n "Stopping $DESC: "
	echo -n "Shutting down the psadwatchd monitoring daemon: "
	start-stop-daemon --oknodo --stop --quiet --pidfile $PIDDIR/psadwatchd.pid ; echo "psadwatchd."
	echo -n "Shutting down the psad daemon: " 
        start-stop-daemon --oknodo --stop --quiet --pidfile $PIDDIR/psad.pid ; echo "psad."
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f /var/run/psad.lock
	echo -n "Shutting down the kmsgs daemon: "
        start-stop-daemon --oknodo --stop --quiet --pidfile $PIDDIR/kmsgsd.pid ; echo "kmsgs."
#	echo -n "Shutting down the disk monitoring daemon: "
#        start-stop-daemon --oknodo --stop --quiet --pidfile $PIDDIR/diskmond.pid ; echo "diskmond."
#		--exec $DAEMON
	echo "$NAME."

}

status() {
	$DAEMON --Status
}



case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart|force-reload|reload)
  	stop
	sleep 2
	start
	;;
  status)
	status
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0
