#!/bin/bash
#this shell script is used to update fru SN & AN in pangu
#author:carl.jia
#sum version:2.1

Manufacture=`cat /tmp/os_init/cmdb.info |grep -i 'Manufacture:'|awk -F : '{print $NF}'`
ManufactureType=`cat /tmp/os_init/cmdb.info |grep -i 'ManufactureType:'|awk -F : '{print $NF}'`
SN=`cat /tmp/os_init/cmdb.info |grep "SN" |cut -d ":" -f 2`
AN=`cat /tmp/os_init/cmdb.info |grep "AN" |cut -d ":" -f 2`
IPMI_IP=`ipmitool lan print 1 |grep -i 'IP Address'|sed 1d|sed 's/ //g'|awk -F : '{print $2}'`


#厂商列表
list=("TTY","CHBESTER" "STONEGROUP" "SUMA" "H3C" "INSPUR" "DELL" "POWERLEADER" "SUPERPHI" "HUAWEI" "XFUSION" "PUERSAI" "HUAQIN" "ENGINETECH" "KUNQIAN" "NETTRIX" "ASUS" "IEISYSTEM" "GIGABYTE" "FOXCONN" "ZTE" "SUPERCLOUD" "SUPERMICRO" "HUAKUN" "LENOVO" "POWERLEADER" "SUGON" "SUMA")  

if [[ "${list[@]}" =~ "${Manufacture}" ]]; then    
	echo "已匹配到厂商"
else
	echo "未匹配到厂商，可能是新厂商或厂商字段不匹配"
	exit 1
fi

Comman_FRU(){
	ipmitool fru edit 0 field p 4 "$SN"
	ipmitool fru edit 0 field p 5 "$AN"
}

H3C_FRU(){
	bash H3C_UpdateFru.sh -K PAT "$AN"
	bash H3C_UpdateFru.sh -K PS "$SN"
}

SUPERMIRCO_FRU(){
	./IPMICFG-Linux.x86_64 -fru PAT "$AN"
	./IPMICFG-Linux.x86_64 -fru PS "$SN"
	rm -rf DMI.txt
	./sum -c EditDmiInfo --file DMI.txt --item_type "Chassis" --item_name "Tag Number" --value "$AN" #待本地测试
	./sum -c EditDmiInfo --file DMI.txt --item_type "system" --item_name "Serial Number" --value "$SN"	#待本地测试
	./sum -c ChangeDmiInfo --file DMI.txt	#待本地测试
}

ASUS_FRU(){
	chmod +x ./amidelnx_64
	ipmitool raw 0x30 0x17 0x01	#unlock FRU protect
	ipmitool fru edit 0 field p 4 "$SN"
    	ipmitool fru edit 0 field p 5 "$AN"
	./amidelnx_64 /ss "$SN"
	./amidelnx_64 /ca "$AN"
}


YongQing_FRU(){
	chmod +x amidelnx_64
	chmod +x FRUTool_v2.12
	./FRUTool_v2.12 ID=0 PAT="$AN"
	./FRUTool_v2.12 ID=0 PSN="$SN"
	./amidelnx /ss "$SN"
	./amidelnx_64 /ca "$AN"
}


#HUAQIN_FRU()
#DELL_FRU()
#QUANTA_FRU()
#GENGZUN_FRU()

update_SUPERMIROC_lisence() #lisence过期后无法使用sum工具，需要重新激活，此方法适配V4 V5 V6机型，新平台待验证
{
 mac=`ipmitool lan print 1|grep -i "MAC Address"|awk  '{print $NF}'`
 key=`echo -n "$mac" | xxd -r -p | openssl dgst -sha1 -mac HMAC -macopt hexkey:8544E3B47ECA58F9583043F8 | awk '{print $2}' | cut -c 1-24 |sed 's/\(.\{4\}\)/\1-/g'| sed 's/.$//'`
	echo $key
./sum -c  ActivateProductKey --key $key

}


if echo $ManufactureType |grep -qwE 'ESC8000A-E12|ESC8000A-E11' ;then
	ASUS_FRU
elif [[ $ManufactureType == H8230 ]];then
	echo "华勤主板，未适配"
	exit 1
#elif [[ $ManufactureType == PR4908P ]]|| [[ $ManufactureType == R5215_A12 ]]||[[ $ManufactureType == W580-G20 ]]||[[ $ManufactureType == S780-G20 ]]||[[ $ManufactureType == X780-G30 ]];then	
elif echo $ManufactureType|grep -qwE 'PR4908P|R5215_A12|W580-G20|S780-G20|X780-G30';then
	update_SUPERMIROC_lisence
	SUPERMIRCO_FRU
elif [[ $Manufacture == H3C ]];then
	H3C_FRU
elif [[ $Manufacture == ASUS ]];then
	ASUS_FRU
elif [[ $Manufacture == SUPERPHI ]]||[[ $Manufacture == ENGINETECH ]]||[[ $Manufacture == KUNQIAN ]];then
	update_SUPERMIROC_lisence
	SUPERMIRCO_FRU
elif [[ $Manufacture == STONEGROUP ]]||[[ $Manufacture == CHBESTER ]];then
	YongQing_FRU
elif [[ $Manufacture == DELL ]];then
	echo "$Manufacture 未适配"
	exit 1
elif [[ $Manufacture == HP ]];then
	echo "$Manufacture 未适配"
	exit 1	
elif [[ $Manufacture == foxconn ]];then
	echo "$Manufacture 未适配"
	exit 1
elif [[ $Manufacture == HUAQIN ]];then
	echo "$Manufacture 未适配"
	exit 1
elif [[ $Manufacture == QUANTA ]];then
	echo "$Manufacture 未适配"
	exit 1
elif [[ $Manufacture == GENGZUN ]];then
	echo "$Manufacture 未适配"
	exit 1	
elif [[ $Manufacture == GIGABYTE ]];then
	echo "$Manufacture 未适配"
	exit 1
elif [[ $Manufacture == XFUSION ]];then
	echo "$Manufacture 未适配"
	exit 1
elif [[ $Manufacture == HUAWEI ]];then
	echo "$Manufacture 未适配"
	exit 1
else
	Comman_FRU

fi
