#!/bin/sh

####################################
# ChangeLog:
#
# 2013/04/24	* Ziming Xu <Ziming.b.Xu@alcatel-lucent.com>
#		- PR431002: Add FocalTech touch panel firmware upgrade
#
###################################

ST_FWUP_SCR=/sbin/touchpanel_fw_update
FT_FWUP_SCR=/sbin/ft_tpfw_up

TP_FW_DIR=/firmware/touch_panel
ST_TPFW_DESC=${TP_FW_DIR}/touchpanel_firmware_desc
FT_TPFW_DESC=${TP_FW_DIR}/ft_tpfw_desc
# get touchpanel hardware
HW_TP=$(CLISettings get CURRENT FAB_HW_TOUCHPANEL 2>/dev/null)

if [[ -z "$HW_TP" ]]; then
	logger -s -t touchpanel -p emerg "Error: Cannot get touchpanel hardware from FAB, firmware upgrade quit."
	exit 1
fi

# lookup touchpanel in description file
DESC_FILE=$(grep -l $HW_TP $ST_TPFW_DESC $FT_TPFW_DESC 2>/dev/null)

if [[ -z "$DESC_FILE" ]]; then
	# touchpanel is not found in description file
	logger -s -t touchpanel -p emerg "Error: Can't found matched touchpanel hardware for $HW_TP, firmware upgrade quit."
	exit 1
fi

if [[ "$DESC_FILE" == "$ST_TPFW_DESC" ]]; then
	# found touchpanel in SinTec description file, current touchpanel is old, will invoke
	# old firmware upgrade script 
	TP_FWUP_SCR=$ST_FWUP_SCR
elif [[ "$DESC_FILE" == "$FT_TPFW_DESC" ]]; then
	# found touchpanel in FocalTec description file, current touchpanel is new, will invoke
	# new firmware upgrade script
	TP_FWUP_SCR=$FT_FWUP_SCR
else
	# touchpanel is found in both two description files, this should never happen
	logger -s -t touchpanel -p emerg "Error: Confused for touchpanel hardware $HW_TP, found matched hardware in \
		both \"$ST_TPFW_DESC\" and \"$FT_TPFW_DESC\"."
	exit 1
fi

# everything is OK, executing firmware upgrade script
$TP_FWUP_SCR $@

exit $?
