#!/bin/bash
#
# Execute Date, Time, and/or Timezone changes o the HMC with commands
#  requiring root privilege 
#
# Description: Issue to run privileged Date/Time commands 
#
# Change Activity:
#   11/11/2007 ferrants    63xxxx - initial version
#
#STARTUSAGE
#
# Usage:
#   efixUtil -c queryMediaPackages -m <mountpoint>
#   efixUtil -c queryFtpPackages -s <server> -u <user> -p <password> [-d <directory>]
#   efixUtil -c queryNfsPackages -s <server> -r <resource> [-o <options>] [-d <directory>]
#   efixUtil -c removePackageFile
#   efixUtil -c resetLogFile -l <type>
#   efixUtil -c getHmcServicePackage -s <server> -u <user> -p <password>
#                                   [-d <directory>] -z <packageName>
#            -c getHmcServicePackage -s <server> -r <resource> [-o <options>]
#                                   [-d <directory>] -z <packageName>
#   efixUtil -c getSecureHmcServicePackage -s <server> -u <user> -p <password>
#                                         [-d <directory>] -z <packageName>
#
# Where:
#   -c   The operation to be performed.  Must be one of:
#        . queryMediaPackages - mounts and reads the specified local media for
#                               all supported corrrective service packages.
#                               Generates a file containing this output.
#        . queryFtpPackages - connects to a remote FTP server and generates a
#                             a file containing all supported corrective
#                             service packages located on that server
#        . queryNfsPackages - connects to a remote NFS server and generates a
#                             a file containing all supported corrective
#                             service packages located on that server
#        . removePackageFile - erases the service package list file created by
#                              the query package commands
#        . resetLogFile - command to erase this script's cumulative log file or
#                         the ftp.log file in the HMC log directory
#        . getHmcServicePackage - command to offload either an ftp or nfs
#                                 specified service package to the expected
#                                 installaion directory
#        . getSecureHmcServicePackage - command to offload the HMC service package
#                                       using 'sftp'
#
#   -d   The remote directory on a remote server to (optionally) look for and 
#        and install corrective service from
#   -l   The type of log file to remove - "efix" or "ftp"
#   -m   The current directory where the fixed media (CD/DVD, USB flash memory
#        device, or diskette) is mounted at
#   -o   additional NFS mount options
#   -p   If specifying an FTP server, this parameter corresponds to the user
#        ID's password on the remote server.
#   -r   If specifying an NFS server, this parameter specifies the remote resource
#   -s   The remote server, either FTP or NFS, to install service from
#   -u   If specifying an FTP server, this parameter specifies the user ID to
#        log in
#   -z   selected remote HMC corrective service package to offload
#
# Return Codes:
#  4 - no service files detected on remote NFS server
# 21 - ftp, not logged in
# 22 - ftp, login failed, user/password error
# 23 - ftp, connection refused
# 24 - ftp, could not resolve server (unknown host)
# 25 - ftp, no connection to server errror
# 26 - ftp, permission denied error
# 27 - ftp, catch all for all "other" errors
# 28 - ftp, file/directory not found
# 30 - portmap started successfully
# 31 - could not start portmap
# 32 - could not mount remote NFS server
# 33 - could not 'cd' to NFS directory
# 50 - ftp, file name not allowed (permission errors?)
# 99 - missing argument, showUsage
# * - other system command return codes
#
#ENDUSAGE
#****************************************************************************

function showUsage() {
   # Print out the prologue comments as usage info
   sed -e '/STARTUSAGE/,/ENDUSAGE/ s/^#//' -e '1,/STARTUSAGE/ d' -e '/ENDUSAGE/,$ d' "$0"
}


