#!/bin/bash
CPWD=$(cd `dirname $0`;pwd)
which ipmitool >/dev/null 2>&1
if [ $? -ne 0 ];then
        yum install *ipmi* *IPMI* -y
fi
rpm -qa |grep jq
if [ $? = 0 ];then
	echo "The package has been installed"
else
	yum install jq -y
fi
ipmi_ip=$(ipmitool lan print |grep "IP Address  " |awk -F ": " '{print$2}')
SN=$(dmidecode -s system-serial-number |grep -v ^#  | sed s/[[:space:]]//g)
passwd=$(curl -s -H "Content-Type:application/json" -X POST --data  '{"data":  ["'${SN}'"],"type": "SN"}' http://udcm.ucloudadmin.com/api/ipmi/v1/getpwd/ | jq -r '.[]|.IPMIPassword')
[[ -n $passwd  ]] || exit 1
netmask=$(ipmitool lan print |grep "Subnet Mask" |awk -F ": " '{print$2}')
gateway=$(ipmitool lan print |grep "Default Gateway IP" |awk -F ": " '{print$2}')
function bmcUpdating() {
	bmc_file=`ls MOORETHREADS20230327_T1DMG20414.ima`
	chmod +x socflash_x64
	PN=`dmidecode -s system-product-name`
	BMC_Ver_main=`ipmitool mc info |grep "Firmware Revision" |awk '{print $NF}'`
	BMC_Ver_aux=`echo $(ipmitool mc info |grep -A4 "Aux Firmware Rev Info" |sed -n 2p |sed 's/\s*//g') |awk -F "x" '{print $2}'`
	BMC_Ver=${BMC_Ver_main}"."${BMC_Ver_aux}
	if [[ $PN} =~ "MCCX D800" ]] && [[ x"${BMC_Ver}" != x"2.04.14" ]];then
		echo y |./socflash_x64 if=${bmc_file}
		sleep 180
	fi
}

function ipmiSetting() {
	echo "*** Config IPMI related settings ***"
	if [ $(ipmitool lan print |grep "IP Address Source" |awk -F ": " '{print$2}' |awk -F " " '{print$1}') = "Static" ];then
		echo "IPMI has been set completely"
	elif [ $(ipmitool lan print |grep "Default Gateway IP" |awk -F ": " '{print$2}') = "0.0.0.0" ];then
	        ipmitool lan set 1 ipsrc static
        	ipmitool lan set 1 ipaddr $ipmi_ip
        	ipmitool lan set 1 netmask $netmask
        	ipmitool lan set 1 defgw ipaddr $gateway
        	#ipmitool user set password 2 $passwd
	else
		ipmitool lan set 1 ipsrc static
		ipmitool lan set 1 ipaddr $ipmi_ip
		#ipmitool user set password 2 $passwd
	fi

}
bmcUpdating
ipmiSetting
