#!/bin/sh
BCNUNOLOG="yes"
DEFMALERT=20    # for users who haven't set it in hostinfo
#. /usr/local/bcnu/etc/bcnuenv
. $BCNUHOME/agent/agent_head

#set -x

bcnu_default()
{
TODAY=`date "+%a"`

sed -ne "\+^\[alias]+,\+^\[+p" $BCNUHOME/etc/hostinfo | sed -ne "/^[^\[#]/p" >$BCNUTMP/alert.alias
. $BCNUTMP/alert.alias
sed -ne "\+^\[default]+,\+^\[+p" $BCNUHOME/etc/hostinfo | sed -ne "/^[^\[#]/p" >$BCNUTMP/alert.default
. $BCNUTMP/alert.default
}

bcnu_contact()
{
unset INFO AGENTPAGER HOSTPAGER AGENTEMAIL HOSTEMAIL

if grep "\[agent=$aagent.*]" $BCNUHOME/etc/hostinfo >/dev/null
then
        sed -ne "\+^\[agent=$aagent.*]+,\+^\[+p" $BCNUHOME/etc/hostinfo | sed -ne "/^[^\[#]/p" >$BCNUTMP/alert.agent
        . $BCNUTMP/alert.agent

fi
if grep "\[host=$ahost\]" $BCNUHOME/etc/hostinfo >/dev/null
then
        sed -ne "\+^\[host=$ahost]+,\+^\[+p" $BCNUHOME/etc/hostinfo | sed -ne "/^[^\[#]/p" >$BCNUTMP/alert.host
        . $BCNUTMP/alert.host

fi
EMAIL="$DEFEMAIL $HOSTEMAIL $AGENTEMAIL"
PAGER=${AGENTPAGER:-${HOSTPAGER:-$DEFPAGER}}
MALERT=${MALERT:=$DEFMALERT}
}

BW=

# check for alert
if [ -f $BCNUHOME/flags/bcnud.alert ]
   then
        bcnu_default
        # process actions: Options are MAIL, RUN, PAGE, PROXY, ACK, WIN32ALERT

        for param in $BCNUPARAM
        do
             bcnu_param

             if [ "$p1" = "MAIL" ]
                then
                        cat $BCNUHOME/flags/bcnud.alert | sort | {
                        while read ahost aagent adate atime amsg
                        do
                                bcnu_contact
                                if expr "$MAILDAYS" : ".*$TODAY" >/dev/null
                                then
                                        # check for too many alerts
                                        if asent="`cat $BCNUHOME/flags/sentalert.$ahost 2>/dev/null`"
                                                then
                                                        asent=`expr $asent + 1`
                                        else
                                                        asent=1
                                        fi

                                        echo $asent >$BCNUHOME/flags/sentalert.$ahost

                                        if [ $asent -gt $MALERT ]
                                                then
                                                        timestamp=`date +%Y%m%d %H.%M`
                                                        echo "$STAMP agent(alert): too many email alerts for $ahost ($asent) limit=$MALERT - ignoring all alerts"
                                                        echo "$timestamp:alert agent: too many email alerts ($asent) limit=$MALERT" >$BCNUHOME/flags/$ahost.ignore
                                        else
                                                for MADDR in $EMAIL
                                                do
                                                        echo "($ahost:$INFO:$MADDR) $aagent $adate $atime $amsg" >>$BCNUHOME/logs/mail.log
                                                        echo "($ahost:$INFO) $aagent $adate $atime $amsg" >>$BCNUHOME/flags/mailq-$MADDR
                                                if [ $p2 ] ; then
                                                  LOG=`echo $adate | sed "s/-//g"`
                                                  ATTACHED_FILE=`$AWK \
                                                        -v ahost=$ahost \
                                                        -v aagent=$aagent \
                                                        -v astamp="$adate $atime" \
                                                        -v amsg="$amsg" \
                                                        'BEGIN {FS=":"} 
							{ if ( $3 == ahost && $5 == aagent && $6 == astamp && index(amsg,$7)==1 ) {print $11; exit }}' $BCNUHOME/data/${LOG}.log`
                                                  if [ $ATTACHED_FILE ] ; then
							if [ -s $ATTACHED_FILE -a -r $ATTACHED_FILE ] ; then
                                                      case $p2 in
                                                      link)
                                                        # add link to email if attached file exists
                                                        printf "see http://$BCNUHOST$BCNUURL/cgi-bin/bcnuqry.cgi?host=${ahost}&agent=${aagent}&dtime=${adate}+${atime}&logfile=$LOG for details\n\n" >>$BCNUHOME/flags/mailq-$MADDR
                                                        echo  >>$BCNUHOME/flags/mailq-$MADDR
                                                        ;;
                                                      full)
                                                        # add attached file if exists
                                                        cat $ATTACHED_FILE  >>$BCNUHOME/flags/mailq-$MADDR
                                                        echo  >>$BCNUHOME/flags/mailq-$MADDR
                                                        ;;
                                                      esac
                                                    fi
                                                  fi
                                                fi
                                                done
                                        fi
                                else
                                        echo "No mail alerts on $TODAY"
                                        echo "$ahost $aagent $adate $atime $amsg(deferred)" >>$BCNUHOME/flags/bcnud.alert.defer
                                fi
                        done
                        }
             elif [ "$p1" = "PAGE" ]
                then
                        PAGE="Y"
                        sed -n "s/PAGE //p" $BCNUHOME/flags/bcnud.alert | sort | {
                        while read ahost aagent adate atime amsg
                        do
                                bcnu_contact
                                if expr "$PAGEDAYS" : ".*$TODAY" >/dev/null
                                then
                                        for PADDR in $PAGER
                                        do
                                                echo "($ahost:$INFO:$PADDR) $aagent $adate $atime $amsg" >>$BCNUHOME/logs/pager.log
                                                echo "($ahost:$INFO) $amsg" >>$BCNUHOME/flags/pagerq-$PADDR
                                        done
                                else
                                        echo "No pager alerts on $TODAY"
                                        echo "$ahost $aagent $adate $atime $amsg(deferred)" >>$BCNUHOME/flags/bcnud.alert.defer
                                fi
                        done
                        }

             elif [ "$p1" = "ACK" ]
                then
                        cat $BCNUHOME/flags/bcnud.alert >>$BCNUHOME/flags/bcnud.ack
                        chmod 777 $BCNUHOME/flags/bcnud.ack

             elif [ "$p1" = "RUN" ]
                then
                        printf "\n\nsee http://$BCNUHOST$BCNUURL for details\n" | cat $BCNUHOME/flags/bcnud.alert - >>$BCNUTMP/bcnud.alert
                        $BCNUHOME/agent/wraprun $p2 $BCNUTMP/bcnud.alert &

             elif [ "$p1" = "WIN32ALERT" ]
                then
