#!/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
# 7 - No special hmcdump partition
# 8 - bzImage not written
# 9 - initrd.gz not written
# 10 - disk1.img not written
# 11 - disk2.img not written
# 12 - disk3.img not written
#
#-------------------------------------------------------------------------------
#

function exit_cleanup {
#   rm -f $FTP_LOG_FILE
   chown hscroot.hmc $FTP_LOG_FILE 2>/dev/null
   cd /
   umount $INSTALL_DIR 2>/dev/null
   exit $1
}

function getReturnCode {
    case "$1" in
    "bzImage" )
	exit_cleanup 8
	;;
     "initrd.gz" )
        exit_cleanup 9
	;;
     "disk1.img" )
        exit_cleanup 10
	;;
     "disk2.img" )
        exit_cleanup 11
	;;
     "disk3.img" )
	exit_cleanup 12
	;;
     * )
        exit_cleanup 6
	;;
     esac
}

function ftpRemoteFiles {
   FTP_SERVER=$1
   REMOTE_DIR=$2
   USER=$3
   PASS=$4
   
   cat << END_CLIENT_FTP | $FTP_CMD -vn $FTP_SERVER 2>&1 > $FTP_LOG_FILE
user $USER $PASS
bin
cd $REMOTE_DIR
get bzImage $INSTALL_DIR/bzImage
get initrd.gz $INSTALL_DIR/initrd.gz
get disk1.img $INSTALL_DIR/disk1.img
get disk2.img $INSTALL_DIR/disk2.img
get disk3.img $INSTALL_DIR/disk3.img
quit
END_CLIENT_FTP
} # end function getRemoteFiles

function ftpChecksumFile {
   FTP_SERVER=$1
   REMOTE_DIR=$2
   USER=$3
   PASS=$4
   
   cat << END_CLIENT_FTP | $FTP_CMD -vn $FTP_SERVER 2>&1 > $FTP_LOG_FILE
user $USER $PASS
bin
cd $REMOTE_DIR
get hmcnetworkfiles.sum $INSTALL_DIR/hmcnetworkfiles.sum
quit
END_CLIENT_FTP
} # end function ftpChecksumFile
#-------------------------------------------------------------------------------
# "Main" program start   
#-------------------------------------------------------------------------------
FTP_LOG_FILE=/var/hsc/log/getupgrftp.log
FTP_CMD=/usr/kerberos/bin/ftp
INSTALL_DIR=/hmcdump
FILE_LIST="bzImage initrd.gz disk1.img disk2.img disk3.img"

if [ "$1" = "" ]
then
   echo "Usage: getRemoteUpgrFiles <ftp server> <remote dir> <user> <password>"
   exit_cleanup 1
fi
if [ ! -d $INSTALL_DIR ]; then
   mkdir -p $INSTALL_DIR
fi
mount $INSTALL_DIR 2>/dev/null
if [ $? -ne 0 ]; then
  # need to do something here, either the filesystem is not created
  cat /etc/fstab | grep -q "$INSTALL_DIR"
  if [ $? -ne 0 ]; then
  # something is wrong fstab does not have the special partition needed
     exit_cleanup 7 
  fi
fi
cd $INSTALL_DIR
#-------------------------------------------------------------------------------
# 'ftp' the file from service server
#-------------------------------------------------------------------------------
ftpRemoteFiles $1 $2 $3 $4 

# good transfer, now see if the file actually got written to the HMC
if [ $? -eq 0 ]; then
  all_transferred=1
  for i in $FILE_LIST
  do
    if [ ! -f $INSTALL_DIR/$i ]; then
      all_transferred=0
      break
    fi
  done
  if [ $all_transferred -eq 1 ]; then
# Now make sure at the minimum the file size are all ok, i.e none zero
     typeset -i fileSize
     zero_fileszie=0
     for i in $FILE_LIST
     do
        fileSize=`/usr/bin/du -m $INSTALL_DIR/$i | /usr/bin/cut -f1`
        if [ $fileSize -eq 0 ]; then
	   zero_filesize=1
	   break
	fi
     done 
     if [ $zero_filesize -eq 1 ]; then
        getReturnCode $i
     fi
# Now make sure at the checksum the file size are all ok
     rm -f $INSTAL_DIR/hmcnetworkfiles.sum 2>/dev/null
     ftpChecksumFile $1 $2 $3 $4 
     if [ -f $INSTALL_DIR/hmcnetworkfiles.sum ]; then
  	for i in $FILE_LIST
	do
	   s1=`/usr/bin/sum $INSTALL_DIR/$i | cut -d' ' -f1`
	   s2=`/usr/bin/grep "$i" $INSTALL_DIR/hmcnetworkfiles.sum | cut -d':' -f1`
	   if [ "$s1" != "$s2" ]; then
	      getReturnCode $i
	   fi
	done
     fi
     exit_cleanup 0
  else
    getReturnCode $i
  fi
else
  # look for "Transfer complete", "Successful transfer" or equivalent msg code
  # careful on grep'ing for all these values - note the space
   grep -i "^226 " $FTP_LOG_FILE 2>&1 >/dev/null
   if [ $? -ne 0 ]
   then
     # ftp failed for some reason - find out why

     # special case for i5 OS FTP
     grep -i "^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
	exit_cleanup 4
     fi

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


     grep -i "^230 " $FTP_LOG_FILE 2>&1 >/dev/null
     if [ $? -ne 0 ]
     then
        exit_cleanup 2
     fi
   
     grep -i "^550 " $FTP_LOG_FILE 2>&1 >/dev/null
     if [ $? -eq 0 ]
     then
        exit_cleanup 3
     fi

     grep -i "^530 " $FTP_LOG_FILE 2>&1 >/dev/null
     if [ $? -eq 0 ]
     then
        exit_cleanup 2
     fi
   
     errRC=$(grep -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

