#!/bin/bash	
dir=$(cd $(dirname $0);pwd)
ipmi_ip=$(ipmitool lan print 1|grep 'IP Address  '|awk '{print $NF}')
netmask=$(ipmitool lan print 1|grep 'Subnet Mask  '|awk '{print $NF}')
defgw=$(ipmitool lan print 1|grep 'Default Gateway IP  '|awk '{print $NF}')
user_id=3
passwd=$1
#passwd=ucloud.cn


function BMCUpdating() {
    ./Yafuflash -cd ISBMC_B_Genoa_2.03.02_Standard_IEISYSTEMS_20241018.hpm -pc
}

function CreateUser() {
	if ipmitool user list 1 | grep -q "root";then
		echo "用户root已存在"
                user_id=$(ipmitool user list 1 |grep "root" |awk '{print $1}')
	else
		ipmitool user set name $user_id root
		ipmitool user enable $user_id
		ipmitool user set password $user_id ucloud.cn
		ipmitool user priv $user_id 4 1
		ipmitool channel setaccess 1 $user_id callin=on link=on ipmi=on
	fi
}


function IPMISetting() {
    local Address_type=$(ipmitool lan print |grep "IP Address Source" |awk -F ": " '{print$2}' |awk -F " " '{print$1}')
    local Now_ipmi_ip=$(ipmitool lan print |grep "IP Address  " |awk -F ": " '{print$2}')
    echo "*** Config IPMI related settings ***"

    if [[ $Address_type == "Static" ]] && [[ $Now_ipmi_ip == $ipmi_ip  ]];then
	echo "IPMI don't require configuration"
    else
        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 $defgw
        ipmitool user set name $user_id root
        ipmitool user set password $user_id "$passwd"
    fi
}

BMCUpdating
sleep 180
CreateUser
sleep 3
IPMISetting
