#!/bin/bash
CPWD=$(cd `dirname $0`;pwd)
rm -rf $CPWD/bmc_lan_info $CPWD/bmc_ip_info_temp $CPWD/bmc_ip_info
which ipmitool >/dev/null 2>&1
if [ $? -ne 0 ];then
        yum install *ipmi* *IPMI* -y
fi

Preserve_bmc_config(){
ipmitool lan print 1 > $CPWD/bmc_lan_info
cat $CPWD/bmc_lan_info |grep "IP Address" |grep -v Source |awk 'NF=NF-0{print $NF}' >> $CPWD/bmc_ip_info_temp
cat $CPWD/bmc_lan_info |grep "Subnet Mask"|awk 'NF=NF-0{print $NF}' >> $CPWD/bmc_ip_info_temp
cat $CPWD/bmc_lan_info |grep "Default Gateway IP"|awk 'NF=NF-0{print $NF}' >> $CPWD/bmc_ip_info_temp
cat $CPWD/bmc_ip_info_temp |tr -s '\n' ' ' > $CPWD/bmc_ip_info
ipmitool fru > $CPWD/bmc_fru
ipmitool user list 1 > $CPWD/bmc_user
}

Update_bmc(){
./socflash_x64 if=BMCV126.ima
sleep 120
}

Write_bmc_mac(){
local _MAC1=`cat $CPWD/bmc_lan_info |grep "^MAC Address" |awk '{print $NF}'  |awk -F":" '{print $1}'`
local _MAC2=`cat $CPWD/bmc_lan_info |grep "^MAC Address" |awk '{print $NF}'  |awk -F":" '{print $2}'`
local _MAC3=`cat $CPWD/bmc_lan_info |grep "^MAC Address" |awk '{print $NF}'  |awk -F":" '{print $3}'`
local _MAC4=`cat $CPWD/bmc_lan_info |grep "^MAC Address" |awk '{print $NF}'  |awk -F":" '{print $4}'`
local _MAC5=`cat $CPWD/bmc_lan_info |grep "^MAC Address" |awk '{print $NF}'  |awk -F":" '{print $5}'`
local _MAC6=`cat $CPWD/bmc_lan_info |grep "^MAC Address" |awk '{print $NF}'  |awk -F":" '{print $6}'`
#enable BMC MAC SET mode
ipmitool raw 0x0c 0x01 0x01 0xc2 0x00
#Write MAC to BMC
ipmitool raw 0x0c 0x01 0x01 0x05 0x${_MAC1} 0x${_MAC2} 0x${_MAC3} 0x${_MAC4} 0x${_MAC5} 0x${_MAC6}
}

Write_bmc_ip(){
local bmc_info_ip=`awk '{print $1}' $CPWD/bmc_ip_info`
local bmc_info_netmask=`awk '{print $2}' $CPWD/bmc_ip_info`
local bmc_info_defgw=`awk '{print $3}' $CPWD/bmc_ip_info`
ipmitool lan set 1 ipsrc static
ipmitool lan set 1 ipaddr ${bmc_info_ip}
ipmitool lan set 1 netmask ${bmc_info_netmask}
ipmitool lan set 1 defgw ipaddr ${bmc_info_defgw}
}

Write_bmc_user(){
local bmc_info_ip=`awk '{print $1}' $CPWD/bmc_ip_info`
local SN=$(dmidecode -s system-serial-number |grep -v ^#  | sed s/[[:space:]]//g)
local bmc_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 $bmc_passwd  ]] || exit 1

#local bmc_passwd=`curl -H "Content-Type:application/json" -X POST --data  "{\"data\":  [\"${bmc_info_ip}\"],\"type\": \"IPMI\"}" http://udcm.ucloudadmin.com/api/ipmi/v1/getpwd/ |jq "."|grep 'IPMIPassword'|awk '{print $NF}' |sed -e 's/,//' -e 's/^\"//' -e 's/\"$//'`
local root_user_id=`cat $CPWD/bmc_user |grep "root" |awk '{print $1}'`
ipmitool user set name ${root_user_id} root
ipmitool user set password ${root_user_id} ${bmc_passwd} 
}
cd $CPWD
if [[ -e /usr/bin/jq ]];then
        echo "jason tool jq is already installed"
else
        yum install -y jq
        yum install -y jq
fi
service ipmi restart
ipmitool lan print 1 > ipmi_info.txt
if [[ -s ipmi_info.txt ]];then
        Preserve_bmc_config
        Update_bmc
        service ipmi restart
        Write_bmc_mac
        sleep 60
        Write_bmc_ip
        sleep 30
        Write_bmc_user
        sleep 10
	ipmitool mc reset cold
	sleep 180	
else
        echo "/dev/ipmi0 doesn't exist, please check bmc status"
        exit 1
fi
