#!/bin/sh
#
# /etc/init.d/sfcapd
#   and its symbolic link
# /(usr/)sbin/rcsfcapd
#
### BEGIN INIT INFO
# Provides:          sfcapd
# Required-Start:    $syslog $network $remote_fs
# Required-Stop:     $syslog $network $remote_fs
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: sFlow capture daemon
# Description:       sfcapd is the sflow capture daemon of the nfdump tools. It reads sflow
#	data from the network and stores it into nfcapd compatible files.
### END INIT INFO
DESCRIPTION="sFlow capture daemon"

# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
DAEMON_BIN=/usr/sbin/sfcapd
test -x ${DAEMON_BIN} || { echo "${DAEMON_BIN} not installed"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

# Check for existence of needed config file and read it
DAEMON_CONFIG=/etc/sysconfig/sfcapd
test -r ${DAEMON_CONFIG} || { echo "${DAEMON_CONFIG} not existing";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }

# Read config	
. ${DAEMON_CONFIG}

# Source LSB init functions
#. /lib/lsb/init-functions

# Source Shell functions
. /etc/rc.status

# Reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status
case "$1" in
    start)
	echo "Starting ${DESCRIPTION} "
	if [ -n "${SFCAPD_GROUP}" ]; then
		SFCAPD_ARGS="-g ${SFCAPD_USER} ${SFCAPD_ARGS}"
	fi
	if [ -n "${SFCAPD_USER}" ]; then
		SFCAPD_ARGS="-u ${SFCAPD_USER} ${SFCAPD_ARGS}"
	fi
	if [ ! -d "${SFCAPD_DATA_DIR}" ]; then
		rc_failed 6
		rc_status -v
		exit 6
	fi
	for PORT_SPEC in ${SFCAPD_PORTS}; do
		DAEMON_ARGS="${SFCAPD_ARGS}"
		PORT_NUM="`/bin/echo ${PORT_SPEC} | /usr/bin/awk -F , '{print $1}'`"
		FLOW_NAME="`/bin/echo ${PORT_SPEC} | /usr/bin/awk -F , '{print $2}'`"
		FLOW_DATA_DIR="${SFCAPD_DATA_DIR}/${PORT_NUM}"
		DAEMON_PID="/var/run/nfdump/sfcapd-${PORT_NUM}.pid"
		if [ -n "${PORT_NUM}" ]; then
			DAEMON_ARGS="${DAEMON_ARGS} -p ${PORT_NUM}"
		else
			rc_failed 6
			rc_status -v
			exit 6
		fi
		if [ ! -d "${FLOW_DATA_DIR}" ]; then
			/bin/mkdir -p ${FLOW_DATA_DIR}
			if [ -n "${SFCAPD_USER}" ]; then
				/bin/chown ${SFCAPD_USER} "${FLOW_DATA_DIR}"
			fi
			if [ -n "${SFCAPD_GROUP}" ]; then
				/bin/chgrp ${SFCAPD_GROUP} "${FLOW_DATA_DIR}"
			fi
		fi
		if [ -d "${FLOW_DATA_DIR}" ]; then
			DAEMON_ARGS="${DAEMON_ARGS} -l ${FLOW_DATA_DIR}"
		else
			rc_failed 6
			rc_status -v
			exit 6
		fi
		echo -en "\t${PORT_NUM}"
		if [ -n "${FLOW_NAME}" ]; then
			DAEMON_ARGS="${DAEMON_ARGS} -I ${FLOW_NAME}"
			echo -en "\t${FLOW_NAME}"
		fi
		${DAEMON_BIN} -P ${DAEMON_PID} ${DAEMON_ARGS}
		rc_status -v
	done
	;;
    stop)
	echo -n "Shutting down ${DESCRIPTION} "
	/sbin/killproc -TERM ${DAEMON_BIN}
	rc_status -v
	;;
    try-restart|condrestart)
	if test "$1" = "condrestart"; then
		echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
	fi
	$0 status
	if test $? = 0; then
		$0 restart
	else
		rc_reset
	fi
	rc_status
	;;
    restart)
	$0 stop
	$0 start
	rc_status
	;;
    force-reload)
	echo -n "Reload service ${DESCRIPTION} "
	$0 try-restart
	rc_status
	;;
    reload)
	echo -n "Reload service ${DESCRIPTION} "
	rc_failed 3
	rc_status -v
	;;
    status)
	echo -n "Checking for service ${DESCRIPTION} "
	## Check status with checkproc(8), if process is running
	## checkproc will return with exit status 0.

	# Return value is slightly different for the status command:
	# 0 - service up and running
	# 1 - service dead, but /var/run/  pid  file exists
	# 2 - service dead, but /var/lock/ lock file exists
	# 3 - service not running (unused)
	# 4 - service status unknown :-(
	# 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)
	
	# NOTE: checkproc returns LSB compliant status values.
	/sbin/checkproc ${DAEMON_BIN}
	# NOTE: rc_status knows that we called this init script with
	# "status" option and adapts its messages accordingly.
	rc_status -v
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
	exit 1
	;;
esac
rc_exit
