#!/bin/bash

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

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

else    
    /usr/kerberos/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
    
    # Go to target directory
    cdir=`pwd`
    cd /dump

if [ "$dir" != "" ]; then    
    /usr/kerberos/bin/ftp -n -u $server <<EOF3 > /var/hsc/log/ftp.log 2>&1
    
user $userid $password
bin
cd $dir
put $file
quit
EOF3

else
    /usr/kerberos/bin/ftp -n -u $server <<EOF4 > /var/hsc/log/ftp.log 2>&1
    
user $userid $password
bin
put $file
quit
EOF4

fi
    
    # save rc for return
    grep -i "failed" /var/hsc/log/ftp.log > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        # try looking for this
        grep -i "unknown" /var/hsc/log/ftp.log > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            # try looking for this
            grep -i "cannot" /var/hsc/log/ftp.log > /dev/null 2>&1
            if [ $? -ne 0 ]; then
                # last try...
                grep -i "Permission denied" /var/hsc/log/ftp.log > /dev/null 2>&1
                if [ $? -ne 0 ]; then
                    funcRC=0
                else
                    funcRC=24
                fi
            else
                funcRC=23
            fi
        else
            funcRC=22
        fi
    else
        funcRC=21
    fi
   
    # switch back to original directory
    cd $cdir
   
    return $funcRC
}

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

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

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

# if here, all arguements must be valid dump files
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