#---------------------------------------------------------------------------------
# function to offload a service package file from a remote ftp server
#---------------------------------------------------------------------------------
function offloadFtpServicePackage {
    SERVER=$1
    USER=$2
    PASSWORD=$3
    DIRECTORY=$4
    PACKAGE=$5

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

    echo "host $SERVER" > $TMP_FTP
    echo "user $USER" >> $TMP_FTP
    echo "pass $PASSWORD" >> $TMP_FTP

    OLDPWD=`pwd`
    extension=`echo ${PACKAGE##*.}`
    case $extension in
        iso | img ) if [ ! -d $HMC_AUX_SERVICE_DIR ]; then
                        mkdir -p $HMC_AUX_SERVICE_DIR
                    fi
                    cd $HMC_AUX_SERVICE_DIR ;;
                * ) cd $HMC_SERVICE_INSTALL_DIR ;;
    esac

    # If the file already exists on the HMC, remove it prior to ftp'ing a new version.
    # There appears to be an issue with 'ncftp' and parsing the verbose ftp debug output
    # if the target file already exists. Note: we just changed to the local directory
    # on the HMC where we expect the file to reside
    if [ -f $PACKAGE ]; then
        rm -f $PACKAGE
        echo "prior service package $PACKAGE removed from the HMC prior to downloading of new version." >> $log
    fi

    if [[ "$DIRECTORY" == "" || "$DIRECTORY" == "." ]]; then
        # no remote directory specified
        echo "archive file query does not require a change of directory." >> $log

        LANG=en_US $FTP_GET_CMD -f $TMP_FTP -d $FTP_LOG -V -t $TIMEOUT -o useMDTM=0 . $PACKAGE

        # Check for errors during the transfer
        checkForFtpDataTransferErrors $FTP_LOG
        funcRC=$?
    else
        # case here where remote target directory is specified
        LANG=en_US $FTP_GET_CMD -f $TMP_FTP -d $FTP_LOG -V -t $TIMEOUT -o useMDTM=0 . $DIRECTORY/$PACKAGE

        # now check for data transfer errors
        checkForFtpDataTransferErrors $FTP_LOG
        funcRC=$?
    fi

    # return to shell directory & clean up
    cd $OLDPWD
    rm -f $TMP_FTP
    
    return $funcRC
}


