#!/usr/bin/bash
# Copyright (C) 2014 McAfee, Inc.  All Rights Reserved.
#
# check for hddupdate updates for the current RAID controller
#
# assumes that stderr goes somewhere useful
# call with CWD=host* controller directory
# or call with p1="all" to check all drives+controllers
# or set
# p1=hostN
# p2=controller type
# p3=pciBus/pciID
# p4=HDD PD ID (if required)
# p5=HDD model
# p6=HDD curr firmware
# TODO: move this functionality into a helper shared with hddupdate
# exit code 0 = no updates available
# exit code 1 = updates available

me=$(readlink -f $0)
# path to my directory
mydir=$(dirname $me)

. $mydir/../utils/functions

shopt -s nullglob

# if asked to check all controllers then recurse for each controller
if [[ $1 = all ]] ; then
    hddcheck=0
    function hddcheck() {
        # call ourselves again with the controller as an arg
        # remember if any updates are available
        $me $1 || hddcheck=$?
    }
    each_controller hddcheck
    exit $hddcheck
fi

# if we are running from getlogs then we can just query the hardware directly
if [[ -n $1 ]] ; then
    # query the live system rather than cached files
    if [[ $1 = /* ]] ; then
        host=$1
    elif [[ $1 = nvme* ]] ; then
        host=/sys/class/nvme/$1
    else
        host=/sys/class/scsi_host/$1
    fi
    controller_type=$(get_controller_type $host)
else
    # otherwise we have to fall back to querying hardware log files
    export CONTROLLERDIR=$PWD
    if [[ $PWD = */nvme* ]] ; then
        host=/sys/class/nvme/${PWD##*/}
    else
        host=/sys/class/scsi_host/${PWD##*/}
    fi
    controller_type=$(tr -dc '[:print:]' <controller.txt)
fi

# controller type specific identifier for HDD
hdd=
# local model of hdd
model=
# current firmware level of HDD
curr_fw=
# local status code
r=0

# p1=hdd pd id
# p2=model
# p3=current firmware
do_drive () {
    local pd="$1"
    local model="$2"
    local curr_fw="$3"

    # check for any new firmware revision
    # the inquiry data format is in different drives so we have to be sloppy on
    # how we match it
    local new_fw_file=
    local new_fw_rev=
    local new_fw_dir=
    local updater=

    # find an index file that pertains to this model
    read new_fw_dir new_fw_file new_fw_rev updater < <(awk -v h=${model} -v fw="$curr_fw" '(h!~$1){next}($2~"\\<"fw"\\>"){printf "%s %s %s %s\n",gensub(/\/[^/]*$/,"",1,FILENAME),$3,$4,$5}' $mydir/*/index.txt)
    : :$new_fw_dir:$new_fw_file:$new_fw_rev:$updater:

    # display the new firmware
    echo -n "  PD ${pd} ${model} ${curr_fw} : "
    if [[ -n $new_fw_rev ]] ; then
        echo "Update to $new_fw_rev"
        hddcheck=1
    else
        echo "(no new firmware)"
    fi
}

hddcheck=0
if [[ -n $4 && -n $5 && -n $6 ]] ; then
    do_drive "$4" "$5" "$6"
else
    while read hdd model curr_fw ; do
        : $hdd $model $curr_fw

        [[ -z $4 || $4 = $hdd ]] || {
            : skipped, want $4
            continue
        }

        do_drive "$hdd" "$model" "$curr_fw"

    done < <(get_drives "$host" "$controller_type")
fi

exit $hddcheck
