#!/bin/sh
# Contributed by Wulf Dietrich - w_dietrich@hotmail.com 21/1/2000
#
# Customised version of logs agent, specifically for oracle backups
#

AGENT=`basename $0`
# exec 2>/tmp/$AGENT.trc
# set -x

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

OK=1
DEFPATTERNS="$BCNUHOME/etc/${AGENT}_patterns"

eval exparam=\"$BCNUPARAM\"

for param in $exparam
do

	bcnu_param
	FILE=$p1
	PATTERNS=${p2:=$DEFPATTERNS}
	BASEFILE=`basename $FILE`
	
       POSFILE=$BCNUHOME/flags/${AGENT}_${BASEFILE}

       if [ "$FILE" = "AIX/errpt" ]
               then
               POS_NEW=`date +%m%d%H%M%y`
               if [ -r "$POSFILE.pos" ]
                       then
                               POS=`cat $POSFILE.pos`
               else
                               POS=$POS_NEW
               fi
               /bin/errpt -s $POS >$BCNUTMP/$AGENT.$$

               # set new position
               echo $POS_NEW >$POSFILE.pos
	else

		if [ ! -r "$FILE" ] 
		then
			echo "$BCNUAGENT $FILE not found"
			continue
		fi

		# pos-file differs from default, so we need to set it
		$BCNUMESS -n $POSFILE $FILE >$BCNUTMP/$AGENT.$$
	
	fi

	# while watching to the same files as logs-agent we need to take other names
	ERRFILE="$BCNUTMP/${AGENT}_${BASEFILE}.error"
	WARNFILE="$BCNUTMP/${AGENT}_${BASEFILE}.warning"

	touch $POSFILE	# to make sure it doesn't get deleted

	sed -ne "\+^\[$FILE - error+,\+^\[+p" $PATTERNS | sed -ne "/^[^\[#]/p" >$ERRFILE
	sed -ne "\+^\[$FILE - warning+,\+^\[+p" $PATTERNS | sed -ne "/^[^\[#]/p" >$WARNFILE


	if [ -s "$WARNFILE" ]
	then
	 if $GREP -i -f $WARNFILE $BCNUTMP/$AGENT.$$ >$BCNUTMP/$AGENT.out 2>/dev/null
	   then
		BCNUMSG="$BCNU -m 'warning - Oracle-DB Backups (see $FILE)' -f $BCNUTMP/$AGENT.out -e $BCNU_WARN -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_warn_send
		OK=
	 fi
	fi
	if [ -s "$ERRFILE" ]
	then
	 if $GREP -i -f $ERRFILE $BCNUTMP/$AGENT.$$ >$BCNUTMP/$AGENT.out 2>/dev/null
	   then
		BCNUMSG="$BCNU -m 'error - Oracle-DB Backups (see $FILE)' -f $BCNUTMP/$AGENT.out -e $BCNU_ERR -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_err_send
		OK=
	 fi
	fi

done

if [ $OK ]
	then
		BCNUMSG="$BCNU -m 'ok - Oracle-DB Backups' -e $BCNU_OK -t $BCNUHOSTTYPE $BCNUHOST"
		bcnu_send
fi

rm $BCNUTMP/$AGENT.$$

