#!/bin/sh

BCNUHOST=$1
BCNUHOST=${BCNUHOST:=`uname -n`}
BCNUAGENT=$2
BCNUAGENT=${BCNUAGENT:=`basename $0`}
BCNULOGINT=$3
BCNULOGINT=${BCNULOGINT:=0}
BCNUHOSTTYPE=${BCNUHOSTTYPE:=1}

. $BCNUHOME/etc/bcnuenv.$BCNUSERVER
# read local settings

if [ -r $BCNUHOME/etc/bcnulocal ]
        then
                . $BCNUHOME/etc/bcnulocal
fi

BCNU="$BCNUHOME/bin/bcnu -s $BCNUPORT ${ALTHOST:+-h $ALTHOST}"
BCNUMESS=$BCNUHOME/bin/bcnumsg

export BCNUAGENT BCNUHOST BCNUHOME BCNU BCNUMSG BCNULOGINT BCNUHOSTTYPE

# set variables for WARNING, ERROR OK

BCNU_ERR=9	# error level
BCNU_ALERT=6	# alert level
BCNU_WARN=5	# warning level
BCNU_OK=0	# OK

BCNU_BLACK=1	# blackout


OK=1
STAMP=`date +%Y%m%d:%H.%M:`
DAY=`date +%a`
HOUR=`date +%H`

######################################################################
# Functions used by agents
######################################################################

bcnu_param()	# Parse paramaters
{
	if expr "$param" : '.*+PAGE.*' >/dev/null
		then
			PAGE="Y"
			param="`echo $param | sed "s/+PAGE//"`"
		else
			PAGE=
	fi
	OLDIFS=$IFS
	IFS="="
	set -- $param
	p1=$1
	p2=$2
	p3=$3
	p4=$4
	p5=$5
	p6=$6
	IFS=$OLDIFS
}

bcnu_err_send()	
{

# check to see if I should log this time. 
# if last run was OK the log it this time

	if [ -r $BCNUHOME/flags/$host$BCNUAGENT.ok ]
                then
                        rm $BCNUHOME/flags/$host$BCNUAGENT.ok
			BCNULOGINT=0
	fi

	echo $STAMP $BCNUAGENT $BCNUMSG >$BCNUHOME/flags/$host$BCNUAGENT.bad

	if [ $PAGE ]
		then
			BCNUTEMPMSG="$BCNUMSG"
			BCNUMSG=`echo "$BCNUTEMPMSG" | sed "s/-m '/-m 'PAGE /" `
	fi

	bcnu_send_now
}

bcnu_warn_send()
{

# check to see if I should log this time. 
# if last run was OK the log it this time

	if [ -r $BCNUHOME/flags/$host$BCNUAGENT.ok ]
                then
                        rm $BCNUHOME/flags/$host$BCNUAGENT.ok
			BCNULOGINT=0
	fi

	echo $STAMP $BCNUAGENT $BCNUMSG >$BCNUHOME/flags/$host$BCNUAGENT.bad

	bcnu_send_now
}

bcnu_send()
{
	# should we log the message - only if <= 0 and no error log exists
	if [ -r $BCNUHOME/flags/$host$BCNUAGENT.bad ]
		then
			rm $BCNUHOME/flags/$host$BCNUAGENT.bad
			BCNULOGINT=0
	fi

	echo $STAMP $BCNUAGENT $BCNUMSG >$BCNUHOME/flags/$host$BCNUAGENT.ok

	bcnu_send_now
}
		
bcnu_send_now()
{

	if [ $BCNULOGINT -gt 0 ]
		then
			echo $STAMP nolog BCNUAGENT=$BCNUAGENT $BCNUMSG >>$BCNUHOME/logs/message.log
			return
	fi

	echo $STAMP log BCNUAGENT=$BCNUAGENT $BCNUMSG >>$BCNUHOME/logs/message.log

	if [ -r $BCNUHOME/retry/$BCNUAGENT ]
		then
			rm $BCNUHOME/retry/$BCNUAGENT
			echo "$STAMP removed ($BCNUAGENT) normal run" >>$BCNUHOME/logs/retry.log
	fi

# handle proxy
#set -x

	if [ ! -z "$PROXYAGENT" ]
		then
			BCNUAGENT=$PROXYAGENT
	fi
 
	if eval $BCNUMSG
		then
			:
	else
		echo \# Retry from $STAMP >$BCNUHOME/retry/$BCNUAGENT
		echo "BCNUAGENT=$BCNUAGENT;export BCNUAGENT" >>$BCNUHOME/retry/$BCNUAGENT
		echo $BCNUMSG >>$BCNUHOME/retry/$BCNUAGENT
		chmod 750 $BCNUHOME/retry/$BCNUAGENT
		echo "Failed - will retry: $STAMP BCNUAGENT=$BCNUAGENT $BCNUMSG"
	fi

}


# Check for any errors e.g. out of disk space
bcnu_check_err()
{
SAVESTAT=$?
EMSG=$1
ERRMSG=${EMSG:="unexpected error"}

if [ $# -eq 3 ]
	then
		FILE=$2
		PATTERN=$3
        	$GREP "$PATTERN" $FILE >/dev/null
        	if [ $? -eq 0 ]
			then
				SAVESTAT=1
		fi
fi
if [ $SAVESTAT -ne 0 ]
	then
		BCNUMSG="$BCNU -m 'error - $ERRMSG' -e $BCNU_ERR -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_err_send
		exit
fi
}

blackout()
{
BLACKOUT=`date +%H.%M.%S`

BCNUMSG="$BCNU -m 'zzzz - blackout started at $BLACKOUT' -e $BCNU_BLACK $BCNUHOST"
bcnu_send
}

sort_by_severity()
{
	sed -e "s/^ok /wok /g" $1 | sort $2 | sed "s/^wok /ok /g"
}

###########################################################################
#
#
# Agent starts running here all above are functions
#
#echo "bcnu:running agent($BCNUAGENT) , logging to ($BCNUHOST) params= ($BCNUPARAM)"

if [ "$BCNUPARAM" = "blackout" ]
	then
		if [ -z "$BCNUNOLOG" ]
			then
				blackout
				exit
			else
				exit
		fi
fi

# suggested by Wulf Dietrich allows debugging to be switched on
for AGENT in $AGENTDEBUG
do
	if [ "$AGENT" = "$BCNUAGENT" ]
	   then
		exec 2>$BCNUTMP/${AGENT}.trc
		set -x
	fi
done
