#!/bin/sh
#
# pmacctd	pcap-based accounting daemon.
#
# chkconfig: - 90 10
# description:	pcap-based accounting daemon \
#		it gathers packets from an \
#		interface it is bound to (enabling optionally Promiscuous \
#		mode); statistics may be either pushed to stdout, stored \
#		in a memory table or a PostgreSQL/MySQL/SQLite database.

# processname: pmacctd
# config: /etc/pmacct/pmacct.conf
# pidfile: /var/run/pmacctd.pid

### BEGIN INIT INFO
# Provides: pmacctd
# Required-start: $network $syslog
# Required-stop: $network $syslog
# Default-start: 2 3 4 5
# Default-stop: 0 1 6
# Short-Description: NetFlow accounting daemon
### END INIT INFO

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

PIDFILE=/var/run/pmacctd.pid
LOCKFILE=/var/lock/subsys/pmacctd
RETVAL=0

DAEMON=/usr/sbin/pmacctd
NAME=pmacctd
CONFDIR=/etc/pmacct

start()
{
	start_daemon --lockfile "$LOCKFILE" --expect-user root -- $DAEMON -D -F $PIDFILE -f "$CONFDIR/pmacctd.conf"
	RETVAL=$?
	return $RETVAL
}

stop()
{
    for P in $(ls /var/run/$NAME*); do
	N=$(echo "$P" | sed "s/\/var\/run\/$NAME\.pid//g")
	stop_daemon --displayname "$NAME$N" --pidfile "$P" --lockfile "$LOCKFILE" --expect-user root -- pmacctd
	RETVAL=$?
	if [ ! $RETVAL = 0 ]; then
	    RETVAL2=$RETVAL
	fi
    done
    return $RETVAL2
}

restart()
{
        stop
        sleep 3
        start
}

stat()
{
    for P in $(ls /var/run/$NAME*); do
	N=$(echo "$P" | sed "s/\/var\/run\/$NAME\.pid//g")
	status --displayname "$NAME$N" --pidfile "$P" --expect-user root -- pmacctd
	RETVAL=$?
	if [ ! $RETVAL = 0 ]; then
	    RETVAL2=$RETVAL
	fi
    done
    return $RETVAL
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload|restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE*" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE*" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE*" ]; then
			restart
		fi
		;;
	status)
		stat
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
