#!/bin/sh -x
#
# routine: 	NETCPY.SH
#
# purpose:	Script file which can be run as a pipeline process
#		in order to automate retrieval of files from a
#		remote host.
#
# modification history:
#
#   date    opr     who	    reason
# -------- -----  --------  --------------------------------------	
# 05/26/98 35570  MSwam     port to UNIX
# 06/28/10 45189  MSwam     use netcopy/SFTP
#
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo $banner
date
#
# define error/exit status values
all_is_well=1
netcpy_success=3
netcpy_no_files=5
netcpy_files_missing=7
netcpy_copy_failed=9
netcpy_usage_error=11
netcpy_insuff_disk=13
netcpy_failed_delete=15
go_absent=102
#
# run the netcopy file transfer tool
netcopy -pa $PATH_FILE -pr netcpy
savestat=$?
#
# exit if no files found (send no e-mail either)
if [ $savestat = $netcpy_no_files ]
then
   echo $banner
   exit $all_is_well
fi
#
# Send a mail message indicating the status of the NETCPY run.
#-- create the mail message based on the NETCPY exit status
#
mailfile=$NETCPY_MAIL_FILE
estatus=$all_is_well

echo  "NETCPY FILE TRANSFER  `date`" >> $mailfile
echo  " " >> $mailfile
if [ $savestat = $netcpy_success ]
then
   echo  "Files copied SUCCESSFULLY." >> $mailfile
   mail_subject="NETCPY: SUCCESS"
elif [ $savestat = $netcpy_files_missing ]
then 
   echo  "Catalog(s) found but files were MISSING. Check process logfile." >> $mailfile
   mail_subject="NETCPY: Files MISSING"
elif [ $savestat = $netcpy_copy_failed ]
then 
   echo  "Copy FAILED. Check process log file." >> $mailfile
   mail_subject="NETCPY: Copy FAILED"
elif [ $savestat = $netcpy_usage_error ]
then 
   echo  "NETCPY ABSENT! USAGE ERROR. Check process log file." >> $mailfile
   mail_subject="NETCPY: USAGE error"
elif [ $savestat = $netcpy_insuff_disk ]
then 
   echo  "NETCPY ABSENT! INSUFFICIENT DISK SPACE. Check process log file." >> $mailfile
   mail_subject="NETCPY: INSUFF DISK SPACE"
elif [ $savestat = $netcpy_failed_delete ]
then 
   echo  "NETCPY ABSENT! FILE DELETION FAILED. Check process log file." >> $mailfile
   mail_subject="NETCPY: File deletion FAILED"
else
   echo  "UNEXPECTED exit status from NETCPY; $savestat " >> $mailfile
   mail_subject="NETCPY: UNEXPECTED ERROR"
   # an unexpected error causes the process to go absent
   estatus=$go_absent
fi
#
/bin/mailx -s \"$mail_subject\" $NETCPY_MAIL_ADDRESS < $mailfile
if [ $? != 0 ]
then
 	echo "ERROR sending mail about NETCPY results."
 	echo "      Check process log file."
fi
#
# cleanup
/bin/rm $mailfile
echo $banner
exit $estatus
