#!/bin/bash
ipmi_ip=$(ipmitool lan print |grep "IP Address  " |awk -F ": " '{print$2}')
#password=`curl -H "Content-Type:application/json" -X POST --data  "{\"data\":  [\"${ipmi_ip}\"],\"type\": \"IPMI\"}" http://udcm.ucloudadmin.com/api/ipmi/v1/getpwd/ | jq "." | grep 'IPMIPassword' | awk '{print $NF}' | sed  's/^//g' | sed 's/^,$//g'`
SN=$(dmidecode -s system-serial-number |grep -v ^#  | sed s/[[:space:]]//g)
passwd=$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() {
	echo y|./socflash_x64 if=ROME2D32GM-2T_1.36.05.ima
}

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 -iE 'IP Address\s*:'|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 $gateway
        fi
}

BMCUpdating
IPMISetting

