#!/bin/bash

#get work path
path=$(dirname "$0")
path="${path/\./$(pwd)}"
pwd="${path}"

cd "${pwd}"
#init 
rm "${pwd}/result.xml" 2>/dev/null
rm "${pwd}/work.log" 2>/dev/null
tool_type=""
dev_num=0
flag_fail=0

#column for result.xml
result=""
errorCode=""
description=""

#create result.xml for esight
create_resultXml(){	
	echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>" >"${pwd}/result.xml"
	echo "<ConfigureResult>" >>"${pwd}/result.xml"
	echo "    <FwUpgrade>" >>"${pwd}/result.xml"
	echo "        <Result>${result}</Result>" >>"${pwd}/result.xml"
	echo "        <ErrorCode>${errorCode}</ErrorCode>" >>"${pwd}/result.xml"
	echo "        <Description>${description}</Description>" >>"${pwd}/result.xml"
	echo "    </FwUpgrade>" >>"${pwd}/result.xml"
	echo "</ConfigureResult>" >>"${pwd}/result.xml"
}

#read version xml
read_version_xml(){
	if [ ! -f "${pwd}/version.xml" ]
	then
		#version xml not found
		result="Fail"
		errorCode="5"
		description="Version xml not found"
		create_resultXml
		exit 5
	fi

	#get device info
	fw_file=$(cat "${pwd}/version.xml" |grep -i "<FileName>"|awk 'BEGIN{FS=">"}{print $2}'|awk 'BEGIN{FS="<"}{print $1}')
	cfg_fw_Version=$(cat "${pwd}/version.xml" |grep -i "<Version>"|awk 'BEGIN{FS=">"}{print $2}'|awk 'BEGIN{FS="<"}{print $1}')
	cfg_devices=$(cat "${pwd}/version.xml" |grep -i "<SupportModel>"|awk 'BEGIN{FS=">"}{print $2}'|awk 'BEGIN{FS="<"}{print $1}' |sed 's/;/ /g')
	upgrade_devices=$(cat "${pwd}/version.xml" |grep -i "<SupportModelUID>"|awk 'BEGIN{FS=">"}{print $2}'|awk 'BEGIN{FS="<"}{print $1}' |sed 's/;/ /g')

}

#install tools
prepare_tools(){
	local tool_flag=0
	
	cd "${pwd}/tools/"
	if [ -f "./sas3flash" ]
	then
		chmod +x "./sas3flash" 
		chmod +x "./sas3ircu"
	else 
		tool_flag=1
	fi
	
	cd "${pwd}/"
	if [ ${tool_flag} == 1 ]
	then
		#prepare tools failed
		result="Fail"
		errorCode="6"
		description="Prepare tools failed"
		create_resultXml
		exit 6;
	fi
}

#get device info by tools
get_device_info(){	
	get_device_3008	
	#no device found in version.xml
	if [ ${dev_num} == 0 ]
	then
		result="Fail"
		errorCode="4"
		description="No available device found"
		create_resultXml
		exit 4;
	fi
}

#get 3008 device
get_device_3008(){
	local num_3008=$("${pwd}/tools/sas3ircu" list |grep -i "3008" |wc -l)
	local fw_dev=0
	local num_index=$("${pwd}/tools/sas3ircu" list |grep -i "3008" |grep -v "IT"|wc -l)
	for ((i=0; i<num_3008; i++))
	do
		bus_num[$dev_num]=$("${pwd}/tools/sas3ircu" $i display |grep -i "Bus" |awk 'BEGIN{FS=" "}{print $3}')
		device_num[$dev_num]=$("${pwd}/tools/sas3ircu" $i display |grep -i "Device  " |awk 'BEGIN{FS=" "}{print $3}')
		function_num[$dev_num]=$("${pwd}/tools/sas3ircu" $i display |grep -i "Function" |awk 'BEGIN{FS=" "}{print $3}')
		cur_fw[$dev_num]=$("${pwd}/tools/sas3flash" -c $i -list |grep -i "Firmware Version" |awk 'BEGIN{FS=" "}{print $4}')
		fw_version=$(cat "${pwd}/version.xml" |grep -i "<Version>"|awk 'BEGIN{FS=">"}{print $2}'|awk 'BEGIN{FS="<"}{print $1}')
		device_index[$dev_num]=$i
		if [ "${cur_fw}" == "${fw_version}" ]
		then
			let fw_dev=$fw_dev+1
			continue
		fi					
		let dev_num=$dev_num+1
	done
	if [ ${fw_dev} -eq ${num_index} ] && [ ${num_index} != 0 ]
	then
		#already upgrade
		result="OK"
		errorCode="0"
		description="already upgrade"
		create_resultXml
		exit 0;
	fi
}

#upgrade firmware
upgrade_firmware(){
	
	#upgrade in loop
	for ((i=0;i<${dev_num};i++))
	do
		index=$i
		upgrade_By_tools	
	done
	
	if [ ${flag_fail} == 1 ]
	then
		#upgrade Failed
		result="Fail"
		errorCode="1"
		description="Upgrade failed"
		create_resultXml
		exit 1;
	else
		#upgrade Success
		result="OK"
		errorCode="NA"
		description="NA"
		create_resultXml
		exit 0;
	fi
}

