#!/bin/ksh
#set -x
# 7/28/06 PEYOUN. This script unzips, decrypts, and FTP's the BAMH file provided by HSBC to the Mainframe for subsequent processing.
#
##
## NOTE: FILE and UNZIP_FILE are only good up until the end of 2009,
## at which time the hard-coded 0 will need to be incremented by 1.
##
DIR=/home/gmcs_team
cd $DIR
YMM=`echo $(date +%y%m) | cut -c 2-4`
((DATA_MONTH=$YMM-1))
##overriding DATA_MONTH because files are old. Still catching up from 0601 data through the present.
##once we're caught up we can remove the override.
DATA_MONTH=602
FILE=`ls AXEM.BM0${DATA_MONTH}OT.zip`
##
##
##
if [[ ! -a ${FILE} ]]; then
echo "BAMH input file does not exist on cwyeta01 yet. Quitting..."
exit 1
fi
LOG_DIR=~/log
cd $LOG_DIR
LOG_NAME="$(basename $0 .ksh)"_$(date +%m%d%Y).log
exec 1>$LOG_NAME
exec 2>&1
Script_Name=$(basename $0)
echo "$Script_Name has been initiated"
echo "LOG_DIR=$LOG_DIR"
ODRIVE=General_Motors/GM_Card/Production/Monthly/HSBC_BAMH/Logs
echo "ODRIVE=$ODRIVE"
echo "FILE=$FILE"
echo "YMM=$YMM"
echo "DIR=$DIR"
DATE=$(date +%m%d%Y)
echo "DATE=$DATE"
DATE2=$(date +%m%d%y)
echo "DATE2=$DATE2"
DSN=BAMH.IN
echo "DSN=$DSN"
PUSER=#####
echo "PUSER=$PUSER"
PPASS=#######
USER=######
echo "User=$USER"
PASS=#######
RECFM=FB
echo "RECFM=$RECFM"
integer LRECL=256
echo "LRECL=$LRECL"
integer BLKSIZE=32512
echo "BLKSIZE=$BLKSIZE"
UNIT=ACS2
echo "UNIT=$UNIT"
integer VCOUNT=20
echo "VCOUNT=$VCOUNT"
RETPD=90
echo "RETPD=$RETPD"
FILE_NAME=HSBCemail_${DATE2}_
echo "FILE_NAME=$FILE_NAME"
PROCESS="BAMH"
echo "PROCESS=$PROCESS"
DECRYPT_FILE=${FILE_NAME}${PROCESS}.txt
echo "DECRYPT_FILE=$DECRYPT_FILE"
DIR=/home/gmcs_team
echo "DIR=$DIR"
LOG=/home/gmcs_team/log
echo "LOG=$LOG"
EMAIL_SUPPORT=############@$######.com
echo EMAIL_SUPPORT="$EMAIL_SUPPORT"
# override while debugging
EMAIL_SUPPORT=peyoun@######.com
integer HOLDFILESIZE=0
echo "HOLDFILESIZE=$HOLDFILESIZE"
MFFILE="'ACXI.S08.C01028.F03108.PYU31.${DSN}'"
echo "MFFILE=$MFFILE"
integer FILESIZE=0
echo "FILESIZE=$FILESIZE"
integer DIFF=0
echo "DIFF=$DIFF"
integer I=0
echo "I=$I"
SUBJECT="BAMH Process Log File Created"
MESSAGE="$Script_Name has completed on ########. See ########################################################### for a complete log of this job."
#integer NUMRECS=`ls $FILE | awk -F '' '{print $7}' | awk -F '.' '{print $1}'` #integer NUMRECS=`ls $FILE | awk -F '' '{print $7}' | awk -F '.' '{print $1}'`
cd $DIR
# potentially infinite loop!
while :
do
FILESIZE=`ls -l $FILE | awk '{print $5}'`
echo "FILESIZE=$FILESIZE"
(( DIFF = $FILESIZE - $HOLDFILESIZE ))
if (( $DIFF != 0 )); then
(( HOLDFILESIZE=$FILESIZE ))
echo "HOLDFILESIZE=$HOLDFILESIZE"
(( I= $I + 1 ))
echo "I=$I"
if (( $I > 1 ));then
echo "$PROCESS file - $FILE processed by $Script_Name has not been completely FTP'd to ######## ${DIR}. Waiting....."
echo "$PROCESS file - $FILE processed by $Script_Name has not been completely FTP'd to ######## ${DIR}. Waiting....." | mailx -s "$PROCESS file - $FILE has not been completely FTP'd to cwyeta01 $DIR. Waiting 1 minute ....." $EMAIL_SUPPORT
fi
if (($I > 20));then
echo "$PROCESS file - FTP loop for $FILE in script $Script_Name exceeded 20 iterations. Possible infinite loop - breaking"
mailx -s "$PROCESS - FTP loop for $FILE in script $Script_Name exceeded 20 iterations. Possible infinite loop - breaking" $EMAIL_SUPPORT
break
fi
sleep 60
else
echo "$PROCESS file - ${DIR}${FILE} has been completely FTP'd to ########. Initiating unzip/decrypt/gzip/FTP to mainframe.."
echo "$PROCESS file - ${DIR}${FILE} has been completely FTP'd to ########. Initiating unzip/decrypt/gzip/FTP to mainframe.." | mailx -s "$PROCESS file $FILE has been completely FTP'd to cwyeta01 $DIR. Initiating unzip/decrypt/gzip/FTP to mainframe.." $EMAIL_SUPPORT
break
fi
done
#unzip file
echo "Initiating unip of $FILE"
unzip -o $FILE
RC=$?
if (( $RC != 0 )); then
echo "$PROCESS - Error in unzip command for $FILE....investigate."
echo "$PROCESS - Error in unzip command for $FILE....investigate." | mailx -s "$PROCESS - Error in unzip" $EMAIL_SUPPORT
else
echo "$PROCESS - Unzip of $FILE completed successfully."
fi
#echo "$(date +%T) - Sleeping for 5 minutes"
#sleep 300
UNZIP_FILE=`ls AXEM.BM0${DATA_MONTH}OT.conv.*`
echo "UNZIP_FILE=$UNZIP_FILE"
#decrypt file
sudo -u gpg /usr/bin/gpg --homedir=/home/gpg/.gnupg --no-tty --decrypt <${UNZIP_FILE}> ${DECRYPT_FILE}
set -x
RC=$?
if (( $RC != 0 )); then
echo "error in decrypting $PROCESS - file $UNZIP_FILE on ########. Investigate...."
echo "error in decrypting $PROCESS - file $UNZIP_FILE on ########. Investigate...." | mailx -s "$PROCESS file - error in decryption. Investigate..." $EMAIL_SUPPORT
exit 1
else
echo "$UNZIP_FILE decrypted successfully to file ${DECRYPT_FILE}. Initiating gzip of $DECRYPT_FILE"
gzip $DECRYPT_FILE
#echo "$(date +%T) - Sleeping for 15 minutes"
#sleep 900
RC=$?
if (( $RC != 0 )); then
echo "Error in gzip command within $PROCESS....investigate."
echo "Error in gzip command within $PROCESS....investigate." | mailx -s "$PROCESS - failed within gzip step" $EMAIL_SUPPORT
else
echo "gzip of $DECRYPT_FILE complete. Initiating FTP for ${DECRYPT_FILE}.gz"
fi
# initiate file transfer
ftp -v -n cpu3 <<-!
binary
quote user $USER
quote pass $PASS
quote site RECFM=$RECFM LRECL=$LRECL BLKSIZE=$BLKSIZE UNIT=$UNIT VCOUNT=$VCOUNT RETPD=$RETPD
put ${DECRYPT_FILE}.gz $MFFILE
bye
!
# done with FTP
# ensure FTP transmitted successfully
grep -q "Data bytes written:" $LOG_NAME
RC2=$?
if (( $RC2 == 0 )); then
# capture record count
((NUMRECS=`grep "Data bytes written:" ${LOG_NAME} | awk '{print $4}' | tr -d '.' | tr -d '\r'`/$LRECL))
# integer NUMBYTES=`grep "Data bytes written:" ${LOG_NAME} | awk '{print $4}' | tr -d '.' | tr -d '\r'`
#echo "NUMBYTES=$NUMBYTES"}
#(( NUMRECS=$NUMBYTES/$LRECL ))
#echo "NUMRECS=$NUMRECS"
NUMRECSF=$(echo $NUMRECS | sed -e :a -e 's/^\([^.]*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta')
echo "NUMRECSF=$NUMRECSF"
echo "$Script_Name has completed FTP to mainframe. You can expect $MFFILE with $NUMRECSF records to be on the mainframe."
echo "$Script_Name has completed FTP to mainframe.. You can expect $MFFILE with $NUMRECSF records to be on the mainframe." | mailx -s "$Script_Name has FTP'd BAMH file to mainframe" $EMAIL_SUPPORT
else
echo "$Script_Name has failed within the FTP step to the mainframe."
echo "$Script_Name has failed within the FTP step to the mainframe." | mailx -s "$PROCESS FTP failure" $EMAIL_SUPPORT
fi
fi
#FTP log file to midway
ftp -nv midway <<-!
quote user $PUSER
quote pass $PPASS
cd $ODRIVE
pwd
put ${LOG}/$(basename $0 .ksh)"$(date +%m%d%Y).log $(basename $0 .ksh)"$(date +%m%d%Y).log
ls
bye
!
# notify users that BAMH log file is on O Drive
echo "$MESSAGE" | mailx -s "$SUBJECT" $EMAIL_SUPPORT
## end of job cleanup
mv -f $FILE ${FILE}.pulled
echo "$FILE has been renamed to ${FILE}.pulled"
rm -f $UNZIP_FILE
echo "$UNZIP_FILE has been deleted from $DIR"
rm -f ${DECRYPT_FILE}.gz
echo "${DECRYPT_FILE}.gz has been deleted from $DIR"
exit 0