#!/bin/bash
##################################################################################################################################################
#Program:
#	This shell is used to update the firmware of server HP DL380 Gen9, the FW types are include BIOS,BMC,HDD Controller,HDD,SSD and powerpic.	
#Author: Sky.Zhang
#Date: 2018/09/11
#Version: 1.0
#Chage List: 
###################################################################################################################################################

currentDir=$(cd $(dirname $0); pwd)
biosUpdate=0
bmcUpdate=0
hddControllerUpdate=0
hddUpdate=0
ssdUpdate=0
powerpicUpdate=0
manufacturer=`dmidecode -s system-manufacturer | tail -n 1 | tr A-Z a-z`
productName=`dmidecode -s system-product-name | tail -n 1 | tr A-Z a-z | sed 's/ /_/g'`
tmpFile="${currentDir}/tmp.log"

function rpmsInstall() {
	yum install expect -y
	yum install *ipmi* *IPMI* -y
	rpm -ivh ${currentDir}/ssacli*.rpm
	rpm -ivh ${currentDir}/ssaducli*.rpm

	if [ $(ls /lib/modules|grep -ic el6) -ge 1 ]; then
		rpm -ivh ${currentDir}/hp-scripting-tools*el6*.rpm
	else
		rpm -ivh ${currentDir}/hp-scripting-tools*el7*.rpm
	fi
}

function configFileImport() {
	configFileName="${manufacturer}_${productName}.config"

	if [ -f "${currentDir}/${configFileName}" ]; then
		source ${currentDir}/${configFileName}
	else
		echo -e "The error code is 0x00.\nThere is no suitable config file to import."
		exit 0
	fi
}

################################################################
#BIOS/BMC/HDD Controller/powerpic FW information check.
###############################################################
##BIOS version check.
function biosCheck() {
	bios_version=`dmidecode -t bios | grep "Version" | awk '{print $2}'`
	bios_release_date=`dmidecode -t bios | grep "Release Date" | awk '{print $NF}'`
	if [ "${bios_version}" == "${BIOS_VERSION}" ] && [ "${bios_release_date}" == "${BIOS_RELEASE_DATE}" ]; then
		biosUpdate=0
		echo "The BIOS version is right."
	else	
		biosUpdate=1
	fi	
}

function bmcCheck() {
	bmc_version=""
	/etc/init.d/ipmi restart
	sleep 10s
	
	if [ "ls /dev/* | grep "/dev/ipmi0"" != "" ]; then
		bmc_version=`ipmitool mc info | grep "Firmware Revision" | awk '{print $NF}'`
	else
		echo -e "The ipmi service is in abnormal condition, can't check BMC version"
	fi

	if [ "${bmc_version}" == "${BMC_VERSION}" ];then
		bmcUpdate=0
		echo "The BMC version is right."
	else
		bmcUpdate=1
	fi
}

#
function hddControllerCheck() {

	ssacli ctrl all show | grep "in Slot" | cut -d '(' -f 1 | tr A-Z a-z > ${tmpFile}
	hdd_controller_count=`wc -l ${tmpFile} |awk '{print $1}'`
	types=`echo "${HDD_CONTROLLER_TYPE}" | sed 's/_/\n/g'`
	types_count=`echo "${HDD_CONTROLLER_TYPE}" | sed 's/_/\n/g' | wc -l | awk '{print $1}'`
		
	count=0
	for ((i=1;i<=${hdd_controller_count};i++))
	do
	
		controller_info=`sed -n ${i}p ${tmpFile}`
	echo "${controller_info}"
		for j in `echo ${types}`
		do	
		
			echo "${j}_${count}"	
			let count=count+1
			isMatch=`echo ${controller_info} | grep "${j} "`
			
			if [ "${isMatch}" != "" ]; then
				hddControllerUpdate=1
				break;
			else 
				hddControllerUpdate=0
				if [ ${count} -eq ${types_count} ]; then
					echo "The hdd controller type is not matches with config file, please checek first."
					break;
				fi
			fi
		done
	done

	rm -rf ${currentDir}/${tmpFile}
}

