#!/bin/bash

#
# Simple script to offload a dump file to an ftp serevr
#  format: ftpDumpToServer <dump filename> <ftp server> <user id> <password> <optional directory>
#
exit_cleanup() {
    rm -f /var/hsc/log/ftp.log
    rm -f $TMP_FTP
    exit $1
}

testftp() {
    file=$1
    server=$2
    userid=$3
    password=$4
    dir=$5
    
if [ "$dir" != "" ]; then    
    /usr/lib/mit/bin/ftp -n -u $server <<EOF1 > /var/hsc/log/ftp.log 2>&1
    
user $userid $password
pwd
cd $dir
pwd
quit
EOF1

else    
    /usr/lib/mit/bin/ftp -n -u $server <<EOF2 > /var/hsc/log/ftp.log 2>&1
    
user $userid $password
quit
EOF2

fi

    # 
    # First quick check to see if we're even able to login
    #
    grep -i "not logged in" /var/hsc/log/ftp.log > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        # Oops - error logging in
        funcRC=7
        return $funcRC
    fi
       
    grep -i "Connection refused" /var/hsc/log/ftp.log > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        # Oops - error logging in
        funcRC=5
        return $funcRC
    fi   
         
    grep -i "unknown host" /var/hsc/log/ftp.log > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        # Oops - error logging in
        funcRC=5
        return $funcRC
    fi
       
    grep -i "Not connected" /var/hsc/log/ftp.log > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        # Oops - error logging in
        funcRC=5
        return $funcRC
    fi
       
    # one more check...
    grep -i "Login failed" /var/hsc/log/ftp.log > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        # Oops - error logging in
        funcRC=6
        return $funcRC
    fi 

    # if no directory was specified, we're done    
    if [ "$dir" == "" ]; then
        funcRC = 0
        return $funcRC
    fi 
            
    #                                                 
    # See if the 'cd' command with the ftp will work.
    #                                                 
    funcRC=8
    found_pwd=0
    while read line
    do
        echo $line | grep "^257 \"" > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            # found first match
            if [ $found_pwd -eq 0 ]; then
                CUR_DIR=`echo $line | awk '{print $2}'`
                HOME_DIR=`echo $CUR_DIR | sed 's/\"//g'`
                found_pwd=1
            else
                # found next match
                PWD_DIR=`echo $line | awk '{print $2}'`
                NEW_DIR=`echo $PWD_DIR | sed 's/\"//g'`
            
                echo $dir | grep "^/" > /dev/null 2>&1
                if [ $? -ne 0 ]; then
                    if [[ $HOME_DIR/$dir == $NEW_DIR || $HOME_DIR/$dir == "/"$NEW_DIR || $HOME_DIR/$dir == $NEW_DIR"/" || $HOME_DIR/$dir == "/"$NEW_DIR"/" ]]; then
                        funcRC=0
                    else
                        funcRC=8
                    fi
                else
                    if [ $dir == $NEW_DIR ]; then
                        funcRC=0
                    else
                        funcRC=9
                    fi
                fi
            fi
        fi
    done < /var/hsc/log/ftp.log
   
    return $funcRC
}


ftpToServer() {
    file=$1
    server=$2
    userid=$3
    password=$4
    dir=$5
    
    FTP_LOG=/var/hsc/log/ftp.log

    echo "host $server" > $TMP_FTP
    echo "user $userid" >> $TMP_FTP
    echo "pass $password" >> $TMP_FTP

    # Go to target directory
    cdir=`pwd`
    cd /dump

if [ "$dir" != "" ]; then
    LANG=en_US $FTP_CMD -f $TMP_FTP -d $FTP_LOG -V -t $TIMEOUT -o useMDTM=0 $dir $file
#    /usr/lib/mit/bin/ftp -n -u $server <<EOF3 > /var/hsc/log/ftp.log 2>&1
#    
#user $userid $password
#bin
#cd $dir
#put $file
#quit
#EOF3

else
    LANG=en_US $FTP_CMD -f $TMP_FTP -d $FTP_LOG -V -t $TIMEOUT -o useMDTM=0 . $file
#    /usr/lib/mit/bin/ftp -n -u $server <<EOF4 > /var/hsc/log/ftp.log 2>&1
#    
#user $userid $password
#bin
#put $file
#quit
#EOF4

fi
    # look for server filesytem full/error codes first
    grep -q -i "^452" $FTP_LOG 2>&1 >/dev/null || grep -q -i "^451" $FTP_LOG 2>&1 >/dev/null || grep -q -i "^552" $FTP_LOG 2>&1 >/dev/null
    if [ $? -eq 0 ]; then
        funcRC=29
    else
        # Requested action not taken. File name not allowed (permission errors?)
        grep -q -i "^553" $FTP_LOG 2>&1 >/dev/null
        if [ $? -eq 0 ]; then
            funcRC=50
        else
            # Requested action not taken. File unavailable (e.g., file not found, no access)
            grep -q -i "^550" $FTP_LOG 2>&1 >/dev/null
            if [ $? -eq 0 ]; then
                funcRC=28
            else
                # login check...
#                grep -q -i "Login failed" $FTP_LOG 2>&1 >/dev/null || grep -q -i "Login incorrect" $FTP_LOG 2>&1 >/dev/null || grep -q -i "Identification failed" $FTP_LOG 2>&1 >/dev/null
                grep -q -i "^530" $FTP_LOG 2>&1 >/dev/null
                if [ $? -eq 0 ]; then
                    # error logging in - bad user/password(?)
                    funcRC=22
                else
                    # no critical errors detected so far - see if operation succeeded.
                    grep -q -i "^226" $FTP_LOG 2>&1 >/dev/null || grep -q -i "^250" $FTP_LOG 2>&1 >/dev/null
                    if [ $? -eq 0 ]; then
                        funcRC=0
                        rm -f $FTP_LOG
                    else
                        # If no file transfer, look for (known) errors
                        grep -q -i "not logged in" $FTP_LOG > /dev/null 2>&1
                        if [ $? -eq 0 ]; then
                            # Oops - error logging in
                            funcRC=21
                        else
                            grep -q -i "Connection refused" $FTP_LOG > /dev/null 2>&1
                            if [ $? -eq 0 ]; then
                                # Oops - error accessing server
                                funcRC=23
                            else
                                grep -q -i "unknown host" $FTP_LOG > /dev/null 2>&1
                                if [ $? -eq 0 ]; then
                                    # Oops - error accessing server
                                    funcRC=24
                                else
                                    grep -q -i "Not connected" $FTP_LOG > /dev/null 2>&1
                                    if [ $? -eq 0 ]; then
                                        # Oops - error accessing server
                                        funcRC=25
                                    else
                                        # last check for permission issues
                                        grep -q -i "Permission denied" $FTP_LOG > /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
        fi
    fi
   
    # switch back to original directory
    cd $cdir
   
#    echo "Exiting ftpToServer, funcRC = $funcRC." >> $LOG
    return $funcRC
}

