#!/bin/bash

#
# Simple script to list the contents of remote FTP server
# directory and redirect into output file (for subsequent Java
# parsing.  See HMCRestoreTasklet.
#


#---------------------------------------------------------------------------------
# check contents of ftp log file when sending data
#---------------------------------------------------------------------------------
function checkForFtpDataTransferErrors {

    FTP_INFO=$1
    funcRC=0

    # First, test if the file transfer succeeded, then if not, check for
    # any specific errors (login, etc)
    #
    # Look for "Transfer complete", "Successful transfer" or equivalent msg
    # code. Careful on grep'ing for this value - note the space
    grep -i "^226 " $FTP_INFO 2>&1 >/dev/null
    if [ $? -eq 0 ]; then
        # successful file transfer, remove the log
        funcRC=0
    else
        # special case for i5 OS FTP
        grep -i "^250 " $FTP_INFO 2>&1 >/dev/null
        if [ $? -eq 0 ]; then
            funcRC=0
        else
            # If no file transfer, look for (known) errors
            grep -i "not logged in" $FTP_INFO > /dev/null 2>&1
            if [ $? -eq 0 ]; then
                # Oops - error logging in
                funcRC=21
            else
                grep -i "Connection refused" $FTP_INFO > /dev/null 2>&1
                if [ $? -eq 0 ]; then
                    # Oops - error accessing server
                    funcRC=23
                else
                    grep -i "unknown host" $FTP_INFO > /dev/null 2>&1
                    if [ $? -eq 0 ]; then
                        # Oops - error accessing server
                        funcRC=24
                    else
                        grep -i "Not connected" $FTP_INFO > /dev/null 2>&1
                        if [ $? -eq 0 ]; then
                            # Oops - error accessing server
                            funcRC=25
                        else
                            # one more login check...
                            grep -i "Login failed" $FTP_INFO > /dev/null 2>&1
                            if [ $? -eq 0 ]; then
                                # Oops - error logging in
                                funcRC=22
                            else
                                # assuming past these login checks, now check for successful file xfer
                                grep -i "Permission denied" $FTP_INFO > /dev/null 2>&1
                                if [ $? -eq 0 ]; then
                                    # permission issues
                                    funcRC=26
                                else
                                    # All other errors here
                                    funcRC=27
                                fi
                            fi 
                        fi 
                    fi
                fi
            fi   
        fi
    fi
    
    echo "Exiting checkForFtpDataTransferErrors, funcRC = $funcRC." >> $LOG
    return $funcRC
}  


#---------------------------------------------------------------------------------
# check contents of ftp log file when changing directories
#---------------------------------------------------------------------------------
function checkForFtpChdirError {

    FTP_INFO=$1
    funcRC=0

    # First check for the '550' error code indicating the 'cd' comamnd failed.
    # Then check for the connection/login errors
    grep -i "^250 " $FTP_INFO 2>&1 >/dev/null
    if [ $? -eq 0 ]; then
        # Directory change ok
        funcRC=0
    else
        # Look for (known) errors
        grep -i "not logged in" $FTP_INFO > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            # Oops - error logging in
            funcRC=21
        else
            grep -i "Connection refused" $FTP_INFO > /dev/null 2>&1
            if [ $? -eq 0 ]; then
                # Oops - error accessing server
                funcRC=23
            else
                grep -i "unknown host" $FTP_INFO > /dev/null 2>&1
                if [ $? -eq 0 ]; then
                    # Oops - error accessing server
                    funcRC=24
                else
                    grep -i "Not connected" $FTP_INFO > /dev/null 2>&1
                    if [ $? -eq 0 ]; then
                        # Oops - error accessing server
                        funcRC=25
                    else
                        # one more login check...
                        grep -i "Login failed" $FTP_INFO > /dev/null 2>&1
                        if [ $? -eq 0 ]; then
                            # Oops - error logging in
                            funcRC=22
                        else
                            # assuming past these login checks, now check for successful file xfer
                            grep -i "Permission denied" $FTP_INFO > /dev/null 2>&1
                            if [ $? -eq 0 ]; then
                                # permission issues
                                funcRC=26
                            else
                                # last check is for the 'cd' error
                                grep -i "^550 " $FTP_INFO 2>&1 >/dev/null
                                if [ $? -eq 0 ]; then
                                    funcRC=28
                                else
                                    # All other errors here
                                    funcRC=27
                                fi
                            fi
                        fi 
                    fi 
                fi
            fi
        fi   
    fi
    
    echo "Exiting checkForFtpChdirError, funcRC = $funcRC." >> $LOG
    return $funcRC
}


# function to list the contents of ftp site to log file
function generateFtpListFile {
    SERVER=$1
    USER=$2
    PASSWORD=$3
    DIRECTORY=$4

    FTP_INFO=/var/hsc/log/ftp.info
    
    if [[ "$DIRECTORY" == "" || "$DIRECTORY" == "." ]]; then
        # no remote directory specified
        echo "archive file query does not require a change of directory." >> $LOG
        /usr/kerberos/bin/ftp -n -u -v $SERVER <<EOF 2>&1 > $FTP_INFO
user $USER $PASSWORD
ls
quit
EOF
        # Check for errors during the transfer
        checkForFtpDataTransferErrors $FTP_INFO
        funcRC=$?
    else
        # case here where remote target directory is specified
        echo "archive file query requires a change to directory named <$DIRECTORY>." >> $LOG
        
        /usr/kerberos/bin/ftp -n -u -v $SERVER <<EOF 2>&1 > $FTP_INFO
user $USER $PASSWORD
cd $DIRECTORY
quit
EOF
        # First check for 'cd' errors (and the usual connection/login errors...)
        checkForFtpChdirError $FTP_INFO
        funcRC=$?
        
        if [ $funcRC == 0 ]; then
            # repeat ftp with actual data transfer
            /usr/kerberos/bin/ftp -n -u -v $SERVER <<EOF 2>&1 > $FTP_INFO
user $USER $PASSWORD
cd $DIRECTORY
ls
quit
EOF
            # now check for data transfer errors
            checkForFtpDataTransferErrors $FTP_INFO
            funcRC=$?
        fi
    fi    
    
    return $funcRC
}
# --------------  End Subroutines --------------------------------------------



# --------------- BEGIN MAIN PROGRAM  ----------------------------------------

# inputs to this routine are:
#
# $1 - retsore type (not used)
# $2 - ftp server
# $3 - user ID
# $4 - password
# $5 - remote directory (optional)

#
# Just in case we have NLS troubles reading system information...
#
LANG=en_US
export LANG

LOG=/var/hsc/log/listRemoteArchive.log

# Start log to record backup actions.
echo -e "List remote archive on ftp server log for `date`, server = $2, user ID = $3, directory = <$5>.\n" > $LOG

generateFtpListFile $2 $3 $4 $5
rc=$?

exit $rc
