#!/bin/sh

# PROVIDE: yabm 
# REQUIRE: NETWORKING 
# KEYWORD: FreeBSD shutdown
#
# 
# Add the following to your /etc/rc.conf to enable yabm
# 
# yabm_enable="YES"
#
# Then, for each interface you want to monitor you add the following, where the 
# number 0 has to be increased for each interface
# Example
# yabm_if0="fxp0"
# yabm_if0_output="/path/to/image.png"
# yabm_if0_flags="-m 512k -t"
#
# Monitoring ip-aliases is like monitoring interfaces except the keyword
# alias is used instead of if
# Example
# yabm_alias0="192.168.0.7"
# yabm_alias0_output="/path/to/image2.png"
# yabm_alias0_flags=""
#
#
# DO NOT CHANGE OR SET ANY VALUES IN THIS FILE
#

yabm_enable=${yabm_enable-"NO"}

. /etc/rc.subr

name="yabm"
rcvar=`set_rcvar`
command="/usr/local/bin/yabm"

load_rc_config $name

count=0
while : ; do
	eval yabm_if=\$yabm_if${count}
	eval yabm_alias=\$yabm_alias${count}
	if [ -n "${yabm_if}" ]; then 
		eval yabm_output=\$yabm_if${count}_output
		eval yabm_flags=\$yabm_if${count}_flags
		if [ ! -n "${yabm_output}" ]; then
			yabm_output="${yabm_if}.png"
		fi	
		pidfile=/var/run/yabm-${yabm_if}.pid
		start_cmd="${command} -i ${yabm_if} ${yabm_flags} -o ${yabm_output} -p ${pidfile}"
		run_rc_command "$1"
	fi
	if [ -n "${yabm_alias}" ]; then
		eval yabm_output=\$yabm_alias${count}_output
		eval yabm_flags=\$yabm_alias${count}_flags
		if [ ! -n "${yabm_output}" ]; then
			yabm_output="${yabm_alias}.png"
		fi	
		pidfile=/var/run/yabm-${yabm_alias}.pid
		start_cmd="${command} -a ${yabm_alias} ${yabm_flags} -o ${yabm_output} -p ${pidfile}"
		run_rc_command "$1"
	fi

	if [ -n "${yabm_if}" ]; then
		count=$((${count} + 1))
	elif [ -n "${yabm_alias}" ]; then
		count=$((${count} + 1))
	else
		break;
	fi
done

