#!/bin/bash
#
# ossec-hids      This shell script takes care of starting and stopping
#                 the OSSEC-HIDS daemon(s).
#
# chkconfig: 2345 99 15
# description:    OSSEC-HIDS is an Open Source Host-based Intrusion \
#                 Detection System.
# config: /etc/ossec-init.conf

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 1

# Check for binaries and configs
[ -x /var/ossec/bin/ossec-control ] && {
  OSSEC=/var/ossec/bin/ossec-control
  SHORT=ossec-hids
}
[ -z "$OSSEC" ] && exit 1
[ -r /etc/ossec-init.conf ] || exit 1


RETVAL=0

start() {
	# Start daemons.
	gprintf "Starting %s: " "$SHORT"
	initlog -q -c "$OSSEC start"
	RETVAL=$?

	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$SHORT

	[ $RETVAL -eq 0 ] && success "%s startup" "$SHORT" || failure "%s startup" "$SHORT"
	echo
	return $RETVAL
}

stop() {
	# Stop daemons.
	gprintf "Shutting down %s: " "$SHORT"
	initlog -q -c "$OSSEC stop"
	RETVAL=$?

	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$SHORT

	[ $RETVAL -eq 0 ] && success "%s shutdown" "$SHORT" || failure "%s shutdown" "$SHORT"
	echo
	return $RETVAL
}

rhstatus() {
	$OSSEC status
	RETVAL=0
	return $RETVAL
}

restart() {
	stop
	sleep 10
	start
}


# See how we were called.
case "$1" in
	start)
	  start
	  ;;
	stop)
	  stop
	  ;;
	status)
	  rhstatus
	  ;;
	restart)
	  restart
	  ;;
	condrestart)
	  if [ -e /var/lock/subsys/$SHORT ]; then restart; fi
	  ;;
	reload)
	  restart
	  ;;
	*)
	  gprintf "Usage: ossec-hids {start|stop|status|restart|condrestart|reload}\n"
	  exit 1
esac

exit $RETVAL

