#!/bin/bash
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}')
#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() {
	uname -a |grep el6
	if [ "$?" = 0 ];then
		service ipmi restart
	else
		systemctl restart ipmi.service
	fi
	echo "*** BMC updating ***"
	bin_file=$(ls *.bin)
#	ipmitool bmc info |grep "4.26"
#	if [ $? = 0 ];then
#		echo "The FW version is the latest,no need to update..."
#		exit
#	else
		#echo YY|./socflash.sh if=$bin_file offset=0x02000000
	#	echo "Waiting for sleep to execute next script"
	#	sleep 10
		echo YY|./socflash.sh if=$bin_file offset=0x02000000
		sleep 30
		echo YY|./socflash.sh if=$bin_file
		ipmitool mc reset cold
		sleep 130
#	fi
}


function UserSetting() {
	ipmitool user list 1 |grep root
	if [ $? = 0 ];then
		echo "Root user is existed..."
	else
		#//Delete the default user "Admin"
		ipmitool raw 0x06 0x45 0x01 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
		ipmitool raw 0x06 0x45 0x02 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
		sleep 3
		echo "*** Config User related settings ***"
		#//Create new user named "root"
		ipmitool user set name 2 root
		#//Enabled the new user
		ipmitool user enable 2
		#//Set new password to root
		ipmitool user set password 2 "$passwd"
		#//Set the administrator priv to root
		ipmitool user priv 2 4 1
		#//Set the highest access priv to root
		ipmitool channel setaccess 1 2 callin=on link=on ipmi=on
		echo "Modified KVM using privilege"
		echo "Check the machine type"
		M_type=`ipmitool fru |grep "Product Name" |awk -F ":" '{print$2}' |awk NR==1 |awk -F " " '{print$1}'`
		echo "Machine Type: $M_type"
		if [ $M_type = SA5112M4 ];then
			echo "No need to set KVM using privilege"
		elif [ $M_type = SA5112M5 ];then
			if [ $(ipmitool raw 0x32 0xa4 0x02 |awk -F " " '{print$1}') = 03 ];then		
				echo "The KVM privilege has been set for root user"
			else
        			ipmitool raw 0x32 0xA3 0x02 0x03 0x00 0x00 0x00   #附予kvm权限
			fi
		elif [ $M_type = SA5212M5 ];then
			if [ $(ipmitool raw 0x32 0xa4 0x02 |awk -F " " '{print$1}') = 03 ];then 
                                echo "The KVM privilege has been set for root user"
                        else
                                ipmitool raw 0x32 0xA3 0x02 0x03 0x00 0x00 0x00   #附予kvm权限
                        fi
		fi

	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
}


function Deletefiles() {
	echo "Delete update packages"
	cd /tmp
	rm -rf info.* BMC
}

function RaidFWCheck() {
	echo "Check the RAID card FW"
	RAID_type=`lspci |grep SAS |awk -F " " '{print$11}'`
	echo "Raid_Type: $RAID_type"
	if [ $RAID_type = SAS3008 ];then
		FW_version=`/opt/op/sas3ircu 0 display |grep "Firmware version" |awk -F ":" '{print$2}'`
		if [ $FW_version == "16.00.10.00" ];then
			echo "The version is the latest,no need to update"
			exit
		else
			echo "Updating is coming..."
		fi
	elif [ $RAID_type = SAS3108 ];then
		/opt/MegaRAID/storcli/storcli64 /c0 show all "Firmware version"
		if [ $FW_version == "4.680.00-8376" ];then
			echo "The version is the latest,no need to update"
			exit
		else
			echo "Updating is coming..."
		fi
	elif [ $RAID_type = SAS2208 ];then
		/opt/MegaRAID/storcli/storcli64 /c0 show all "Firmware version"
		if [ $FW_version == "3.460.65-6268" ];then
			echo "The version is the latest,no need to update"
			exit
		else
			echo "Updating is coming..."
		fi
	fi
}

function RaidFWUpdating() {
	pwd=$(pwd |awk -F "/B" '{print$1}')
	echo "Start to update RAID card FW"
	if [ $RAID_type = SAS3008 ];then
		cd $pwd/RAID/LSI_3008 && ./update.sh
	elif [ $RAID_type = SAS3108 ];then
		cd $pwd/RAID/LSI_3108 && ./update.sh
	elif [ $RAID_type = SAS2208 ];then
		cd $pwd/RAID/LSI_2208 && ./update.sh
	fi
}
BMCUpdating
UserSetting
IPMISetting
#RaidFWCheck
#RaidFWUpdating
Deletefiles