#---------------------------------------------------------------------------------
# function to offload a service package file from a remote nfs server
#---------------------------------------------------------------------------------
function offloadNfsServicePackage {
    SERVER=$1
    RESOURCE=$2
    OPTIONS=$3
    DIRECTORY=$4
    PACKAGE=$5

    funcRC=0

    tryStartPortmap
    portmapRC=$?
    if [[ $portmapRC -eq 0 || $portmapRC -eq 30 ]]; then
        # portmap is now running, continue. Check on resource mountpoint first...
        if [ -d /mnt/remote ]; then
            echo "remote nfs mount point already exists, continuing..." >> $log
        else
            mkdir -p /mnt/remote
        fi
        MTPT=/mnt/remote

        # now attempt to mount...
        # NFS example:
        #   mount -o vers=2 -o proto=udp <server>:<resource> <mount point>
        # Samba example:
        #   mount -t smbfs -o username=xxx,password=yyy //<server>/<resource> <mount point>
        if [ "$OPTIONS" == "null" ]; then
            mount -v $SERVER:$RESOURCE $MTPT >> $log 2>&1
        else
            mount -v -o $OPTIONS $SERVER:$RESOURCE $MTPT >> $log 2>&1
        fi
        mountRC=$?
        if [ $mountRC -eq 0 ]; then
            extension=`echo ${PACKAGE##*.}`
            case $extension in
                iso | img ) if [ ! -d $HMC_AUX_SERVICE_DIR ]; then
                                mkdir -p $HMC_AUX_SERVICE_DIR
                            fi
                            TARGET_DIR=$HMC_AUX_SERVICE_DIR ;;
                        * ) TARGET_DIR=$HMC_SERVICE_INSTALL_DIR ;;
            esac

            if [[ "$DIRECTORY" == "" || "$DIRECTORY" == "." ]]; then
                cp -v $MTPT/$PACKAGE $TARGET_DIR >> $log 2>&1
            else
                cp -v $MTPT/$DIRECTORY/$PACKAGE $TARGET_DIR >> $log 2>&1
            fi
            funcRC=$?

            # all done - unmount
            umount $MTPT >> $log 2>&1
        else
            # set exit code due to the 'mount' error
            funcRC=32
        fi
        if [ $portmapRC -eq 30 ]; then
            # A '30' code indicates that this function started a new instance
            # of the portmap service, so stop it. A '0' code meant that the service
            # was already running, leave it be.
            tryStopPortmap
        fi

    else
        # set exit code with portmap error
        funcRC=$portmapRC
    fi
    
    return $funcRC
}


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

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

    echo "host $SERVER" > $TMP_FTP
    echo "user $USER" >> $TMP_FTP
    echo "pass $PASSWORD" >> $TMP_FTP

    if [[ "$DIRECTORY" == "" || "$DIRECTORY" == "." ]]; then
        # no remote directory specified
        echo "archive file query does not require a change of directory." >> $log
        if [ "${SERVER/*:*/:}" = ":" ]; then
            # has a ":" char, so assume this is IPv6
            LANG=en_US $FTP_LS_CMD -f $TMP_FTP -1 -d $FTP_LOG -t $TIMEOUT -o useMDTM=0 ftp://[$SERVER] > $workingFileList
        else
            LANG=en_US $FTP_LS_CMD -f $TMP_FTP -1 -d $FTP_LOG -t $TIMEOUT -o useMDTM=0 ftp://$SERVER > $workingFileList
        fi
        # Check for errors during the transfer
        checkForFtpDataTransferErrors $FTP_LOG
        funcRC=$?

        if [ $funcRC -ne 0 ]; then
            rm -f $workingFileList
        fi
    else
        # case here where remote target directory is specified
        # repeat ftp with actual data transfer
        LANG=en_US $FTP_LS_CMD -f $TMP_FTP -1 -d $FTP_LOG -t $TIMEOUT -o useMDTM=0 -W "cwd $DIRECTORY" ftp://$SERVER > $workingFileList
        if [ "${SERVER/*:*/:}" = ":" ]; then
            # has a ":" char, so assume this is IPv6
            LANG=en_US $FTP_LS_CMD -f $TMP_FTP -1 -d $FTP_LOG -t $TIMEOUT -o useMDTM=0 -W "cwd $DIRECTORY" ftp://[$SERVER] > $workingFileList
        else
            LANG=en_US $FTP_LS_CMD -f $TMP_FTP -1 -d $FTP_LOG -t $TIMEOUT -o useMDTM=0 -W "cwd $DIRECTORY" ftp://$SERVER > $workingFileList
        fi
        # now check for data transfer errors
        checkForFtpDataTransferErrors $FTP_LOG
        funcRC=$?

        if [ $funcRC -ne 0 ]; then
            rm -f $workingFileList
        fi
    fi

    if [ -f $workingFileList ]; then
        # No errors recorded from ftp transfer - check the actual output now.
        # It is possible there may be no service files detected
        grep -x -e ".*.zip" -e ".*.iso" $workingFileList > $servicePackageList

        echo "output file list returned from zip/iso file query is:" >> $log
        cat $servicePackageList >> $log

        rm -f $workingFileList
    else
        # some sort of file transfer error
        echo "File transfer error $funcRC occurred." >> $log
    fi

    # bit o' cleanup
    rm -f $TMP_FTP

    return $funcRC
}


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

    FTP_LOG=$1
    funcRC=0

    # 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 detcted 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
                    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
                                ncftpRc0=$?
                                grep -q -i "Name or service not known" $FTP_LOG > /dev/null 2>&1
                                ncftpRc1=$?
                                if [ $ncftpRc0 -eq 0 ]; then
                                    # Oops - error accessing server
                                    funcRC=24
                                elif [ $ncftpRc1 -eq 0 ]; then
                                    # 'ncftpget' issues with unresolvable server name
                                    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
    
    echo "exiting checkForFtpDataTransferErrors, funcRC = $funcRC." >> $log
    return $funcRC
}


#---------------------------------------------------------------------------------
# function to start the portmap daemon
#---------------------------------------------------------------------------------
function tryStartPortmap {

    # Determine if portmap is running - it should not be. Then start the service
    # for the duration of this command invocation. Note that if the user really
    # commits to type of installation, we'll be called multiple times
    PORTMAP=/sbin/portmap
    PORTMAP_SHORT=portmap
    PID=`ps -ef | grep $PORTMAP_SHORT | grep -v grep | awk '{print $2}'`
    
    if [ -n "$PID" ]; then
        echo "portmap is currently executing, PID is $PID." >> $log
        funcRC=0
    else
        # echo No PID, so start the service.
	echo "about to start the portmap service..." >> $log
        /etc/init.d/portmap start
        sleep 2
     
        # save away this PID for later process termination    
        NEW_PID=`ps -ef | grep $PORTMAP_SHORT | grep -v grep | awk '{print $2}'`
        
        if [ -n "$NEW_PID" ]; then
            echo "portmap started successfully, pid is $NEW_PID" >> $log
            funcRC=30
        else
            echo "unable to start the portmap daemon, rc = $RC" >> $log
            funcRC=31
        fi
    fi

    echo "exiting tryStartPortmap, funcRC = $funcRC." >> $log
    return $funcRC
}


