#!/bin/sh
# UPDERROR
#-----------------------------------------------------------------
#! Report errors in UPDATE job
## Update UNIX
#-----------------------------------------------------------------------
#;  Copyright (C) 1995-1996, 2003, 2006, 2009, 2021, 2023
#;  Associated Universities, Inc. Washington DC, USA.
#;
#;  This program is free software; you can redistribute it and/or
#;  modify it under the terms of the GNU General Public License as
#;  published by the Free Software Foundation; either version 2 of
#;  the License, or (at your option) any later version.
#;
#;  This program is distributed in the hope that it will be useful,
#;  but WITHOUT ANY WARRANTY; without even the implied warranty of
#;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#;  GNU General Public License for more details.
#;
#;  You should have received a copy of the GNU General Public
#;  License along with this program; if not, write to the Free
#;  Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
#;  MA 02139, USA.
#;
#;  Correspondence concerning AIPS should be addressed as follows:
#;         Internet email: aipsmail@nrao.edu.
#;         Postal address: AIPS Project Office
#;                         National Radio Astronomy Observatory
#;                         520 Edgemont Road
#;                         Charlottesville, VA 22903-2475 USA
#-----------------------------------------------------------------------
#   Mails the error log to listed people
#
#   Inputs
#      ErrorFile         error log
#      ErrorList         people to recieve error message
#      ClientName        client machine
#      VerName           AIPS version
#      RunTime           date
#-----------------------------------------------------------------
[ ! -f $ErrorFile ] && echo "UPDERROR: No error file $ErrorFile" \
                            "found!" >> $ErrorFile
#
#                                       Mail to me if no-one else
[ "$MailList" = "" ] && MailList="$LOGNAME"
if [ "$MailList" != "" ] ; then
   subj="$ClientName/$ARCH $RunTime update errors for $VerName"
   sendmail="/usr/lib/sendmail"
   [ ! -x $sendmail ] && sendmail="/usr/sbin/sendmail"
   if [ ! -x $sendmail ] ; then
     echo "Cannot find sendmail (/usr/sbin or /usr/lib)"
     echo "so AIPS error report is shown here"
     echo " -------------------------------------------"
     echo "From: $LOGNAME"
     echo "To: $MailList"
     echo "Subject: $subj"
     echo ""
     cat $ErrorFile
   else
     tmpnam=UPERROR.$$
     /bin/rm -f $tmpnam
     echo "From: $LOGNAME"  >$tmpnam
     echo "To: $MailList"  >>$tmpnam
     echo "Subject: $subj" >>$tmpnam
     echo "X-Mailer: AIPS Midnight Job ERROR report" >>$tmpnam
     echo "" >>$tmpnam
     cat $ErrorFile >>$tmpnam
     cat $LogFile >>$tmpnam
     cat $tmpnam | $sendmail -f "aipsmgr@nrao.edu" -oi -B8BITMIME -t
     /bin/rm -f $tmpnam
   fi
fi
