#!/bin/bash
#
# nepenthes       This shell script takes care of starting and stopping
#                 Nepenthes.
#
# chkconfig: 2345 99 15
# description:    Nepenthes is a low interaction honeypot.
# pidfile: /var/run/nepenthes.pid
# config: /var/nepenthes/etc/nepenthes/nepenthes.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

# Config
if [ -s /etc/sysconfig/nepenthes ]; then
  . /etc/sysconfig/nepenthes
fi

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


RETVAL=0

start() {
	# Start daemons.
	echo -n $"Starting $SHORT: "
	daemon $NEPENTHES -D ${OPTIONS}
	RETVAL=$?

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

	  pid=`/sbin/pidof -o %PPID $SHORT`
	  RETVAL=$?

	  [ $RETVAL -eq 0 ] && echo $pid > /var/run/$SHORT.pid
	}
	echo
	return $RETVAL
}

stop() {
	# Stop daemons.
	echo -n $"Shutting down $SHORT: "
	killproc $NEPENTHES
	RETVAL=$?

	[ $RETVAL -eq 0 ] && {
	  rm -f /var/lock/subsys/$SHORT
	  rm -f /var/run/$SHORT.pid
	}
	echo
	return $RETVAL
}

rhstatus() {
	status $NEPENTHES
	RETVAL=$?
	return $RETVAL
}

restart() {
	stop
	sleep 2
	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
	  ;;
	*)
	  echo $"Usage: nepenthes {start|stop|status|restart|condrestart|reload}"
	  exit 1
esac

exit $RETVAL