#---------------------------------------------------------------------------------
# function to stop the portmap service
#---------------------------------------------------------------------------------
function tryStopPortmap {

    # Simply stop the portmap process
    echo "about to terminate the portmap daemon..." >> $log
    /etc/init.d/portmap stop

    echo "exiting tryStopPortmap." >> $log
    return 0
}


#---------------------------------------------------------------------------------
# function to list the contents of a specified NFS directory
#---------------------------------------------------------------------------------
function listNfsDirectory {

    MTPT=$1
    DIRECTORY=$2

    funcRC=0

    if [[ "$DIRECTORY" == "" || "$DIRECTORY" == "." ]]; then
        echo "remote NFS access does not require a directory change." >> $log

        # ensure it's not a '.' char
        DIRECTORY=""
    else
        echo "remote NFS access requires a change to directory named <$DIRECTORY>." >> $log
        cd $MTPT/$DIRECTORY
        cdRC=$?
        if [ $cdRC != 0 ]; then
            funcRC=33
        else
            # Just switch to a different directory so we can unmount (otherwise, umount fails due to "resource busy")
            cd /
        fi
    fi
              
    if [ $funcRC == 0 ]; then
        # generate the listing!
        ls -1 $MTPT/$DIRECTORY | grep -x -e ".*.zip" -e ".*.iso" > $servicePackageList
        if [ $? -ne 0 ]; then
            # no *.zip or *.iso files...
            echo "no *.zip or *.iso files detected on the remote NFS server at $MTPT/$DIRECTORY" >> $log
            rm -f $servicePackageList
            funcRC=4
        else
            echo "output file list returned from zip/iso file query is:" >> $log
            cat $servicePackageList >> $log
        fi
    fi

    echo "exiting listNfsDirectory, funcRC = $funcRC." >> $log
    return $funcRC
}


#---------------------------------------------------------------------------------
# function to list the contents of the NFS site
#---------------------------------------------------------------------------------
function generateNfsPackageList {
    SERVER=$1
    RESOURCE=$2
    OPTIONS=$3
    DIRECTORY=$4

    funcRC=0

    tryStartPortmap
    portmapRC=$?
    if [[ $portmapRC -eq 0 || $portmapRC -eq 30 ]]; then
        # portmap is now running, continue. Check on resource mountpoint first...
        if [ -d /mnt/remote ]; then
            echo "remote nfs mount point already exists, continuing..." >> $log
        else
            mkdir -p /mnt/remote
        fi
        MTPT=/mnt/remote

        # now attempt to mount...
        # NFS example:
        #   mount -o vers=2 -o proto=udp <server>:<resource> <mount point>
        # Samba example:
        #   mount -t smbfs -o username=xxx,password=yyy //<server>/<resource> <mount point>
        if [ "$OPTIONS" == "null" ]; then
            mount -v $SERVER:$RESOURCE $MTPT >> $log 2>&1
        else
            mount -v -o $OPTIONS $SERVER:$RESOURCE $MTPT >> $log 2>&1
        fi
        mountRC=$?
        if [ $mountRC -eq 0 ]; then
            listNfsDirectory $MTPT $DIRECTORY

            # note that a value of '4' returned from the above function is an indication
            # of no listing generated, not an error code
            funcRC=$?

            # all done - unmount
            umount $MTPT >> $log 2>&1
        else
            # set exit code due to the 'mount' error
            funcRC=32
        fi
        if [ $portmapRC -eq 30 ]; then
            # A '30' code indicates that this function started a new instance
            # of the portmap service, so stop it. A '0' code meant that the service
            # was already running, leave it be.
            tryStopPortmap
        fi

    else
        # set exit code with portmap error
        funcRC=$portmapRC
    fi
    
    echo "exiting generateNfsPackageList, funcRC = $funcRC." >> $log
    return $funcRC
}


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

# Initialize some constants
log='/var/hsc/log/efixUtil.log'
servicePackageList='/tmp/servicepackages.lst'

# Initialize variables that hold option values 
giveUsage=0
cmd=
directory=
options=
password=
resource=
server=
mountPoint=
userid=
package=
logtype=