#upgrade FW of 3008
upgrade_By_tools(){
	local fw_path=""
	if [ -d "${pwd}/${fw_file}" ]
	then
		fw_path="${fw_file}"
	fi

	local ir_fw=""
	local it_fw=""
	local ir_stdfw=""
	local it_stdfw=""
	local fw=""
	local rom=""
	local efidriver=""
	
	#get FW files by type
	for file_name in $(ls "${pwd}/${fw_path}")
	do 		
		echo "${file_name}" |egrep -i "P12|3008IR.bin" >/dev/null
		if [ $? == 0 ]
		then
			ir_fw="${file_name}"
		fi  

		echo "${file_name}" |grep -i "9311" >/dev/null
		if [ $? == 0 ]
		then
			ir_stdfw="${file_name}"
		fi 
	   
		echo "${file_name}" | grep -i "T12" >/dev/null
		if [ $? == 0 ]
		then
			it_fw="${file_name}"
		fi  

		echo "${file_name}" |grep -i "9300" >/dev/null
		if [ $? == 0 ]
		then
			it_stdfw="${file_name}"
		fi 
		
		echo "${file_name}" |grep -i "mptsas3" >/dev/null
		if [ $? == 0 ]
		then
			rom="${file_name}"
		fi 
		
		echo "${file_name}" |grep -i "mpt3x64" >/dev/null
		if [ $? == 0 ]
		then
			efidriver="${file_name}"
		fi 			
	done

	#choose FW file
	"${pwd}/tools/sas3flash" -c ${device_index[$index]} -list | grep -i "Firmware Product ID" | grep -i "IR" >/dev/null
	if [ $? == 0 ]
	then
		"${pwd}/tools/sas3flash" -c ${device_index[$index]} -list | grep -i "NVDATA Product ID" | grep -i "SAS3008" >/dev/null
		if [ $? == 0 ]
		then
			fw="${ir_fw}"
		else
			fw="${ir_stdfw}"
		fi
	fi
	
	"${pwd}/tools/sas3flash" -c ${device_index[$index]} -list | grep -i "Firmware Product ID" | grep -i "IT" >/dev/null
	if [ $? == 0 ]
	then
		"${pwd}/tools/sas3flash" -c ${device_index[$index]} -list | grep -i "NVDATA Product ID" | grep -i "SAS3008" >/dev/null
		if [ $? == 0 ]
		then
			fw="${it_fw}"
		else
			fw="${it_stdfw}"
		fi
	fi
	
	if [ "${fw}" == "" ]
	then
		return
	fi
	#text
	chmod +x "${pwd}/flashoem"
	"${pwd}/tools/sas3flash" -c ${device_index[$index]} -list | grep -i "SAS Address" | awk '{print $4}' > "tmp_SASADD.log"
		SAS1=`cat tmp_SASADD.log | awk -F- '{print $1}'`
		SAS2=`cat tmp_SASADD.log | awk -F- '{print $2}'`
		SAS3=`cat tmp_SASADD.log | awk -F- '{print $3}'`
		SAS4=`cat tmp_SASADD.log | awk -F- '{print $4}'`
		SASADD="${SAS1}${SAS2}${SAS3}${SAS4}"
		
		TRACE=`${pwd}/tools/sas3flash -c ${device_index[$index]} -list | grep -i "Tracer Number" | awk '{print $5}'`
	#clear
	"${pwd}/tools/sas3flash" -o -c ${device_index[$index]} -e 3  >>"${pwd}/work.log"
	#upgrade
	if [ -f "${pwd}/${fw_path}/${fw}" ]
	then	
		"${pwd}/tools/sas3flash" -o -c ${device_index[$index]} -f "${pwd}/${fw_path}/${fw}" >>"${pwd}/work.log"	
		if [ $? != 0 ]
		then
			"${pwd}/flashoem" -c ${device_index[$index]} -ctype sas3 -ftype ir2ir -f "${pwd}/${fw_path}/${fw}" >>"${pwd}/work.log"
			if [ $? != 0 ]
			then
				flag_fail=1
			fi
		fi	
	else
		flag_fail=1
	fi

	#upgrade rom if file exists
	if [ -f "${pwd}/${fw_path}/${rom}" ]
	then
		"${pwd}/tools/sas3flash" -o -c ${device_index[$index]} -b "${pwd}/${fw_path}/${rom}" >>"${pwd}/work.log"
		if [ $? != 0 ]
		then
			flag_fail=1
		fi
	else
		flag_fail=1
	fi
	
	#upgrade efidriver if file exists
	if [ -f "${pwd}/${fw_path}/${efidriver}" ]
	then
		"${pwd}/tools/sas3flash" -o -c ${device_index[$index]} -b "${pwd}/${fw_path}/${efidriver}" >>"${pwd}/work.log"
		if [ $? != 0 ]
		then
			flag_fail=1
		fi	
	else
		flag_fail=1
	fi	
	#write
	"${pwd}/tools/sas3flash" -o -c ${device_index[$index]} -sasadd $SASADD > "fwtmp.log"
	if [ $? != 0 ]
	then
		flag_fail=1
	fi
	"${pwd}/tools/sas3flash" -o -c ${device_index[$index]} -tracer $TRACE > "fwtmp.log"
	if [ $? != 0 ]
	then
		flag_fail=1
	else
		rm -f "tmp_SASADD.log" 2>/dev/null
		rm -f "fwtmp.log" 2>/dev/null
	fi
}

case $1 in
	upgrade)
		#read version xml
		read_version_xml
		
		#install tools
		prepare_tools
		
		#get device info by tools
		get_device_info

		#upgrade firmware
		upgrade_firmware
		;;
	*)
		;;
esac

exit 0
