#!/bin/bash
#
# Copyright (C) 2017 Christien AuCoin caucoin@45drives.com
#                               2018 Brett Kelly        bkelly@45drives.com
# 45Drives
#
# This program is distributed under the GNU General Public License
# http://www.gnu.org/licenses/gpl.txt

## TOOLS SOURCE
STORCLI="http://images.45drives.com/Firmware/LSI9361/storcli/Linux/storcli-007.0606.0000.0000-1.noarch.rpm"
SAS3FLASH="http://images.45drives.com/Firmware/LSI9305/sas3flash/linux/sas3flash"
SAS2FLASH=""
## FIRMWARE SOURCE
SAS9311FW="http://images.45drives.com/Firmware/LSI3008/firmware/SAS9311_8i_IR.bin" #X11SSZ ONBOARD CONTORLLER
SAS2116FW="http://images.45drives.com/Firmware/LSI9201/Firmware/9201-16i_it.bin" #LSI 9201 16i
SAS322416="http://images.45drives.com/Firmware/LSI9305/16i/SAS9305_16i_IT_P.bin" #LSI 9305 16i
SAS322424="http://images.45drives.com/Firmware/LSI9305/24i/SAS9305_24i_IT_P.bin" #LSI 9305 16i
SAS3316FW="http://images.45drives.com/Firmware/LSI9361/16i/mr3316fw.rom" #LSI 9361 16i
SAS3324FW="http://images.45drives.com/Firmware/LSI9361/24i/mr3316fw.rom" #LSI 9361 24i
## NOTE LSI9361 FW applies to both 16i and 24i

## WORKING DIRECTORY
dir="/tmp/fw"
bindir="/opt/MegaRAID"
mkdir $dir 2>/dev/null
## DEPENDANCIES
if [[ ! -d $bindir ]];then mkdir $bindir ; fi
rpm -qa | grep -qw storcli || yum install -y $STORCLI
## CARD COUNTER
c1=0;c2=0;c3=0;c4=0;c5=0

## FLASH CARDS
cardlist=$(lspci | egrep -o "3008|2116|3224|3316|3324")
for card in $cardlist ;do
        case $card in
        3008)
                if [[ $c1 -eq 0 ]]; then
                        if [[ ! -f $dir/SAS9311_8i_IR.bin ]]; then curl --silent -o $dir/SAS9311_8i_IR.bin $SAS3316FW ; fi
                        C=$($bindir/storcli/storcli64 show | egrep "$card|$8i" | awk '{print $1}')
                        for c in $C ; do $bindir/storcli/storcli64 /c$c download file=$dir/SAS9311_8i_IR.bin ; done
                fi
                let c1=c1+1
                ;;
        2116)
                if [[ $c2 -eq 0 ]]; then curl --silent -o $dir/9201-16i_it.bin $SAS2116FW ; fi
                echo
                $bindir/sas2flash -c $c2 -f $dir/9201-16i_it.bin
                let c2=c2+1
                ;;
        3224)
                if [[ $c3 -eq 0 ]]; then
                        if [[ ! -f $dir/SAS9305_16i_IT_P.bin ]]; then curl --silent -o $dir/SAS9305_16i_IT_P.bin $SAS322416 ; fi
                        if [[ ! -f $dir/SAS9305_24i_IT_P.bin ]]; then curl --silent -o $dir/SAS9305_24i_IT_P.bin $SAS322424 ; fi
                        C=$($bindir/storcli/storcli64 show | grep 16i | awk '{print $1}')
                        D=$($bindir/storcli/storcli64 show | grep 24i | awk '{print $1}')
                        for c in $C ; do $bindir/storcli/storcli64 /c$c download file=$dir/SAS9305_16i_IT_P.bin ; done
                        for d in $D ; do $bindir/storcli/storcli64 /c$d download file=$dir/SAS9305_24i_IT_P.bin ; done
                fi
                let c3=c3+1
                ;;
        3316)
                if [[ $c4 -eq 0 ]]; then
                        if [[ ! -f $dir/mr3316.rom ]]; then curl --silent -o $dir/mr3316fw.rom $SAS3316FW ; fi
                        C=$($bindir/storcli/storcli64 show | grep 16i | awk '{print $1}')
                        for c in $C ; do $bindir/storcli/storcli64 /c$c download file=$dir/mr3316fw.rom ; done
                fi
                let c4=c4+1
                ;;
        3324)
                if [[ $c5 -eq 0 ]]; then
                        if [[ ! -f $dir/mr3316.rom ]]; then curl --silent -o $dir/mr3316fw.rom $SAS3316FW ; fi
                        C=$($bindir/storcli/storcli64 show | grep 24i | awk '{print $1}')
                        for c in $C ; do $bindir/storcli/storcli64 /c$c download file=$dir/mr3316fw.rom ; done
                fi
                let c5=c5+1
                ;;
        esac
done
echo "--------------------------"
echo "Cleaning up..."
rm -rf $dir
echo "--------------------------"
echo "System reboot required for updates to take effect.... Shutting down in 10 seconds "
sleep 10
#reboot
