#!/bin/sh

#*****************************************************************
# Copyright  :  Smaser AG
# Filename   :  err_mail
# Purpose    :  To send mail to users when a batch process fails
# Parameters :  $1 = Messagefile
# 		$2 = Logfile
#*****************************************************************

if [ -f /usr/bin/uname ] ; then
	uname=/usr/bin/uname
else
	uname=uname
fi

if [ `$uname -s` = SunOS ] ; then
	OS=sun
elif [ `$uname -s` = AIX ] ; then
	OS=aix
elif [ `$uname -s` = HP-UX ] ; then
	OS=hp
elif [ `$uname -s` = OSF1 ] ; then
	OS=osf
else
	OS=unknown
fi

if [ -f /usr/bin/dirname ] ; then
	dirname=/usr/bin/dirname
else
	dirname=dirname
fi

if [ -f /usr/bin/awk ] ; then
	awk=/usr/bin/awk
else
	awk=awk
fi

if [ -f /usr/bin/mail ] ; then
	mail=/usr/bin/mail
else
    which sendmail >/dev/null 2>&1
    if [ $? -eq 0 ]; then
            mail=sendmail
    else
            mail=mail
    fi
fi

if [ -f /usr/bin/cat ] ; then
	cat=/usr/bin/cat
else
	cat=cat
fi

if [ -f /usr/bin/rm ] ; then
	rm=/usr/bin/rm
else
	rm=rm
fi

if [ -f `$dirname $0`/queryrcs ] ; then
	queryrcs=`$dirname $0`/queryrcs
else
	queryrcs=queryrcs
fi

whom=`$queryrcs TDBS_ERRMAILST | $awk -F\" '{print $2}'`
if [ "$whom" = "" -o "$whom" = "<nil>" ]
then
	# No error mail list specified. Do not send
	# anything to the current user - just exit.
        $rm -f $1
	exit 0
else
	whom=`echo $whom | sed 's/,/ /g'`
fi

pid=$$
$cat $1 > $HOME/mailtmp$pid
echo "" >> $HOME/mailtmp$pid
echo "****     Please consult the log file : " >> $HOME/mailtmp$pid
echo "****      $2" >> $HOME/mailtmp$pid
echo "****     for more detail." >> $HOME/mailtmp$pid

for I in $whom
do
    if [ "$OS" = "aix" ]
    then
	$mail -s "Error from TRIP batch process" $I < $HOME/mailtmp$pid
    else
 	$mail $I < $HOME/mailtmp$pid
    fi
done
$rm -f $HOME/mailtmp$pid
$rm -f $1

exit 0
