#!/bin/bash
CPWD=`cd $(dirname  $0);pwd`
os_version=`uname -r |awk -F"el" '{print $2}' |awk -F"." '{print $1}'`
kernel_pre_version=`uname -r |awk -F"." '{print $1}'`
kernel_sub_version=`uname -r |awk -F"." '{print $2}'`
CV=`ipmitool fru print 0 |awk -F":" '/Chassis Part Number/{print $NF}' |sed 's/^\s*//g'`
BIOS_Ver=`dmidecode -t 0 |awk '/Version/{print $NF}' |sed 's/\s*//g'`
Pro_name=`ipmitool fru print 0 |awk -F":" '/Product Name/{print $NF}' |sed 's/^\s*//g'`

gcc_4_9_2_inst(){
yum install policycoreutils-python.x86_64 scl* -y
rpm -ivh \
         devtoolset-3-runtime-3.1-12.el6.x86_64.rpm \
         devtoolset-3-binutils-2.24-18.el6.x86_64.rpm \
         devtoolset-3-gcc-4.9.2-6.2.retpoline.el6.x86_64.rpm \
         devtoolset-3-libstdc++-devel-4.9.2-6.2.retpoline.el6.x86_64.rpm \
         devtoolset-3-gcc-c++-4.9.2-6.2.retpoline.el6.x86_64.rpm
}

I610_I620_G20(){
if [[ "${os_version}" = "6" ]] && [[ "${BIOS_Ver}" = "009" ]];then
        cd $CPWD
        gcc_4_9_2_inst
        scl enable devtoolset-3 "./SCELNX_64 /MAKEDRV && ./SCELNX_64 /i /s V009.cfg && sh upd_tag.sh"
elif [[ "${os_version}" = "7" ]] && [[ "${BIOS_Ver}" = "009" ]];then
	if [[ ${kernel_pre_version} == 4 ]];then
   		./SCELNX_64_4.14 /MAKEDRV && ./SCELNX_64_4.14 /i /s V009.cfg && sh upd_tag.sh
	elif [[ ${kernel_pre_version} == 5 ]];then
		if [[ ${kernel_sub_version} == 10 ]];then
                	./SCELNX_64_5.05.01.0002_2021 /i /s V009.cfg && sh upd_tag.sh
		fi
	fi
fi
}

I610_I620_G30(){
if [[ "$CV" =~ "V5" ]];then
	if [[ "${BIOS_Ver}" = "UJGS1035" ]];then
		if [[ ${kernel_pre_version} == 4 ]];then
			./SCELNX_64_4.14 /MAKEDRV && ./SCELNX_64_4.14 /i /s UJGS1035.cfg && sh upd_tag.sh
		elif [[ ${kernel_pre_version} == 5 ]];then
			if [[ ${kernel_sub_version} == 10 ]];then
				./SCELNX_64_5.05.01.0002_2021 /i /s UJGS1035.cfg && sh upd_tag.sh
			fi
		fi
	elif [[ "${BIOS_Ver}" = "UJGS1223-U02" ]];then
		if [[ ${kernel_pre_version} == 4 ]];then
                	./SCELNX_64_4.14 /MAKEDRV && ./SCELNX_64_4.14 /i /s UJGS1223-U02.cfg && sh upd_tag.sh
                elif [[ ${kernel_pre_version} == 5 ]];then
                      	if [[ ${kernel_sub_version} == 10 ]];then
                                ./SCELNX_64_5.05.01.0002_2021 /i /s UJGS1223-U02.cfg && sh upd_tag.sh
                        fi
		fi
	fi
elif [[ "$CV" =~ "V6" ]];then 
	if [[ "${BIOS_Ver}" = "UJGS1223-U02" ]]||[[ "${BIOS_Ver}" = "UJGS1223" ]];then
		if [[ ${kernel_pre_version} == 4 ]];then
                	./SCELNX_64_4.14 /MAKEDRV && ./SCELNX_64_4.14 /i /s UJGS1223-U02.cfg && sh upd_tag.sh
                elif [[ ${kernel_pre_version} == 5 ]];then
                        if [[ ${kernel_sub_version} == 10 ]];then
                                ./SCELNX_64_5.05.01.0002_2021 /i /s UJGS1223-U02.cfg && sh upd_tag.sh
                        fi
                fi
	fi
fi
}

S640_G30(){
if [[ "${BIOS_Ver}" = "UJGS4036" ]];then
	if [[ ${kernel_pre_version} == 4 ]];then
        	./SCELNX_64_4.14 /MAKEDRV && ./SCELNX_64_4.14 /i /s UJGS4036.cfg && sh upd_tag.sh
        elif [[ ${kernel_pre_version} == 5 ]];then
                if [[ ${kernel_sub_version} == 10 ]];then
                        ./SCELNX_64_5.05.01.0002_2021 /i /s UJGS4036.cfg && sh upd_tag.sh
                fi
        fi
fi
}

X780_G30(){
if [[ "${BIOS_Ver}" = "3.0c" ]];then
	echo "break"
fi
}

if [[ "${Pro_name}" == "I610-G20" ]] || [[ "${Pro_name}" == "I620-G20" ]];then
	I610_I620_G20
elif [[ "${Pro_name}" == "I610-G30" ]] || [[ "${Pro_name}" == "I620-G30" ]] || [[ "${Pro_name}" == "I420-G30" ]];then
	I610_I620_G30
elif [[ "${Pro_name}" == "S640-G30" ]];then
	S640_G30
elif [[ "${Pro_name}" == "X780-G30" ]];then
	X780_G30
elif [[ "${Pro_name}" == "W580-G20" ]];then
       ./sum -c ChangeBiosCfg --file W580_G20.cfg 
	exit $?
else 
	exit 1
fi
