#!/bin/sh
#
# ipclassify       This shellscript takes care of starting and stopping ipclassify.
#
# chkconfig: 2345 10 88
# description: ipclassify is a application that enables you to block internet \
#              traffic based on large lists of ip address ranges in order to \
#              protect your privacy.
# processname: ipclassify
#
### BEGIN INIT INFO
# Provides:          FOO
# Required-Start:    $syslog $remote_fs
# Required-Stop:     $syslog $remote_fs
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: ipclassify
# Description:       blocks internet traffic based on large lists of ip address ranges
### END INIT INFO

PIDF="/var/run/ipclassify.pid"
PRG="ipclassify"
LOG="/var/log/${PRG}.log"
CONF="/etc/${PRG}.conf"
BIN="/usr/bin/$PRG"
# CMD="$BIN -c $CONF -p $PIDF -l $LOG >/dev/null &"


. /etc/rc.status

# Reset status of this service
rc_reset

if [ -f /etc/sysconfig/ipclassify ]; then
  . /etc/sysconfig/ipclassify
fi

if [ -z "${NUMTHREADS}" ]; then NUMTHREADS="-T 4"; fi
if [ -z "$NUMTHREADS" ] && [ "${NUMTHREADS+XXX}" = "XXX" ]; then NUMTHREADS="-T 4"; fi

CMD="$BIN ${NUMTHREADS} ${WATCHDOG} >/dev/null &"

fail () {
	failure "$2"
	echo
	[ -n "$1" ] && echo "$1"
}

start () {
	echo -n $"Starting ipclassify: "
	if ! [ -x $BIN  ]; then
		fail "Can't execute $BIN" "$PRG startup"
		rc_failed
	fi;
	if [ -f $PIDF  ]; then
		PID=`cat $PIDF`
		if ps -p $PID >/dev/null; then
			fail "$PIDF exists and $PRG is running." "$PRG startup"
			rc_failed
		fi;
	fi;
	startproc --pidfile "$PIDF" "$CMD"
	RETVAL=$?
	return $RETVAL
}

stop () {
	echo -n $"Stopping ipclassify: "
	killproc -p "$PIDF" "$PRG"
	RETVAL=$?
	return $RETVAL
}

case "$1" in
  start)
	start
	rc_status -v
	;;
  stop)
	stop
	rc_status -v
	;;
  reload)
	if [ -f $PIDF ]; then
		kill -HUP `cat $PIDF`
		RETVAL=$?
		rc_status -v
	fi
	;;
  restart)
	stop
	rc_status -v
	start
	rc_status -v
	;;
  condrestart)
	# restart only if already running
	if [ -f $PIDF ]; then
		stop
		start
		RETVAL=$?
		rc_status -v
	fi 
	;;
  status)
	checkproc $PRG 
	rc_status -v
	;;
  top)
	if [ -f $PIDF ]; then
		a=""
		for i in `pidof $PRG`; do
			a="$a -p $i"
		done
		top $a
	fi
	;;
  *)
	echo $"Usage: $0 {start|stop|reload|restart|condrestart|status|top}"
	exit 1
esac
rc_exit