function powerpicCheck() {
	powerpic_version=`dmidecode | grep -A 1 "Power Management Controller Firmware" | tail -n 1 | awk '{print $NF}'`
	if [ "${powerpic_version}" != "${POWERPIC_VERSION}" ];then
		powerpicUpdate=1
	else
		echo "The powerpic fw version is right."
	fi
	
}

####################################################
#BIOS/BMC/HDD Controller/HDD/SSD/powerpic FW Update.
####################################################
function biosUpdate() {	
	if [ ${biosUpdate} -eq 1 ]; then
		rpm -ivh ${currentDir}/${BIOS_PACKAGE_NAME}.i386.rpm
		cd /usr/lib/i386-linux-gnu/${BIOS_PACKAGE_NAME}

		expect ${currentDir}/bios.exp

		sleep 20s
		cd ${currentDir}

		rpm -e ${BIOS_PACKAGE_NAME}
	fi	
}

#BMC version update
function bmcUpdate() {
	if [ ${bmcUpdate} -eq 1 ];then
		rpm -ivh ${currentDir}/${BMC_PACKAGE_NAME}.i386.rpm
                cd /usr/lib/i386-linux-gnu/${BMC_PACKAGE_NAME}
                
		if [ "`ls | grep "^setup$"`" != "" ]; then
                        echo y | ./setup -f
                else
                        echo y | ./hpsetup -f
                fi
		
		sleep 60s
                cd ${currentDir}

                rpm -e ${BMC_PACKAGE_NAME}
	fi
}
#
#HDD Coptroller fw version
function hddControllerUpdate() {
	if [ ${hddControllerUpdate} -eq 1 ];then
		
		rpm -ivh ${currentDir}/${HDD_CONTROLLER_PACKAGE_NAME}*.rpm
	        cd /usr/lib/x86_64-linux-gnu/${HDD_CONTROLLER_PACKAGE_NAME}
                if [ "`ls | grep "^setup$"`" != "" ]; then
                        echo a | ./setup -f
                else
                        echo a | ./hpsetup -f
                fi 

        	sleep 20s        	
		rpm -e ${HDD_CONTROLLER_PACKAGE_NAME}

	fi
}
#
#HDD model and fw version check and update.
function hddUpdate() {
	cd ${currentDir}
	rpmCount=`ls | grep -c "firmware-hdd"`
	ls | grep "firmware-hdd" > ${tmpFile} 
	
	for i in `cat ${tmpFile}`
	do
		rpm -ivh ${currentDir}/${i}
		packageName=`echo ${i} | sed 's/.x86_64.rpm//'`
		cd /usr/lib/x86_64-linux-gnu/${packageName}		
	        if [ "`ls | grep "^setup$"`" != "" ]; then
                        echo a | ./setup -f
                else
                        echo a | ./hpsetup -f
                fi
 
        	sleep 20s
        	rpm -e ${packageName}
	done
	rm -rf ${tmpFile}	
}

#Powerpic versionupdate
function powerpicUpdate() {
	if [ ${powerpicUpdate} -eq 1 ]; then
		rpm -ivh ${currentDir}/${POWERPIC_PACKAGE_NAME}*.rpm
		cd /usr/lib/i386-linux-gnu/${POWERPIC_PACKAGE_NAME}
		echo y | ./hpsetup -f 
		sleep 20s
		rpm -e ${POWERPIC_PACKAGE_NAME}
	fi
}

function biosSettingLoadDef() {
	assetTag=`dmidecode  -s chassis-asset-tag | tail -n 1 | sed 's/ //g'`        
        sed -i s?${DEFAULT_ASSET_TAG}?${assetTag}?g ${currentDir}/${manufacturer}_${productName}_bios.dat
	/sbin/conrep -l -f ${currentDir}/${manufacturer}_${productName}_bios.dat
}


rpmsInstall
configFileImport
biosCheck
biosUpdate
bmcCheck
bmcUpdate
hddControllerCheck
hddControllerUpdate
hddUpdate
powerpicCheck
powerpicUpdate
biosSettingLoadDef


