#!/bin/sh

# This script is invoked from FTP or WBI when a user provides an unbundled disk firmware file. (put filename.fla disk:...)
# Expectations:
# - filename is in the format "disk[:<disklist in CLI-ish format>]", e.g. disk:0.0,0.2-4. It is okay to substitute ':' anywhere '.' is accepted (legacy compatibility).
# - firmware file is located in /mnt/ramdisk
# - there is only one disk firmware file present at one time
# - WBI uses flag target disk:999:999 to indicate the presence of another file containing the disk list (/mnt/ramdisk/listOfDisks)

source /usr/bin/utils.sh

actComp="/disk"

fileFormat="/mnt/ramdisk/disk*"
numFiles=$(ls $fileFormat | wc -l)

if [ $numFiles -eq 0 ]
then
    apiError $actComp 1056 "Disk firmware file was not found."
    echo "Disk firmware file was not found."
    echo "RETURN_CODE: 11"
    exit 11
elif [ $numFiles -gt 1 ]
then
    apiError $actComp 1055 "Multiple disk firmware files were found. Unable to proceed."
    echo "Multiple disk firmware files were found. Unable to proceed."
    echo "RETURN_CODE: 11"
    exit 11
fi

# there is exactly one disk firmware file
filename=$(basename $fileFormat)

if [ ! -r $filename ]
then
    apiError $actComp 1055 "Disk firmware file is unreadable for target $filename."
    echo "Disk firmware file is unreadable for target $filename."
    echo "RETURN_CODE: 11"
    exit 11
fi

# Pull the address out of the filename, which should be of the form disk[:<list of encl.slot pairs>]
addr=$(echo $filename | sed "s/disk://")

if [ "$addr" == "disk" ] # user intended all disks
then
    apiActive $actComp "Updating all disks with the provided firmware file."
    echo "Updating all disks with the provided firmware file."

    /usr/bin/capi.app --component $actComp --loaddisk $filename -x
elif [[ $addr == disk* ]] # Some broken formatting starting with "disk"
then
    apiError $actComp 1105 "Invalid FTP target: $addr."
    echo "Invalid FTP target: $addr. Expected usage: put <filename> disk[:<encl>.<slot>,...]"
    echo "RETURN_CODE: 11"
    exit 11
else # target was in format disk:<addr>
    if [ "$addr" == "999:999" ] # WBI flag value
    then
        # WBI pushes a potentially long list of disks into a txt file
        listFile="/mnt/ramdisk/listOfDisks"
        if [ ! -r $listFile ]
        then
            apiError $actComp 1055 "Disk list from WBI was unreadable."
            echo "Disk list from WBI was unreadable."
            echo "RETURN_CODE: 11"
            exit 11
        fi

        addr=$(head -1 $listFile)

        if [ -z "$addr" ];
        then
            apiError $actComp 1105 "Disk list from WBI was empty."
            echo "Disk list from WBI was empty."
            echo "RETURN_CODE: 11"
            exit 11
        fi
    fi

    apiActive $actComp "Updating selected disk(s) with the provided firmware file."
    echo "Updating selected disk(s) with the provided firmware file."

    /usr/bin/capi.app --component $actComp --loaddisk $filename --list $addr -x
fi

if [ $? -ne 0 ]
then
    echo "*** Code Load Fail. Failed to successfully update disk firmware. ***"
    echo "RETURN_CODE: 11"
    exit 11
fi

echo "RETURN_CODE: 9"
exit 0

