#!/bin/bash

#$1是机型， $2是动作，当动作是install的时候才会执行生效
ManufacturerType=$1
action=$2

#当前脚本路径
CURRENT_DIR="`cd $(dirname $0);pwd`"
cd  ${CURRENT_DIR}

function install_racadm() {
	cd  ${CURRENT_DIR}
	tar -zxvf iDRACTools.tar
	cd ${CURRENT_DIR}/iDRACTools/racadm
	./install_racadm.sh
}

function uninstall_racadm() {
	#tar -zxvf iDRACTools.tar
	cd ${CURRENT_DIR}/iDRACTools/racadm
	./uninstall_racadm.sh
}

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

function dell_bios() {
	cd  ${CURRENT_DIR}
	#CONF
	DELL_R220="R220_20220217.xml"
	DELL_R720XD="R720XD_20220217.xml"
	DELL_R740XD="R740XD_20220217.xml"
	DELL_R7525="R7525_20240930.xml"

	Manufacture=`dmidecode -s system-manufacturer`
	ProductName=`dmidecode -s system-product-name`

	if [[ $Manufacture == "Dell Inc." ]]
	then
        	echo "DELL服务器"
        	if [[ $ProductName == "PowerEdge R220" ]]
        	then
                	DELL_BIOS=$DELL_R220
			#配置文件导入导出兼容性太差，且机器太老，暂时注释掉
			#/opt/dell/srvadmin/sbin/racadm set -f ${DELL_BIOS} -t xml
       		elif [[ $ProductName == "PowerEdge R720xd" ]]
        	then
			#配置文件导入导出兼容性太差，且机器太老，暂时注释掉
                	DELL_BIOS=$DELL_R720XD
			#/opt/dell/srvadmin/sbin/racadm set -f ${DELL_BIOS} -t xml
        	elif [[ $ProductName == "PowerEdge R740xd" ]]
        	then
                	DELL_BIOS=$DELL_R740XD
			/opt/dell/srvadmin/sbin/racadm set -f ${DELL_BIOS} -t xml -b "noreboot"
        	elif [[ $ProductName == "PowerEdge R7525" ]]
        	then
                	DELL_BIOS=$DELL_R7525
			/opt/dell/srvadmin/sbin/racadm set -f ${DELL_BIOS} -t xml -b "noreboot"
        	else
                	echo "无$ProductName的setbios配置文件"
			exit 1
        	fi
	else
        	echo "制造厂商非 Dell Inc. 停止导入"
	fi
}

#安装DELL的工具racadm
install_racadm

#获取IPMI和密码
#get_ipmi_ip_and_passwd

#导入BIOS配置文件
dell_bios
sleep 10

#卸载DELL的工具racadm
uninstall_racadm

#需要重启生效
#reboot