# Parse the options
while getopts 'c:d:l:m:o:p:r:s:u:z:?' optname; do
   case "$optname" in
      c) cmd="$OPTARG";;
      d) directory="$OPTARG";;
      l) logtype="$OPTARG";;
      m) mountPoint="$OPTARG";;
      o) options="$OPTARG";;
      p) password="$OPTARG";;
      r) resource="$OPTARG";;
      s) server="$OPTARG";;
      u) userid="$OPTARG";;
      z) package="$OPTARG";;
      \?) giveUsage=1; break;;
   esac
   noopt=0
done

if [ "$giveUsage" -eq 1 ]; then
   showUsage
   exit 99
fi
      
if [ -z "$cmd" ]; then
   echo "Missing required argument command argument" >> $log
   showUsage
   exit 99
fi


#
# Handle the various requests
#

echo "--> efixUtil script invoked on `date`, command is: $cmd" >> $log

FTP_LS_CMD=/usr/bin/ncftpls
FTP_GET_CMD=/usr/bin/ncftpget
TIMEOUT=120

FTP_LOG=/var/hsc/log/ftp.log
HMC_SERVICE_INSTALL_DIR=/dump/hsc_install.images
HMC_AUX_SERVICE_DIR=/dump/efix

workingFileList=/tmp/potentialpackages.lst
servicePackageList=/tmp/servicepackages.lst


if [ "$cmd" == "resetLogFile" ]; then
   if [ -z "$logtype" ]; then
      echo "Missing required -l argument" >> $log
      showUsage
      exit 99
   fi
   # Since this script will be called multiple times throughout the Install HMC
   # Corrective Service task, we'll always be concatenating successive script
   # calls together. This command, to be invoked only at the beginning of the
   # task, should simply erase the prior $log file
   cmdRc=0;
   if [ "$logtype" == "efix" ]; then
      if [ -f $log ]; then
         rm -f $log
      fi
   elif [ "$logtype" == "ftp" ]; then
      if [ -f $FTP_LOG ]; then
         rm -f $FTP_LOG
      fi
   else
      echo "logtype specified is unsupported: $logtype. No files removed." >> $log
      cmdRc=98
   fi

   echo "exiting $cmd with rc = $cmdRc" >> $log
   exit $cmdRc

elif [ "$cmd" == "queryMediaPackages" ]; then
   # Query for all potential corrective service packages. Note that the local
   # media is expected to be mounted before this command is invoked
   if [ -z "$mountPoint" ]; then
      echo "Missing required -t argument" >> $log
      showUsage
      exit 99
   fi

   # simple listing of all files located on the mountpoint, then filter for
   # *.zip and *.iso files only and write the output to the package list file
   ls -1 $mountPoint | grep -x -e ".*.zip" -e ".*.iso" > $servicePackageList
   listRC=$?
   if [ $listRC -ne 0 ]; then
      # no *.zip or *.iso files, or command failed ($listRC=2 is no files I think)
      echo "...queryMediaPackages did not find any zip/iso files on the media, listRC = $listRC" >> $log
      echo "now looking for media image..." >> $log

      # test for hard-coded file present on media. 'installImages' script should
      # be located on every (valid) p series service package
      ls $mountPoint/images/installImages
      lsRC=$?
      if [ $lsRC -ne 0 ]; then
          echo "no HMC image present on media." >> $log
          rm -f $servicePackageList
          listRC=$lsRC
      else
          # assume the entire media itself is the service package
          echo "media image:" > $servicePackageList
      fi
   else
      echo "list command successful, data returned from zip/iso file query is:" >> $log
      cat $servicePackageList >> $log
   fi

   echo "exiting $cmd with rc = $listRC" >> $log
   exit $listRC

elif [ "$cmd" == "queryFtpPackages" ]; then
   if [ -z "$server" ]; then
      echo "Missing required -s argument" >> $log
      showUsage
      exit 99
   fi
   if [ -z "$userid" ]; then
      echo "Missing required -u argument" >> $log
      showUsage
      exit 99
   fi
   if [ -z "$password" ]; then
      echo "Missing required -p argument" >> $log
      showUsage
      exit 99
   fi

   if [ -z "$directory" ]; then
       directory=.
   fi
   echo "about to query service packages on remote ftp server, server = $server, user ID = $userid, directory = <$directory>." >> $log

   generateFtpPackageList $server $userid $password $directory
   cmdRc=$?

   # Some ftp cleanup - the log file will be removed by the invoking code
   # since we want to write it to the log file if any errors occur
#   rm -f $FTP_LOG

   echo "exiting $cmd with rc = $cmdRc" >> $log
   exit $cmdRc

