#!/usr/bin/ksh

#-------------------------------------------------
#emails batch_log when done (good or bad outcome)
#syntax: mail_script batchdir rsh_flag
# $1 = batchdir
# $2 = rsh_flag
# $3 = error or completion string
# $4 = uuencoded file to be sent (includes images and text)
#-----------------------------------------------
# set some defaults
#-----------------------------------------------
#--------------------------------
#set environment variables
#--------------------------------
. $WFU_PATH/global.site.ksh

cmd=mailx 
#---------------------------------------------------
#you really need to test if mailserver OS is Linux
#---------------------------------------------------
mail_os=$OS

if test $rsh_flag -eq 1
	then mail_os=`rsh $mailserver uname`
fi

if test $mail_os = Linux
	then cmd=mail
fi

mailfile=$1/batch_log
if test $# -eq 4
	then mailfile=$4
fi


#----------------------------------------------------
#check if the email address is in the batch folder
#----------------------------------------------------
batchdir=$1
email_flag=`cat $batchdir/batch_email 2>/dev/null`
if (test ${#email_flag} -gt 0) then 
	email_address=$email_flag
fi



#------------------
#mail the batchlog
#------------------
if test $mail_flag -eq 1
	then batchdir=$1
	batchlog=$batchdir/batch_log
	if test $rsh_flag -eq 0 
		then
		sendmail=`$cmd  -s  $3:$batchlog $email_address<$mailfile`
	fi
	if test $rsh_flag -eq 1 
		then sendmail=`rsh $mailserver $cmd -s $3:$batchlog $email_address<$mailfile`
	fi
	#echo $sendmail
fi

exit

