#!/bin/bash
chmod 777 afulnx_64 afulnx_64_4.14 afulnx_64_5.14.03.0039_2021
chmod 777 *.bin
CV=`ipmitool fru |grep "Chassis Part Number" |awk '{print $NF}'`
SN=`dmidecode -s system-serial-number |sed 's/\s*//g'`
BIOS_Ver=`dmidecode -t 0 |awk '/Version/{print $NF}' |sed 's/\s*//g'`
kernel_pre_version=`uname -r |awk -F"." '{print $1}'`
kernel_sub_version=`uname -r |awk -F"." '{print $2}'`

if [[ "$CV" =~ "V5" ]];then
	ipmitool fru edit 0 field p 3 "Skylake"
	if [[ -z `grep $SN non_upd_sn_list` ]];then
	 	if [[ "${BIOS_Ver}" != "UJGS1223-U02" ]];then
			if [[ ${kernel_pre_version} == 4 ]];then
				if [[ ${kernel_sub_version} == 1 ]];then
					./afulnx_64 /MAKEDRV
					echo E|./afulnx_64 UJGS1223.bin /p /b /n /x /me
				elif [[ ${kernel_sub_version} == 14 ]];then
					./afulnx_64_4.14 /MAKEDRV
					echo E|./afulnx_64_4.14 UJGS1223.bin /p /b /n /x /me
				fi
			elif [[ ${kernel_pre_version} == 5 ]];then
				if [[ ${kernel_sub_version} == 10 ]];then
                                	echo E|./afulnx_64_5.14.03.0039_2021 UJGS1223.bin /p /b /n /x /me
				fi
			fi
		else
			echo "BIOS Version is already ${BIOS_Ver}"
			exit 1
		fi
	else
		echo "$SN is one of thirty I620-G30 servers that only be updated to BIOS UJGS1035"
		if [[ "${BIOS_Ver}" != "UJGS1035" ]];then
			if [[ ${kernel_pre_version} == 4 ]];then
				if [[ ${kernel_sub_version} == 1 ]];then
                        		./afulnx_64 /MAKEDRV
                        		echo E|./afulnx_64 UJGS1035.bin /p /b /n /x /me
                		elif [[ ${kernel_sub_version} == 14 ]];then
                        		./afulnx_64_4.14 /MAKEDRV
                        		echo E|./afulnx_64_4.14 UJGS1035.bin /p /b /n /x /me
				fi
			elif [[ ${kernel_pre_version} == 5 ]];then
				if [[ ${kernel_sub_version} == 10 ]];then
                                	echo E|./afulnx_64_5.14.03.0039_2021 UJGS1035.bin /p /b /n /x /me
				fi
			fi
                fi
	fi
elif [[ "$CV" =~ "V6" ]];then
	if [[ "${BIOS_Ver}" != "UJGS1223" ]];then
		if [[ ${kernel_pre_version} == 4 ]];then
			if [[ ${kernel_sub_version} == 1 ]];then
        			./afulnx_64 /MAKEDRV
        			echo E|./afulnx_64 UJGS1223.bin /p /b /n /x /me
			elif [[ ${kernel_sub_version} == 14 ]];then
        			./afulnx_64_4.14 /MAKEDRV
        			echo E|./afulnx_64_4.14 UJGS1223.bin /p /b /n /x /me
			fi
		elif [[ ${kernel_pre_version} == 5 ]];then
			if [[ ${kernel_sub_version} == 10 ]];then
                        	echo E|./afulnx_64_5.14.03.0039_2021 UJGS1223.bin /p /b /n /x /me
			fi
		fi
	else 
		echo "BIOS Version is already ${BIOS_Ver}"
  		exit 1

        fi
fi
