#!/bin/bash
# Description: H3C R4900 G3 BIOS Fw Update
# 2020-04-29: 拆分BMC/BIOS/RAID升级包（allin.han）
# 2021/08/16 拆分BIOS更新和配置文件更新功能

currentDir=`pwd`
resultLog="`pwd`/result.log"

source "${currentDir}/bios.conf"

function getIpmiIPAndPasswd() {
	ipmi_ip=`ipmitool  lan print 1 | grep "IP Address  "| awk '{print $NF}'`
	if [ "${ipmi_ip}" != "0.0.0.0" ]; then
		SN=$(dmidecode -s system-serial-number |grep -v ^#  | sed s/[[:space:]]//g)
		ipmi_password=$(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 $ipmi_password ]] || exit 1
	fi
	if [ "${ipmi_password}" == "" ]; then
		ipmi_password="ucloud.cn"
	fi
}

function biosVersionCheckAndUpdate() {
	biosVersion=`dmidecode -t bios| grep "Version"|awk '{print $NF}'`
	if [ "${biosVersion}" != "${biosTargetVersion}" ]; then
		bash curl.sh -I ${ipmi_ip} -U root -P ${ipmi_password} -T BIOS -F ${biosFile} -R 2 -K 0 -V 1
	fi
}

getIpmiIPAndPasswd

biosVersionCheckAndUpdate