elif [ "$cmd" == "queryNfsPackages" ]; then
   if [ -z "$server" ]; then
      echo "Missing required -s argument" >> $log
      showUsage
      exit 99
   fi
   if [ -z "$resource" ]; then
      echo "Missing required -r argument" >> $log
      showUsage
      exit 99
   fi

   # init values if not specified
   if [ -z "$options" ]; then
       options=null
   fi
   if [ -z "$directory" ]; then
       directory=.
   fi
   echo "about to query service packages on remote nfs server, server = $server, resource = <$resource>, options = <$options>, directory = <$directory>." >> $log

   generateNfsPackageList $server $resource $options $directory
   cmdRc=$?

   echo "exiting $cmd with rc = $cmdRc" >> $log
   exit $cmdRc

elif [ "$cmd" == "removePackageFile" ]; then
   rm -f $servicePackageList
   cmdRc=$?

   echo "exiting $cmd with rc = $cmdRc" >> $log
   exit $cmdRc

elif [ "$cmd" == "getHmcServicePackage" ]; then
   if [ -z "$package" ]; then
      # service package name always required
      echo "Missing required -z argument" >> $log
      showUsage
      exit 99
   fi
   if [ -z "$server" ]; then
      # server always required
      echo "Missing required -s argument" >> $log
      showUsage
      exit 99
   fi

   # Since this routine is "generic" to both ftp and nfs requests, make assumumptions
   # based on input args which type of access is required
   if [ -z "$resource" ]; then
      # no resource specified - must be a request for an ftp offload
      if [ -z "$userid" ]; then
         # userId always required for ftp transfers
         echo "Missing required -u argument" >> $log
         showUsage
         exit 99
      fi
      if [ -z "$password" ]; then
         # password always required for ftp transfers
         echo "Missing required -p argument" >> $$log
         showUsage
         exit 99
      fi

      if [ -z "$directory" ]; then
          directory=.
      fi
      echo "about to offload the selected service package <$package> on remote ftp server, server = $server, user ID = $userid, directory = <$directory>." >> $log

      offloadFtpServicePackage $server $userid $password $directory $package
      cmdRc=$?

      # Some ftp cleanup - the log file will be removed by the invoking code
      # since we want to write it to the log file if any errors occur
#      rm -f $FTP_LOG
      
   else
      # 'resource' parameter specifed - must be an nfs offload request

      if [ -z "$options" ]; then
          options=null
      fi
      if [ -z "$directory" ]; then
          directory=.
      fi
      echo "about to offload the selected service package <$package> on remote nfs server, server = $server, resource = $resource, mount options = <$options>, directory = <$directory>." >> $log

      offloadNfsServicePackage $server $resource $options $directory $package
      cmdRc=$?
   fi

   echo "exiting $cmd with rc = $cmdRc" >> $log
   exit $cmdRc

elif [ "$cmd" == "getSecureHmcServicePackage" ]; then
   if [ -z "$package" ]; then
      # service package name always required
      echo "Missing required -z argument" >> $log
      showUsage
      exit 99
   fi
   if [ -z "$server" ]; then
      # server always required
      echo "Missing required -s argument" >> $log
      showUsage
      exit 99
   fi

   if [ -z "$userid" ]; then
      # userId always required for ftp transfers
      echo "Missing required -u argument" >> $log
      showUsage
      exit 99
   fi
   if [ -z "$password" ]; then
      # password always required for ftp transfers
      echo "Missing required -p argument" >> $$log
      showUsage
      exit 99
   fi

   if [ -z "$directory" ]; then
      directory=
   fi
   echo "using sftp to offload the selected service package <$package> on remote ftp server, server = $server, user ID = $userid, directory = <$directory>." >> $log

   # argv 0 remote hostname
   # argv 1 dir + filename 
   # argv 2 userid 
   # argv 3 passwd 
   # argv 4 location on HMC
   INSTALL_DIR=/dump/hsc_install.images
   if [ -z "$directory" ]; then
      /opt/hsc/bin/sftpGetFile $server $package $userid $password $INSTALL_DIR
   else
      /opt/hsc/bin/sftpGetFile $server $directory/$package $userid $password $INSTALL_DIR
   fi
   cmdRc=$?

   echo "exiting $cmd with rc = $cmdRc" >> $log
   exit $cmdRc

else
   echo "Unknown command verb: $cmd" >> $log
   showUsage
   rc=99
fi
