#!/usr/bin/bash
# Copyright (C) 2014 McAfee, Inc.  All Rights Reserved.
#
# raidcheck checks to see if RAID controller firmware updates are pending
#
# assumes that stderr goes somewhere useful
# call with CWD=host* controller directory
# or call with p1="all" to report on all controllers
# or set
# p1=hostN
# TODO: move this functionality into a helper shared with raidupdate
# exit code 0 = no updates available
# exit code 1 = update available
# exit code 2 = 2 stage updates available

me=$(readlink -f $0)
mydir=$(dirname $me)

. $mydir/../utils/functions

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

# check the firmware version to apply, if any
# p1=$host
# p2=$controller_type
# p3=$controller PCI
# p4=current firmware
lookup_new_firmware() {
    # check for any new firmware revision
    awk -v p=$pcidev -v c=$subsys -v fw="$4" '($1!~"\\<"c"\\>"){next}($3~"\\<"fw"\\>"){$4=p"/"$4;print}' $mydir/$pcidev/index.txt 2>/dev/null
}

# 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
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
fi
pcidir=$(get_pci_dir $host)
pcidev=$(get_pci_device $host)
pcidev=${pcidev/:/.}
subsys=$(get_pci_subsystem $host)
if [[ -n $1 ]] ; then
    controller_type=$(get_controller_type $host)
    controller=$(lspci -n | awk -v p=${pcidir##*/0000:} '($1==p){printf "%s/%s\n",p,$3}')
else
    controller_type=$(tr -dc '[:print:]' <controller.txt)
    controller=$(awk -v p=${pcidir##*/0000:} '($1==p){printf "%s/%s\n",p,$3}' ../lspci-n.txt)
fi

new_fw_file=
h=${host##*/}
next_fw=

echo "Controller $h ($controller_type):"

if [[ -z $pcidev || -z $subsys ]] ; then
    echo "  Unidentified Controller Type"
    exit
fi

curr_fw=$(get_fwrev "$host" "$controller_type" "$controller")

[[ -z $curr_fw ]] && {
    echo "  No firmware revision available"
    exit
}
echo "  Current firmware revision is: $curr_fw"

# check for any new firmware revision
next_fw="$(lookup_new_firmware "$host" "$controller_type" "$controller" "$curr_fw")"
if [[ -n $next_fw ]] ; then
    set $next_fw
    new_fw_file="$4"
    new_ver="$5"
    flags="$6"
    rs=
    echo "  Next available firmware is: $new_ver"

    # check if there is another update coming
    next_fw="$(lookup_new_firmware "$host" "$controller_type" "$controller" "$new_ver")"
    if [[ -n $next_fw ]] ; then
        set $next_fw
        next_fw=$5
        echo "    After that, firmware $next_fw will then need to be applied after a reboot"
        raidcheck=2
    else
        raidcheck=1
    fi
else
    # no new firmware available
    echo "  No new firmware available"
    raidcheck=0
fi
echo
exit $raidcheck
