#!/bin/bash

# nfcd        Startup script for the NetFlow Collector
#
# chkconfig: 2345 97 10
# description: NetFlow Collector analyze exports from Cisco routers.
# processname: nfcd
# config: /etc/netflow/nfcd.conf
# pidfile: /var/run/nfcd.pid

### BEGIN INIT INFO
# Provides:          nfcd
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start and stop the netflow collector daemon
# Description:       Flow Inspector analyze netflow exports from Cisco routers or netflow compatible devices.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nfcd
NAME=nfcd
DESC="NetFlow Collector" 
CONFIG=""
ARGS=""

test -x $DAEMON || exit 0

if [ -z "$CONFIG" ]; then
  PIDFILE=`cat "/etc/netflow/$NAME.conf"|grep '^[[:blank:]]*pidfile[[:blank:]]*='|sed 's/.*=[[:blank:]]*//'|sed 's/[[:blank:]]*#.*//'`
else
  PIDFILE=`cat "$CONFIG"|grep '^[[:blank:]]*pidfile[[:blank:]]*='|sed 's/.*=[[:blank:]]*//'|sed 's/[[:blank:]]*#.*//'`
  ARGS="-c $CONFIG $ARGS"
fi

if [ -z "$PIDFILE" ]; then
  PIDFILE="/var/run/$NAME.pid"
fi


case "$1" in
  start)
	echo -n "Starting $DESC: "
	$DAEMON $ARGS &
	echo "$NAME."

	# Copy backup meminfo information
	cp /proc/meminfo /usr/share/netflow/web/meminfo &>/dev/null
	chmod a+r /usr/share/netflow/web/meminfo &> /dev/null
	;;
  stop)
	echo -n "Stopping $DESC: "

	if [ -f "$PIDFILE" ]; then
	  PIDS=`cat "$PIDFILE"`
	  kill $PIDS &> /dev/null || true
	  sleep 2
	fi
	
	if [ -f "$PIDFILE" ]; then		
	  PIDS=`cat "$PIDFILE"`
	  sleep 2
	  pkill -9 -P $PIDS &> /dev/null || true
	  kill -9 $PIDS &> /dev/null || true
	fi
	echo "$NAME."
	;;
  reload|restart|force-reload)
	#
	#	If the "reload" option is implemented, move the "force-reload"
	#	option to the "reload" entry above. If not, "force-reload" is
	#	just the same as "restart".
	#
	$0 stop
	$0 start
	;;
    *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0

