#!/bin/bash
#Change List
#2021/08/21 
#1. 删除BIOS配置文件对比检查功能
#2. 更新BIOS配置文件：
#2.1. R4700 G3 和R4900 G3 为1个配置文件。
#2.2. R4300 G3 一个配置文件。
#2021/12/27_by_Alan.chen
#2.3. 新增R4950 G5机型配置文件
#2023/07/06 by Sviper.Yan
#2.4. 新增R5500 G5机型配置文件
#2023/09/14 by carl.jia
#修改脚本调用逻辑，取消传入参数，通过fru判断机型
#更新R4900 G3 2.00.62 版本BIOS配置
ManufacturerType=`cat /tmp/os_init/cmdb.info |grep -i 'ManufactureType'|awk -F : '{print $2}'`

# import use
H3C_R4900_G3_CfgFile="R4900G3_20230913_BIOS.cfg"
H3C_R4300_G3_CfgFile="R4300G3_20210816_BIOS.cfg"
H3C_G2_CfgFile="R49G2_20210825_BIOS.cfg"
H3C_R4950_G5_CfgFile="R4950G5_20211210_BIOS.cfg"
H3C_R5500_G5_CfgFile="R5500G5_20230914_BIOS_5_65.cfg"
H3C_R5300_G5_CfgFile="R5300G5_20231116_BIOS_5_58.cfg"
H3C_R5300_G6_CfgFile_efi="R5300G6_20240821_EFI.cfg"
H3C_R5300_G6_CfgFile="R5300G6_20240223_BIOS.cfg"
H3C_R4900_G5_CfgFile="R4900G5_20240409_BIOS.cfg"
H3C_R5500_G6_CfgFile="R5500G6_20240724_BIOS.cfg"
H3C_R4950_G5M_CfgFile="R4950G5-M_20250225_BIOS.cfg"
H3C_R4300_G5_CfgFile="R4300G5_20250617_BIOS.cfg"
H3C_R4900_G6_CfgFile="R4900G6_Ultra_efi_250908.cfg"
G3_SCE_TOOL="SCELNX_64_V5.04.1136"
R5500_SCE_TOOL="SCELNX_64_5.05.01.0002_2021"
R4900G6_SCE_TOOL="SCELNX_64"

R4300_G3_STAND_SN="210235A3THH213000622"
R4900_G3_STAND_SN="210200A00QH189002112"
R4300_G3_STAND_ASSET_TAG="UCSV202103181511"
R4900_G3_STAND_ASSET_TAG="UCSV01201809049338"

function biosConfigFix() {
	SN="`dmidecode -s chassis-serial-number`"
	ASSET_TAG="`dmidecode -s chassis-asset-tag`"
	if [[ "${ManufacturerType}" =~ "R4900 G3" ]] || [[ $ManufacturerType =~ "R4700 G3" ]];then
		sed -i "s/${R4900_G3_STAND_SN}/${SN}/g" ${H3C_R4900_G3_CfgFile}
		sed -i "s/${R4900_G3_STAND_ASSET_TAG}/${ASSET_TAG}/g" ${H3C_R4900_G3_CfgFile}
	elif [[ "${ManufacturerType}" =~ "R4300 G3" ]];then
		sed -i "s/${R4300_G3_STAND_SN}/${SN}/g" ${H3C_R4300_G3_CfgFile}
		sed -i "s/${R4300_G3_STAND_ASSET_TAG}/${ASSET_TAG}/g" ${H3C_R4300_G3_CfgFile}
	fi
}

function install() {
	if [[ "${ManufacturerType}" =~ "R4900 G2" ]];then
		if [ "`uname -r | grep -E "4.1.0|4.14.0" | grep el7`" != "" ];then
			./${G3_SCE_TOOL} /i /s ${H3C_G2_CfgFile}	
		elif [ "`uname -r | grep  "5.10.0" | grep el7`" != "" ];then
			echo -e "The kernel version is `uname -r `, SCE tool not support the kernel 5.10.0-x.\nAnd suggest change kernel to 4.1.0 or 4.14.0, and run import the bios config file again. "
		elif [ "`uname -r | grep el6`" != "" ];then	
			echo -e "The kernel version is `uname -r `, SCE tool not support the OS CentOS6.3.\nAnd suggest change OS to CentOS 7.4, and run import the bios config file again. "
		fi
	elif [[ "${ManufacturerType}" =~ "R4900 G3" ]] || [[ $ManufacturerType =~ "R4700 G3" ]];then
		./${G3_SCE_TOOL} /i /s ${H3C_R4900_G3_CfgFile}
	elif [[ "${ManufacturerType}" =~ "R4300 G3" ]];then
		./${G3_SCE_TOOL} /i /s ${H3C_R4300_G3_CfgFile}
	elif [[ "${ManufacturerType}" =~ "R4950 G5" ]];then
		./${G3_SCE_TOOL} /i /s ${H3C_R4950_G5_CfgFile}
	elif [[ "${ManufacturerType}" =~ "R5500 G5" ]];then
		./${R5500_SCE_TOOL} /i /s ${H3C_R5500_G5_CfgFile}
	elif [[ "${ManufacturerType}" =~ "R5300 G5" ]];then
                ./${R5500_SCE_TOOL} /i /s ${H3C_R5300_G5_CfgFile}
	elif [[ "${ManufacturerType}" =~ "R5300 G6" ]];then
		if [[ -d "/sys/firmware/efi" ]];then
 	               ./${R5500_SCE_TOOL} /i /s ${H3C_R5300_G6_CfgFile_efi}
    		else
 	               ./${R5500_SCE_TOOL} /i /s ${H3C_R5300_G6_CfgFile}
    		fi
	elif [[ "${ManufacturerType}" =~ "R4900 G5" ]];then
                ./${R5500_SCE_TOOL} /i /s ${H3C_R4900_G5_CfgFile}
	elif [[ "${ManufacturerType}" =~ "R5500 G6" ]];then
		./${R5500_SCE_TOOL} /i /s ${H3C_R5500_G6_CfgFile}
	elif [[ "${ManufacturerType}" =~ "R4950 G5-M" ]];then
                ./${R5500_SCE_TOOL} /i /s ${H3C_R4950_G5M_CfgFile}
	elif [[ "${ManufacturerType}" =~ "R4300 G5" ]];then
                ./${R5500_SCE_TOOL} /i /s ${H3C_R4300_G5_CfgFile}
        elif [[ "${ManufacturerType}" =~ "R4900 G6 ULTRA" ]];then
                if [[ -d "/sys/firmware/efi" ]];then
                       ./${R4900G6_SCE_TOOL} /i /s ${H3C_R4900_G6_CfgFile}
                fi
	else
		exit 1
	fi
}

biosConfigFix
install