#--------------------Main Program--------------------------

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

FTP_CMD=/usr/bin/ncftpput
TIMEOUT=120

TMP_FTP=/tmp/ftp_file_$$
>$TMP_FTP
chmod 600 $TMP_FTP

DIR=$5

#
# if no filename specified, list choices, then exit
#
if [ "$1" = "" ]; then
    echo "No input filename specified. Files available to ftp:"
    echo
    a=`ls /dump/SYSDUMP* 2>/dev/null | wc | awk '{print $1}'`
    b=`ls /dump/FSPDUMP* 2>/dev/null | wc | awk '{print $1}'`
    c=`ls /dump/SMADUMP* 2>/dev/null | wc | awk '{print $1}'`
    d=`ls /dump/PWRDUMP* 2>/dev/null | wc | awk '{print $1}'`
    if [ $a -ne 0 ]; then
        cdir=`pwd`
        cd /dump
        ls SYSDUMP* 
        cd $cdir
    fi
    if [ $b -ne 0 ]; then
        cdir=`pwd`
        cd /dump
        ls FSPDUMP* 
        cd $cdir
    fi
    if [ $c -ne 0 ]; then
        cdir=`pwd`
        cd /dump
        ls SMADUMP* 
        cd $cdir
    fi
    if [ $d -ne 0 ]; then
        cdir=`pwd`
        cd /dump
        ls PWRDUMP* 
        cd $cdir
    fi
    exit_cleanup 1
    
elif [ "$2" = "" ]; then
    # missing ftp server arg
    echo "ftp server name was not specified. Exiting."
    exit_cleanup 10
        
elif [ "$3" = "" ]; then
    # missing userid arg
    echo "ftp userid was not specified. Exiting."
    exit_cleanup 11
    
elif [ "$4" = "" ]; then
    # missing password arg
    echo "ftp password was not specified. Exiting."
    exit_cleanup 12
        
elif [ "$5" = "" ]; then
    # missing ftp directory location, use default
    echo "ftp server directory not specified. Defaulting..."
#    DIR=""
fi

# Ok - all arguements were passed in, hopefully they are correct.
# Look for the dump file to ftp and begin the process...
for i in $1
do
    if [ ! -f /dump/$i ]; then
        #input arguement file not found
        echo "File not found: $1"
        exit_cleanup 2
    fi
done


#
# For eClizp GA3+, use 'ncftp' instead of 'ftp' and bypass this pre-check
# Unclear if this script is runon  AIX - if so, this needs to be ported
#

#testftp $1 $2 $3 $4 $DIR
#testRC=$?
#if [[ $testRC -eq 8 || $testRC -eq 9 ]]; then
#    echo "unable to change to target directory. Exiting."
#    exit_cleanup $testRC
#elif [[ $testRC -eq 7 || $testRC -eq 6 ]]; then
#    echo "unable to login to remote server. Exiting."
#    exit_cleanup $testRC
#elif [ $testRC -eq 5 ]; then
#    echo "unable to connect to remote server. Exiting."
#    exit_cleanup $testRC
#fi

# successful with ftp test. continue with file processing
ftpToServer $1 $2 $3 $4 $DIR
shellExit=$?

if [ $shellExit -ne 0 ]; then
    echo "Error $shellExit occurred offloading dump file to remote server."
fi

exit_cleanup $shellExit
