#! /usr/bin/ksh 
# ===========================================================
#  arraymail                                                =
#                                                           =
#  HP disk array monitor daemon error notification script   =
#                                                           =
#  syntax:                                                  =
#     arraymail <name of error message file>                =
#               <listing of $MISC_DIR/arraymon.dest>        =
#                                                           =
#  arguments: mail flag - either TRUE or FALSE - indicates  =
#                         whether or not to e-mail the      =
#                         error message. If not e-mailed,   =
#                         then only send message to         =
#                         designated terminal.              =
#                                                           =
#             error message file, locally created by caller =
#             contents of file $MISC_DIR/arraymon.dest      =
#                  - screen to write msg to                 =
#                  - addresses to mail msg to               =
#             if arraymon.dest is missing, write error      =
#                  message to /dev/console and              =
#                  mail to root\@`hostname`                 =
#                                                           =
#  utility programs                                         =
#               $DAEMON_DIR/aml                             =
#                                                           =
#  system utilities                                         =
#               $CMNDS_DIR/cat                              =
#               $CMNDS_DIR/echo                             =
#               $CMNDS_DIR/mktemp                           =
#               $CMNDS_DIR/rm                               =
#               $CMNDS_DIR/mail                             =
#                                                           =
#                                                           =
#  (c)Copyright 1993, 1995 Hewlett-Packard Company          =
#     All rights reserved.                                  =
#                                                           =
# ===========================================================

hdr="@(#) $Header: arraymail,v 78.1 96/02/09 06:12:57 ssa Exp $"

. /usr/lbin/hpC2400/arraymon.hdr

#-----------------------------------------------
# intialize message catalog error numbers
#-----------------------------------------------

   Mailset=5
   ScreenFail=1
   Mailuse=2

   if [ $# -lt 2 ]
   then 
   
      notice=$(${CMNDS_DIR}/mktemp -d ${TMP_DIR} -c)

      ${DAEMON_DIR}/aml -s $Mailset -m $Mailuse -f $notice
      ${CMNDS_DIR}/cat $notice > /dev/console 2> /dev/null
      ${CMNDS_DIR}/mail root@`hostname` < $notice > /dev/null 2>&1
      ${CMNDS_DIR}/rm $notice 2> /dev/null
      exit 1 

   fi

   mflag=$1
   shift

   errfile=$1
   shift

   if [ $# -eq 0 ]
   then

      screen=/dev/console

      if [ $mflag -eq $FALSE ]
      then
         mail_dest="root@`hostname`" 
         ${CMNDS_DIR}/mail  $mail_dest < $errfile > /dev/null 2>&1
      fi

   else

      screen=$1
      shift

   fi


#------------------------------------------------------------
# =====  MAIN  ===== 
#------------------------------------------------------------

   if [ $mflag -eq $FALSE ]
   then
     while [ $# -gt 0 ]
     do

#------------------------------------------------------------
# loop through the names in the file mailing "$errfile" 
# if mail fails, mailer daemon will send a copy of the   
# undeliverable message back to root with error info     
#------------------------------------------------------------
        mail_dest=$1
        ${CMNDS_DIR}/mail  $mail_dest < $errfile > /dev/null 2>&1
        shift

     done
   fi

#------------------------------------------------------------
# after mail loop, cat info to desired $screen
# if cat fails, notify root by mail and send info to console
#------------------------------------------------------------

   ${CMNDS_DIR}/cat $errfile > $screen 2> /dev/null

   if [ $? -ne 0 ]
   then
      notice=$(${CMNDS_DIR}/mktemp -d ${TMP_DIR} -c)
      host_n=$(${CMNDS_DIR}/uname -n)
      ${DAEMON_DIR}/aml -s $Mailset -m $ScreenFail -L $screen -n $host_n -f $notice
      ${CMNDS_DIR}/cat $errfile >> $notice
      ${CMNDS_DIR}/cat $notice > /dev/console 2> /dev/null
      ${CMNDS_DIR}/mail root@`hostname` < $notice > /dev/null 2>&1
      ${CMNDS_DIR}/rm $notice 2> /dev/null
      exit 1
   fi

#------------------------------------------------------------
# end of arraymail
#------------------------------------------------------------

