#!/bin/sh

BCNUNOLOG="yes"		# don't show blackouts

#. /usr/local/bcnu/etc/bcnuenv
. $BCNUHOME/agent/agent_head

BCNUSEVERITY=$BCNU_ERR        # assume error if not told otherwise
BCNURETRY=3		# try each host 3 times before down
BCNUSLEEP=10		# wait 10 seconds between retries
BCNURUNLIMIT=3		# run max 3 net-test agents at a time

export BCNURETRY BCNUSLEEP BCNURUNLIMIT BCNUSEVERITY

SAVAGENT=$BCNUAGENT
SAVLOGINT=$BCNULOGINT
export SAVAGENT SAVLOGINT BCNUAGENT BCNULOGINT

runlimit=0
#set -x
for param in $BCNUPARAM
do
	eval "sed -ne '/^[^#]/p' $param" | 

	while read host services
	do
		param=$host
		bcnu_param
#set -x
		if [ "$p1" = "BCNUHOST" ]
		then
			BCNUHOST=$p2	
			continue		
		elif [ "$p1" = "BCNUHOSTTYPE" ]
		then
			BCNUHOSTTYPE=$p2	
			continue		
		elif [ "$p1" = "RETRIES" ]
		then
			BCNURETRY=$p2	
			continue		
		elif [ "$p1" = "SEVERITY" ]
		then
			BCNUSEVERITY=$p2
			continue
		elif [ "$p1" = "RUNLIMIT" ]
		then
			BCNURUNLIMIT=$p2
			continue
		fi

		if expr "$services" : '.*bcnu.*' >/dev/null
		then
			up=`$BCNU -a get -f /data/hosts/$p1/av $BCNUHOST | cut -f2 -d:`
			if [ "$up" = "1" ]
				then
					echo "$STAMP agent(net): system ($p1) has been shut down"
					continue
			fi
		fi

		# only run BCNURUNLIMIT hosts at a time

		if [ $runlimit -ge $BCNURUNLIMIT ]
			then
				wait
				runlimit=0
		fi

		$BCNUHOME/agent/net-test $BCNUHOST $BCNUAGENT $BCNULOGINT "$host" "$services" &
		runlimit=`expr $runlimit + 1`
	done

done

wait
