#!/bin/tcsh -fx
#-------------------------------------------------------------------------------
# crds_sendEmail.csh
#
# sends an email reporting the status of the specified CRDS Catalog delivery
# NOTE: no logging; return status to caller and let caller log messages
#
# History:
# 2013-04-01 74512  LThompson   initial version based on cdb_mail.csh
#-------------------------------------------------------------------------------


# define script exit statusus
set emailSent = 0   # success
set emailFail = 1   # failure

printf "DEBUG: in crds_sendEmail.csh\n"

# get arguments
if( $#argv != 4 ) then
   # if call is via opusutil.runCmd, caller gets stdout_msg
   set msg = "Incorrect number of params; usage is: $0 <delivery #> <status>"
   printf $msg
   exit $emailFail
endif

set deliveryNum = $argv[1]
set deliveryStatus = $argv[2]
set catFileName = $argv[3]
set mailList = $argv[4]
printf "DEBUG: received $deliveryNum , $deliveryStatus"
printf "       $catFileName , $mailList\n"

if( $mailList == "" ) then
   set msg = "Missing mail list; don't know who to email\n"
   printf $msg
   exit $emailFail
endif

# set up subject
if( $deliveryStatus == 0 ) then
   set subject = "Delivery $deliveryNum"
else
   set subject = "Delivery $deliveryNum FAILED"
endif

printf "DEBUG: subject = $subject\n"

# send email
printf "DEBUG: sending email"
cat $catFileName | mailx -s "$subject" $mailList

# exit with appropirate status
if( $status != 0 ) then
  exit $emailFail
endif

exit $emailSent
