#! /bin/sh
# 
# Init.d file for Samhain, based on the example init.d file written by
# Miquel van Smoorenburg and modified for Debian GNU/Linux by Ian Murdock
#
### BEGIN INIT INFO
# Provides:          samhain
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starting Samhain data integrity and host intrusion alert system
### END INIT INFO
#

prefix="/usr"
exec_prefix="${prefix}"

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=${exec_prefix}/sbin/samhain
NAME=samhain
DESC="file integrity checker"
PIDFILE=/var/run/${NAME}/${NAME}.pid

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

set -e

# Check if a daemon is running
running()
{
# Check with pidfile first, if available
    if [ -r "$PIDFILE" ] ; then
	    pid=`cat $PIDFILE`
# No pid, probably no daemon present
	    if [ -n "$pid" ] ; then
		    pidofproc -p $PIDFILE $DAEMON
		    return $?
	    fi
    fi
# Try to find the daemon by name
    pidof $DAEMON >/dev/null 
    return $?
}

# Initialize 
init_db()
{
# Initialize the database only if does not exist yet, abort if
# it cannot be created
     # Look in the configuraton file if the Samhain DB has been moved, otherwise use the default
     samhaindb_location="`cat /etc/samhain/samhainrc | grep ^SetDatabasePath | sed s/SetDatabasePath=//`"
     if [ -z "$samhaindb_location" ] ; then
        samhaindb_location="/var/lib/samhain"
     fi
     samhaindb_file="$samhaindb_location/samhain_file"

     [  -f "$samhaindb_file" ] && return
     log_progress_msg "Creating integrity database (this can take some minutes)."
     samhain -t init >/var/log/samhain/samhain-init.log 2>&1
     if [  ! -f "$samhaindb_file" ] ; then
        log_failure_msg "Database could not be created in $samhaindb_location. Review /var/log/samhain/samhain-init.log"
        log_end_msg 1
        exit 1
    fi
    log_progress_msg "Database created."
}



case "$1" in
  start)
        [ ! -e /var/run/${NAME} ] && mkdir -p /var/run/${NAME}
        log_begin_msg "Starting $DESC: $NAME"
        init_db
        start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON
        log_end_msg $?
        ;;
  stop)
        log_begin_msg "Stopping $DESC: $NAME"
        start-stop-daemon --stop --quiet --retry 5 --oknodo --pidfile $PIDFILE  --name  $NAME 
        log_end_msg $?
        ;;
  reload)
  	log_begin_msg "Reloading $DESC configuration files: $NAME"
	if running ; then
		start-stop-daemon --stop --signal 1 --quiet --exec $DAEMON
		log_end_msg $?
	else
            	log_daemon_msg " ERROR: $DAEMON is not running."
		log_end_msg 1
	fi

  	;;
  restart|force-reload)
        log_begin_msg "Restarting $DESC: $NAME"
	if running; then
	        start-stop-daemon --stop --quiet --retry 5 --oknodo --pidfile $PIDFILE  --name  $NAME 
	        for i in 1 2 3 ; do
	            if ! running; then break ; fi
	            sleep 1
	        done
	fi
        if  ! running  ; then
            start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON
            log_end_msg $?
        else
            log_daemon_msg " ERROR: $DAEMON did not die in the expected time, will not restart/force-reload"
            log_end_msg 1
        fi
        ;;
   status)
	if [ -e $PIDFILE ] ; then
	   	status_of_proc -p $PIDFILE $DAEMON $NAME && exit 0 || exit $?
	else
	   	status_of_proc $DAEMON $NAME && exit 0 || exit $?
	fi
        ;;
  *)
        N=/etc/init.d/${0##*/}
        echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
        exit 1
        ;;
esac

exit 0
