#!/bin/sh
#
#
# /etc/init.d/barnyard2
#   and its symbolic link
# /(usr/)sbin/rcbarnyard2
#
##
### BEGIN INIT INFO
# Provides:          barnyard2
# Required-Start:    $syslog $remote_fs
# Should-Start:      $time ypbind smtp snort
# Required-Stop:     $syslog $remote_fs
# Should-Stop:       ypbind smtp
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: barnyard2 unified log parser for snort
# Description:       Start barnyard2 to allow parsing unified logs of snort
### END INIT INFO
#


# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
barnyard2_BIN=/usr/bin/barnyard2
test -x $barnyard2_BIN || { echo "$barnyard2_BIN not installed";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 5; fi; }

# Check for existence of needed config file and read it
barnyard2_CONFIG=/etc/sysconfig/barnyard2
test -r $barnyard2_CONFIG || { echo "$barnyard2_CONFIG not existing";
        if [ "$1" = "stop" ]; then exit 0;
        else exit 6; fi; }

# Read config
. $barnyard2_CONFIG

. /etc/rc.status

# Reset status of this service
rc_reset


case "$1" in
    start)
        echo -n "Starting barnyard2 "
	for INT in ${INTERFACES} ; do
	  ARCHIVEDIR="${SNORTDIR}/${INT}/archive"
	  WALDO_FILE="${SNORTDIR}/${INT}/barnyard2.waldo"
	  mkdir -p ${ARCHIVEDIR} `dirname ${WALDO_FILE}`
	  chown -R ${USER}:${GROUP} ${ARCHIVEDIR} `dirname ${WALDO_FILE}`
	  BARNYARD_OPTS="-D -c ${CONF} -d ${SNORTDIR}/${INT} -w ${WALDO_FILE} -a ${ARCHIVEDIR} -f ${LOG_FILE} -X ${PIDFILE} ${EXTRA_ARGS}"
	  /sbin/startproc $barnyard2_BIN ${BARNYARD_OPTS}
	done
        # Remember status and be verbose
        rc_status -v
        ;;
    stop)
        echo -n "Shutting down barnyard2 "        
        /sbin/killproc -TERM $barnyard2_BIN

        # Remember status and be verbose
        rc_status -v
        ;;
    try-restart|condrestart)
        if test "$1" = "condrestart"; then
                echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
        fi
        $0 status
        if test $? = 0; then
                $0 restart
        else
                rc_reset        # Not running is not a failure.
        fi
        # Remember status and be quiet
        rc_status
        ;;
    restart)
        $0 stop
        $0 start

        # Remember status and be quiet
        rc_status
        ;;
    force-reload)
        echo -n "Reload service barnyard2 "
        ## if it supports it:
        /sbin/killproc -HUP $barnyard2_BIN
        #touch /var/run/barnyard2.pid
        rc_status -v

        ## Otherwise:
        #$0 try-restart
        #rc_status
        ;;
    reload)
        # If it supports signaling:
        echo -n "Reload service barnyard2 "
        /sbin/killproc -HUP $barnyard2_BIN
        #touch /var/run/barnyard2.pid
        rc_status -v

        ## Otherwise if it does not support reload:
        #rc_failed 3
        #rc_status -v
        ;;
    status)
        echo -n "Checking for service barnyard2 "
        /sbin/checkproc $barnyard2_BIN
        rc_status -v
        ;;
    probe)
        test /etc/snort/barnyard2.conf -nt /var/run/barnyard2.pid && echo reload
        ;;
    *)
        echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
        exit 1
        ;;
esac
rc_exit