#set -x
			find $BCNUHOME/data/win32alert -type f -exec basename {} \; >$BCNUTMP/win32alerts
			for W in "$DEFWIN32"
			do
				echo $W >>$BCNUTMP/win32alerts
			done
			WIN32=`sort $BCNUTMP/win32alerts | uniq`
                        cat $BCNUHOME/flags/bcnud.alert | sort | {
                        while read ahost aagent adate atime amsg
                        do
#                            bcnu_contact
                            for WADDR in $WIN32
                            do
				echo "($ahost:$INFO:$WADDR) $aagent $adate $atime $amsg" >>$BCNUHOME/logs/win32.log
				echo "($ahost:$INFO) $amsg" >>$BCNUHOME/flags/win32q-$WADDR
			    done
                        done
                        }


             elif [ "$p1" = "PROXY" ]
                      then
                        #set -x
                        BCNULOGINT=0    # make sure it's always logged
                        cat $BCNUHOME/flags/bcnud.alert | {
                        while read ahost aagent adate atime amsg
                        do
                                PROXYAGENT=$aagent
                                BCNUAGENT=PROXY$aagent
                                BCNUMSG="$BCNU -h $ahost -m \"$amsg\" -e $BCNU_ERR $p2"
                                bcnu_err_send
                                PROXYAGENT=
                        done
                        }
                fi
        done

rm $BCNUHOME/flags/bcnud.alert
BW=1

fi
#set -x

# check for queued pager messages to be sent
for q in `ls $BCNUHOME/flags/pagerq-* 2>/dev/null`
do
        PAGEMSG=`sort -u $q | paste -s -d" " -`
        PAGERNO=`expr "$q" :  '.*\-\(.*\)'`
        if $KERMIT $BCNUHOME/agent/pager.ksc  ="$PAGERNO" ="$PAGEMSG"
                then
                        rm $q
        else
                echo "$STAMP agent(alert): unable to page ($PAGERNO)"
        fi
done
#set -x
# check for queued email messages to be sent
for q in `ls $BCNUHOME/flags/mailq-* 2>/dev/null`
do
        printf "\n\nsee http://$BCNUHOST$BCNUURL for details"  >>$q
        EMAIL=`expr "$q" : '.*/flags/mailq\-\(.*\).*'`

        if $MAIL "bcnu ALERT" $EMAIL <$q
                then
                        rm $q
        else
                echo "$STAMP agent(alert): unable to send email to ($EMAIL)"
        fi
done

#set -x
# check for queued win32 messages to be sent
for q in `ls $BCNUHOME/flags/win32q-* 2>/dev/null`
do
        w32=`expr "$q" :  '.*/flags/win32q\-\(.*\).*'`
	if [ ! -r $BCNUHOME/data/win32alert/$w32 ]
	then
		continue
	fi
	w32host=`cat $BCNUHOME/data/win32alert/$w32`
        printf "\nsee http://$BCNUHOST$BCNUURL for details"  >>$q
	if 	$BCNU -a send -f $q $w32host
	then
		rm $q
	else
		echo "$STAMP agent(win32alert): host ($w32 - $w32host) not responding - disabled"
		rm $BCNUHOME/data/win32alert/$w32
	fi
done

if [ -f $BCNUHOME/flags/buildweb.now ]
   then
        rm $BCNUHOME/flags/buildweb.now
        BW=1
fi

if [ $BW ]
   then
        $BCNUHOME/scripts/buildweb -ad
fi
