#!/bin/sh
#
# ntop	A network traffic probe similar to the UNIX top command
#
# chkconfig: - 81 19
# description: A network traffic probe similar to the UNIX top command
# processname: ntop
# pidfile: /var/run/ntop.pid
# config: /etc/ntop/ntop.conf

### BEGIN INIT INFO
# Provides:             ntop
# Required-Start:       $network
# Required-Stop:        $network
# Default-Start:
# Default-Stop:
# Short-Description:    Start ntop at boot time
# Description:          Enable ntop service
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

CONFIGFILE=/etc/ntop/ntop.conf
PIDFILE=/var/run/ntop.pid
LOCKFILE=/var/lock/subsys/ntop
RETVAL=0

# Get service config - may override defaults
[ -f /etc/sysconfig/ntop ] && . /etc/sysconfig/ntop

if [ -z "$NTOP_DIR" ]; then
	echo "NTOP_DIR not set in /etc/sysconfig/ntop"
	exit 6
fi

# NTOPD="/usr/sbin/ntop @/etc/ntop/ntop.conf  --daemon"
NTOPD="/usr/sbin/ntop"

start()
{
	# Check if ntop is initialized
	if [ ! -f "$NTOP_DIR/ntop_pw.db" ]; then
		echo "ntop is not initialized. Try '$0 init' before start."
		exit 6
	fi

	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user ntop -- $NTOPD @$CONFIGFILE --daemon -P $NTOP_DIR
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user ntop -- $NTOPD
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	sleep 2
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	init)
		$NTOPD -P $NTOP_DIR @$CONFIGFILE -A
		;;

	status)
		status --pidfile "$PIDFILE" --expect-user ntop -- $NTOPD
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|status|condrestart|condreload|condstop}"
		RETVAL=1
esac

exit $RETVAL
