#!/bin/bash

# Description: H3C R4700 G3 Raidcard Fw Update
# 2020-04-29: 拆分BMC/BIOS/RAID升级包（allin.han）

currentDir=`pwd`
resultLog="`pwd`/result.log"
arcconfPath=`which arcconf`
nvramFlashTool="ssflash"
source "${currentDir}/raid.conf"

function hddControllerCheckAndUpdate() {
	controllerCount=`${arcconfPath} list | grep 'Controllers found' | awk '{print $NF}'`
	for ((i=1;i<=${controllerCount};i++))
	do
	        hddControllerMode="`${arcconfPath} getconfig ${i} ad | grep "Controller Model"| awk '{print $NF}'`"
        	hddControllerVersion="`${arcconfPath} getversion 1 | grep '^Firmware'   | cut -d ':' -f 2 | sed  's/^ //g'`"
		slotNumber="`${arcconfPath} getconfig ${i} ad | grep -i slot | awk '{print $NF}'`"

        	if [  "`echo ${hddControllerMode} | grep 'PM8060-RAID'`" != "" ] ;then
			if [ "${hddControllerVersion}" !=  "${PM8060_FW_VERSION}" ] ; then
				${arcconfPath} romupdate ${i} ${currentDir}/${PM8060_FW_FILE} force noprompt
			fi
			sleep 20s
			${arcconfPath} setcache ${i} deviceall drivespecific noprompt
			sleep 3s
			${arcconfPath} setbiosparams ${i} backplanemode 1
			sleep 3s
			${arcconfPath} setcontrollermode ${i} 0
			sleep 3s
			${arcconfPath} CONSISTENCYCHECK ${i} period 14 noprompt
	        elif [ "${hddControllerMode}" == "P460-M2" ];then
			if [ "${hddControllerVersion}" !=  "${P460M2_FW_VERSION}" ]; then
				${arcconfPath} romupdate ${i} ${currentDir}/${P460M2_FW_FILE} force noprompt
			fi
			
			sleep 20s
			./${nvramFlashTool}  --ctrl slot=${slotNumber} --flash-nvram ./${P460M2_NVRAM_FILE} --preserve
			sleep 3s
			${arcconfPath} setcontrollermode ${i} 5
			sleep 3s
			${arcconfPath} CONSISTENCYCHECK ${i} on 3
			sleep 3s
			${arcconfPath} consistencycheck ${i} parallelcount 1
			sleep 3s
			ldCount=`${arcconfPath} getconfig ${i} ld | grep 'Logical Device number' -c`
			for ((j=0;j<${ldCount};j++))
			do
				${arcconfPath} setarrayparam ${i} ${j} ssdiobypass disable
			done
			${arcconfPath} setcache ${i} logicaldrive all con
			sleep 3s
			${arcconfPath} setcache ${i} DRIVEWRITECACHEPOLICY Configured 0 Unconfigured 0 hba 0
		elif [ "${hddControllerMode}" == "H460-M1" ];then
			if [ "${hddControllerVersion}" !=  "${P460M2_FW_VERSION}" ]; then
        	        	${arcconfPath} romupdate ${i} ${currentDir}/${P460M2_FW_FILE} force noprompt
			fi
			./${nvramFlashTool}  --ctrl slot=${slotNumber} --flash-nvram ./${H460M1_NVRAM_FILE} --preserve
			 sleep 3s
                        ${arcconfPath} setcontrollermode ${i} 5
			sleep 3s
			${arcconfPath} CONSISTENCYCHECK ${i} on 3
			sleep 3s
			${arcconfPath} consistencycheck ${i} parallelcount 1
			sleep 3s
			${arcconfPath} setcache ${i} DRIVEWRITECACHEPOLICY Configured 0 Unconfigured 0 hba 0
	        elif [ "${hddControllerMode}" != "" ]; then
        	        echo "The controller mode is ${hddControllerMode} and the firmware version is ${hddControllerVersion}." >> ${resultLog}
	        else
      			echo "There is no HDD Controller." >> ${resultLog}
		fi
	done

}

hddControllerCheckAndUpdate
