#!/bin/sh
#$1:params 与Yafuflash的使用方法一致, 参数加上""即可,具体参考使用说明

#usage example: ./Yafuflash.sh "-nw -ip 100.2.74.184 -u admin -p admin ISBMC_EagleStream_1.26.00_Standard_20221017.hpm -pc"
#               ./Yafuflash.sh "-cd ISBMC_EagleStream_1.26.00_Standard_20221017.hpm -pc"
#               ./Yafuflash.sh "-kcs ISBMC_EagleStream_1.26.00_Standard_20221017.hpm -pc"

params=$1

if [[ $params =~ "-nw" ]]
then 
	echo "OOB"

	#获取-ip右边的所有字符串
	strip=${params#*"-ip"}
	#echo "$strip"
	#删除左边所有空格
	strip=$(echo "$strip" | sed 's/^[ ]*//g')
	#echo "$strip"
	#获取右边开始最后一个空格左边的所有字符串,获取到ip地址
	BMC_IP=${strip%%" "*}
	#echo $BMC_IP

	#获取-u右边的所有字符串
	strusrname=${params#*"-u"}
	#echo "$strusrname"
	#删除左边所有空格
	strusrname=$(echo "$strusrname" | sed 's/^[ ]*//g')
	#echo "$strusrname"
	#获取右边开始最后一个空格左边的所有字符串,获取到用户名
	BMC_USERNAME=${strusrname%%" "*}
	#echo $BMC_USERNAME

	#获取-p右边的所有字符串
	strpwd=${params#*"-p"}
	#echo "$strpwd"
	#删除左边所有空格
	strpwd=$(echo "$strpwd" | sed 's/^[ ]*//g')
	#echo "$strpwd"
	#获取右边开始最后一个空格左边的所有字符串,获取到密码
	BMC_PASSWORD=${strpwd%%" "*}
	#echo $BMC_PASSWORD

	IPMI="ipmitool -I lanplus -H $BMC_IP -U $BMC_USERNAME -P $BMC_PASSWORD raw 0x3c 0x42 0x01"
	echo $IPMI
	$IPMI
	if [[ $? -eq 0 ]] ; then
		CPU_PLATFORM=`$IPMI |awk -F " " '{print $2}'`
		SOC_PLATFORM=`$IPMI |awk -F " " '{print $3}'`
		CODEBASE_PLATFORM=`$IPMI |awk -F " " '{print $4}'`
		#echo $CPU_PLATFORM $SOC_PLATFORM $CODEBASE_PLATFORM
		if [[ $CODEBASE_PLATFORM -eq 0 ]]; then
			#echo "AMI"
			if [[ $CPU_PLATFORM -eq 0 ]]; then
				#echo "INTLE"
				YAFUFLASH="./M7_Yafuflash $params"
				echo $YAFUFLASH
				$YAFUFLASH
			elif [[ $CPU_PLATFORM -eq 1 ]]; then
				#echo "AMD"
				YAFUFLASH="./A7_Yafuflash $params"
				echo $YAFUFLASH
				$YAFUFLASH
			elif [[ $CPU_PLATFORM -eq 2 ]]; then
				#echo "ARM"
				#R7带外与A7一致
				YAFUFLASH="./A7_Yafuflash $params"
				echo $YAFUFLASH
				$YAFUFLASH
			fi
		fi
	else
		#echo "ARM" 
		#ARM BMC暂时没有raw 0x3c 0x42 0x01命令
		#R7带外与A7一致
		YAFUFLASH="./A7_Yafuflash $params"
		echo $YAFUFLASH
		$YAFUFLASH
	fi

else
	echo "Inband"
	IPMI="ipmitool raw 0x3c 0x42 0x01"
	echo $IPMI
	$IPMI
	if [[ $? -eq 0 ]] ; then
		CPU_PLATFORM=`$IPMI |awk -F " " '{print $2}'`
		SOC_PLATFORM=`$IPMI |awk -F " " '{print $3}'`
		CODEBASE_PLATFORM=`$IPMI |awk -F " " '{print $4}'`
		#echo $CPU_PLATFORM $SOC_PLATFORM $CODEBASE_PLATFORM
		if [[ $CODEBASE_PLATFORM -eq 0 ]]; then
			#echo "AMI"
			if [[ $CPU_PLATFORM -eq 0 ]]; then
				#echo "INTLE"
				YAFUFLASH="./M7_Yafuflash $params"
				echo $YAFUFLASH
				$YAFUFLASH
			elif [[ $CPU_PLATFORM -eq 1 ]]; then
				#echo "AMD"
				YAFUFLASH="./A7_Yafuflash $params"
				echo $YAFUFLASH
				$YAFUFLASH
			elif [[ $CPU_PLATFORM -eq 2 ]]; then
				#echo "ARM"
				YAFUFLASH="./R7_Inband_Yafuflash $params"
				echo $YAFUFLASH
				$YAFUFLASH
			fi
		fi
	else
		#echo "ARM" 
		#ARM BMC暂时没有raw 0x3c 0x42 0x01命令
		YAFUFLASH="./R7_Inband_Yafuflash $params"
		echo $YAFUFLASH
		$YAFUFLASH
	fi
fi
exit 0
