#!/bin/sh

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

OK=1
COMPACT=""

# allow the use of $AGENT_patterns file
if [ -s $BCNUHOME/etc/${BCNUAGENT}_patterns ]
	then
		DEFPATTERNS="$BCNUHOME/etc/${BCNUAGENT}_patterns"
else
	DEFPATTERNS="$BCNUHOME/etc/logpatterns"
fi

DEFMSGTRANS="compact"
DEFPOSHANDLING="set"

TMPFILE=$BCNUTMP/$BCNUAGENT.tmp
OUTFILE=$BCNUTMP/$BCNUAGENT.out
ERRFILE="$BCNUTMP/$BCNUAGENT.error"
WARNFILE="$BCNUTMP/$BCNUAGENT.warning"
# to collect output for compact mode
ERROUTFILE="$BCNUTMP/$BCNUAGENT.error.out"
WARNOUTFILE="$BCNUTMP/$BCNUAGENT.warning.out"

rm -f $ERROUTFILE $WARNOUTFILE 2>/dev/null

eval exparam=\"$BCNUPARAM\"

for param in $exparam
do

	bcnu_param
	FILE=$p1
	SEDFILE=$FILE
	PATTERNS=${p2:=$DEFPATTERNS}
	MSGTRANS=${p3:=$DEFMSGTRANS}
	POSHANDLING=${p4:=$DEFPOSHANDLING}

	if [ "$FILE" = "AIX/errpt" ]
	then
		CMD="echo $FILE"
	else
		CMD="eval eval ls $FILE"
	fi
	for FILE in `$CMD`
	do
		BASEFILE=`basename $FILE`
		SUM=`echo $FILE | sum | awk '{printf "%s.%s\n", $1, $2}'`
		# uniqe name for pos file and all other files
	        POSNAME=${BCNUAGENT}_${BASEFILE}_${SUM}
	        POSFILE=$BCNUHOME/flags/$POSNAME.pos
		
		if [ "$FILE" = "AIX/errpt" ]
			then
			POS_NEW=`date +%m%d%H%M%y`
			if [ -r "$POSFILE" ]
				then
					POS=`cat $POSFILE`
			else
					POS=$POS_NEW
			fi
			/bin/errpt -s $POS >$TMPFILE
	
			# set new position
			echo $POS_NEW >$POSFILE
		else
	
			if [ ! -r "$FILE" ] 
			then
				echo "$BCNUAGENT $FILE not found"
				continue
			fi
	
			# pos-file differs from default, so we need to set it
			if [ "$POSHANDLING" = "set" ] 
			then

				$BCNUMESS -n $POSNAME $FILE >$TMPFILE
				touch $POSFILE	# to make sure it doesn't get deleted

			elif [ "$POSHANDLING" = "skip" ]
			then

				$BCNUMESS -s -n $POSNAME $FILE >$TMPFILE

			fi
		
		fi
	
	
	
		sed -ne "\+^\[$SEDFILE - error+,\+^\[+p" $PATTERNS | sed -ne "/^[^\[#]/p" >$ERRFILE
		sed -ne "\+^\[$SEDFILE - warning+,\+^\[+p" $PATTERNS | sed -ne "/^[^\[#]/p" >$WARNFILE
	
	
		if [ -s "$WARNFILE" ]
		then
		 if $GREP -i -f $WARNFILE $TMPFILE >$OUTFILE 2>/dev/null
		   then
		    	if [ "$MSGTRANS" = compact ]
		    	then
				{ echo $FILE: ; cat $OUTFILE ; echo ; } >>$WARNOUTFILE
				COMPACT=1
		    	else
				BCNUMSG="$BCNU -m 'warning - pattern found (see $FILE)' -f $OUTFILE -e $BCNU_WARN -t $BCNUHOSTTYPE $BCNUHOST"
				bcnu_warn_send
		    	fi
			OK=
		 fi
		fi
		if [ -s "$ERRFILE" ]
		then
		 if $GREP -i -f $ERRFILE $TMPFILE >$OUTFILE 2>/dev/null
		   then
		    	if [ "$MSGTRANS" = compact ]
		    	then
				{ echo $FILE: ; cat $OUTFILE ; echo ; } >>$ERROUTFILE
				COMPACT=1
		    	else
				BCNUMSG="$BCNU -m 'error - pattern found (see $FILE)' -f $OUTFILE -e $BCNU_ERR -t $BCNUHOSTTYPE $BCNUHOST"
				bcnu_err_send
		    	fi
			OK=
		 fi
		fi

	done
done
	
if [ $OK ]
then
		BCNUMSG="$BCNU -m 'ok - patterns not found' -e $BCNU_OK -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_send
else
	if [ $COMPACT ]
	then
	  if [ -s "$WARNOUTFILE" ]
	  then
		BCNUMSG="$BCNU -m 'warning - pattern found' -f $WARNOUTFILE -e $BCNU_WARN -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_warn_send
	  fi

	  # wait a bit
	  sleep 1

	  if [ -s "$ERROUTFILE" ]
	  then
		BCNUMSG="$BCNU -m 'error - pattern found' -f $ERROUTFILE -e $BCNU_ERR -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_err_send
	  fi
	fi
fi

