#!/bin/bash
#
#-------------------------------------------------------------------------------
# FTPs files from the HMC efix ftp server
#
# Possible return codes are:
#
# 0 - no error
# 1 - usage error, invalid argument
# 2 - invalid userID/password combination
# 3 - source file not found on server
# 4 - 'ftp' connection error
# 5 - "other" 'ftp' error
# 6 - target file not written to HMC
#
#-------------------------------------------------------------------------------
#

function exit_cleanup {
#   rm -f $FTP_LOG_FILE
   rm -f $TMP_FTP
   chmod 664 $FTP_LOG_FILE
   exit $1
}

function ftpRemoteFiles {
   FTP_SERVER=$1
   REMOTE_FILE=$2
   REMOTE_DIR=`/usr/bin/dirname ${REMOTE_FILE}`
   FILE=`basename ${REMOTE_FILE}`
   USER=$3
   PASS=$4
   >$FTP_LOG_FILE 
   # remove any prior files in download directory first. Create the directory if it does not exist
   if [ -d $INSTALL_DIR/ ]; then
      rm -rf $INSTALL_DIR/*
   else
      mkdir -p $INSTALL_DIR > /dev/null
      chmod 777 $INSTALL_DIR
   fi
   echo "host $FTP_SERVER" > $TMP_FTP
   echo "user $USER" >> $TMP_FTP
   echo "pass $PASS" >> $TMP_FTP
   LANG=en_US $FTP_CMD -f $TMP_FTP -d $FTP_LOG_FILE -V -t $TIMEOUT -o useMDTM=0 $INSTALL_DIR $REMOTE_DIR/$FILE
   return $?
} # end function getRemoteFiles

#-------------------------------------------------------------------------------
# "Main" program start   
#-------------------------------------------------------------------------------
FTP_LOG_FILE=/var/hsc/log/ftp.log
FTP_CMD=/usr/bin/ncftpget
TMP_FTP=/tmp/ftp_file_$$
>$TMP_FTP
chmod 600 $TMP_FTP
TIMEOUT=60
INSTALL_DIR=/dump/hsc_install.images

if [ "$1" = "" ]
then
   echo "Usage: getRemoteFiles <ftp server> <remote file> <user> <password>"
   exit_cleanup 1
fi

#-------------------------------------------------------------------------------
# 'ftp' the file from service server
#-------------------------------------------------------------------------------
ftpRemoteFiles $1 $2 $3 $4 
rc=$?

# good transfer, now see if the file actually got written to the HMC
if [[ $rc -eq 0 && -f /dump/hsc_install.images/$FILE ]]
then
   chmod 777 /dump/hsc_install.images/$FILE
   exit_cleanup 0
else

   # now have to search through FTP-Log file
   # look for "Transfer complete", "Successful transfer" or equivalent msg code
   # careful on grep'ing for all these values - note the space
   grep -q "^226" $FTP_LOG_FILE 2>&1 >/dev/null
   if [ $? -ne 0 ]
   then
     # ftp failed for some reason - find out why
     
     # file name not found on server
     grep -q "^550" $FTP_LOG_FILE 2>&1 >/dev/null
     if [ $? -eq 0 ]
     then
        exit_cleanup 3
     fi

   
     grep -q "^530" $FTP_LOG_FILE 2>&1 >/dev/null
     if [ $? -eq 0 ]
     then
        exit_cleanup 2
     fi
     # special case for i5 OS FTP
     grep -q "^250" $FTP_LOG_FILE 2>&1 >/dev/null
     if [ $? -eq 0 ]
     then
	exit_cleanup 0
     fi

     ping -c 1 $FTP_SERVER >/dev/null 2>&1
     if [ $? -ne 0 ]
     then
        ping6 -c 1 $FTP_SERVER >/dev/null 2>&1
        if [ $? -ne 0 ]
        then
	  exit_cleanup 4
        fi
     fi

     grep -q -i "Connection timed out" $FTP_LOG_FILE 2>&1 >/dev/null
     if [ $? -eq 0 ]
     then
        exit_cleanup 4
     fi

     grep -q -i "Connection refused" $FTP_LOG_FILE 2>&1 >/dev/null
     if [ $? -eq 0 ]
     then
        exit_cleanup 4
     fi

     grep -q "^230" $FTP_LOG_FILE 2>&1 >/dev/null
     if [ $? -ne 0 ]
     then
        exit_cleanup 2
     fi
   
     errRC=$(grep -q -i "Not connected" $FTP_LOG_FILE)
     if [ -n "$errRC" ]
     then
        exit_cleanup 4
     else
        # none of the above errors
        exit_cleanup 5
     fi
   else
      exit_cleanup 6
   fi
fi
exit_cleanup 0

