#!/bin/bash
#
#-------------------------------------------------------------------------------
# FTPs file from the HMC to a remote system
#
# 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 ftpRemoteFiles {
   BASE_FILENAME=`/usr/bin/basename $1`
   FILE=$1
   FTP_SERVER=$2
   REMOTE_DIR=$3
   REMOTE_FILE=`/usr/bin/basename $4`
   USER=$5
   PASS=$6
   >$FTP_LOG_FILE
   echo "host $FTP_SERVER" > $TMP_FTP
   echo "user $USER" >> $TMP_FTP
   echo "pass $PASS" >> $TMP_FTP
   $FTP_CMD -f $TMP_FTP -d $FTP_LOG_FILE -V -t $TIMEOUT -o useMDTM=0 -C $FILE $REMOTE_DIR/$REMOTE_FILE 

} # end function ftpRemoteFiles

function sftpRemoteFiles {
   FILE=$1
   FTP_SERVER="[$2]"
   REMOTE_DIR=$3
   REMOTE_FILE=`/usr/bin/basename $4`
   USER=$5
   IDFILE=$6

   if [ "$IDFILE" != "" ]
   then
      idarg="-o IdentityFile=$IDFILE"
   fi
   if [ "$REMOTE_DIR" != "" ]
   then
      REMOTE_PATH="$REMOTE_DIR/$REMOTE_FILE"
   else 
      REMOTE_PATH="$REMOTE_FILE"
   fi

   $FTP_CMD -o StrictHostKeyChecking=no $idarg $FILE $USER@$FTP_SERVER:$REMOTE_PATH
   return $?
} # end function sftpRemoteFiles
exit_cleanup()
{
  rm -f $SFTP_CMD_FILE 2>/dev/null
  rm -f $TMP_FTP 2>/dev/null
  exit $1
}

#-------------------------------------------------------------------------------
# "Main" program start   
#-------------------------------------------------------------------------------
FTP_LOG_FILE=/var/hsc/log/ftp.log
SFTP_CMD_FILE="/tmp/remote_ftp.$RANDOM"
TMP_FTP=/tmp/ftp_tmp_$$
TIMEOUT=60
if [ "$1" = "" ]
then
   echo "Usage: putRemoteFile <file> <ftp server> <remote dir> <remote file> <user> { <password> | <key file> } <mode>"
   exit_cleanup 1
fi

#-------------------------------------------------------------------------------
# 'ftp' the file from service server
#-------------------------------------------------------------------------------
if [ "$7" == "sftp" ]
then
   FTP_CMD=/usr/bin/scp
   sftpRemoteFiles $1 $2 $3 $4 $5 $6 
   exit_cleanup $?
else
   FTP_CMD=/usr/bin/ncftpput
   >$TMP_FTP
   chmod 600 $TMP_FTP
   ftpRemoteFiles $1 $2 $3 $4 $5 $6
   # 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 >/dev/null 2>&1
   if [ $? -ne 0 ]
   then
     # ftp failed for some reason - find out why
     # special case for i5 OS FTP
     grep -q "^250" $FTP_LOG_FILE 2>&1 >/dev/null
     if [ $? -eq 0 ]
     then
	exit_cleanup 0
     fi

     grep -q -i "Not connected" $FTP_LOG_FILE >/dev/null 2>&1
     if [ $? -eq 0 ]
     then
        exit_cleanup 4
     fi
     grep -q "^230" $FTP_LOG_FILE >/dev/null 2>&1
     if [ $? -ne 0 ]
     then
        exit_cleanup 2
     fi
     grep -q "^530" $FTP_LOG_FILE >/dev/null 2>&1
     if [ $? -ne 0 ]
     then
        exit_cleanup 2
     fi
     exit_cleanup 5
  fi
fi
exit_cleanup 0
