#!/bin/sh
# +--------------------------------------------------------------------------+
# | upgrade_script.sh                                                        |
# +--------------------------------------------------------------------------+
# | Charles Ingels, TESBV, June 2007, <charles.ingels@tesbv.com>             |
# +--------------------------------------------------------------------------+
# | This is the main script for upgrade some parts of the board:             |
# | - items into the DataFlash/NorFlash                                      |
# | - items into the filesystem                                              |
# +--------------------------------------------------------------------------+
#
# +--------------------------------------------------------------------------+
# | Configuration parameters                                                 |
# +--------------------------------------------------------------------------+
#
# 10.01.2012	Add pfpmu upgrade

SCRIPT_VERSION=2.1.10
DEVICE=/dev/sda1
LOGFILE=upgrade_script.log
LISTFILE=upgrade_list.txt
UPGRADE_SCRIPT=upgrade_script.sh
P_500_UPGRADE_PATTERN=p_500_upgrade_
P_800_UPGRADE_PATTERN=p_800_upgrade_
P_ALL_UPGRADE_PATTERN=p_500_upgrade_
UPGRADE_PATTERN_500=p_500_upgrade
UPGRADE_PATTERN_800=p_800_upgrade
UPGRADE_PATTERN=p_500_upgrade
TARGET_MAIN_DATAFLASH=main
TARGET_RESCUE_DATAFLASH=rescue
TARGET_GNSS=gnss
TARGET_FPGA=fpga
TARGET_PMU=pmu
TARGET_PF_PMU=pfpmu
TARGET_ULINK=ulink
TARGET_GSM=gsm
TARGET_CAN=can
DATAFLASHDEV=/dev/mtd3
RAMDISKDEV=/dev/mtd0
RAMDISK_NAME=ramdisk.gz
PMU_HEX_NAME=pmup.hex
TOOLSDIR=/usr/local/bin
PM4LOADER=${TOOLSDIR}/pm4loader
PM4LOADER_NAME=pm4loader
GNSSDETECTOR==${TOOLSDIR}/GNSS_Detector
GNSSDETECTOR_NAME=GNSS_Detector
FLASHTOOL=${TOOLSDIR}/dataflash_tool
CRC32TOOL=${TOOLSDIR}/crc32
PRINTTOOL=${TOOLSDIR}/oled_print
PMU_LOADER=${TOOLSDIR}/pmu_loader
PMU_LOADER_NAME=pmu_loader
PF_PMU_LOADER=${TOOLSDIR}/pfpmu_loader
PF_PMU_LOADER_NAME=pfpmu_loader
ULINK_LOADER=${TOOLSDIR}/ulink_loader
ULINK_LOADER_NAME=ulink_loader
GSM_LOADER=${TOOLSDIR}/gsmloader
GSM_LOADER_NAME=gsmloader
CAN_LOADER=${TOOLSDIR}/canloader
CAN_LOADER_NAME=canloader
UPGRADE=/mnt/datas
DEV_BLOCK1=/dev/mtdblock1
DEV_BLOCK2=/dev/mtdblock2
DEV_CONFIG=/mnt/config
EXTRA_DIR=extra

# +--------------------------------------------------------------------------+
# | Functions                                                                |
# +--------------------------------------------------------------------------+



# +--------------------------------------------------------------------------+
# |                                                                          |
# +--------------------------------------------------------------------------+

PATH=/usr/local/bin:${PATH}

TARTOOL=$(which tar) > /dev/null 2>&1
#
# Check if upgrade file present in /mnt/datas.
# if yes, MOUNTPOINT is datas else USB
#
mount | grep "${UPGRADE}" > /dev/null 2>&1
if [ ! $? -eq 0 ];
then
	#
	# Mount the device and test if the mount operation succeeds
	#
	echo "mount ${UPGRADE}"
	mount -t jffs2 ${DEV_BLOCK2} ${UPGRADE} > /dev/null 2>&1
	#
	# Check the mount point.
	#
	if [ ! $? -eq 0 ];
	then
		if [ -f ${PRINTTOOL} ];
		then
			${PRINTTOOL} --x 16 --y 30 --text "datas error" --clear
		fi
		exit 50
	fi	
fi

#
# Check if upgrade file present in /mnt/datas.
# if yes, MOUNTPOINT is datas else USB
#
if [ -f ${UPGRADE}/${P_500_UPGRADE_PATTERN}*.tar.bz2 ];
then
	echo "Upgrade file is present in ${UPGRADE}"
	MOUNTPOINT=${UPGRADE}
	P_ALL_UPGRADE_PATTERN=${P_500_UPGRADE_PATTERN}
	UPGRADE_PATTERN=${UPGRADE_PATTERN_500}
else
	if [ -f ${UPGRADE}/${P_800_UPGRADE_PATTERN}*.tar.bz2 ];
	then
		echo "Upgrade file is present in ${UPGRADE}"
		MOUNTPOINT=${UPGRADE}
		P_ALL_UPGRADE_PATTERN=${P_800_UPGRADE_PATTERN}
		UPGRADE_PATTERN=${UPGRADE_PATTERN_800}
	else
		MOUNTPOINT=/mnt/usbdisk
	fi
fi

#
# Start script
#
echo "Starting USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE} 2>&1

#
# Remove the file containing the list of files found on the device
#
if [ -f ${MOUNTPOINT}/${LISTFILE} ];
then
  rm -f ${MOUNTPOINT}/${LISTFILE} > /dev/null 2>&1
fi

#
# List the upgrade files found on the device
#
ls -l ${MOUNTPOINT}/${P_500_UPGRADE_PATTERN}*.tar.bz2 >> /dev/null 2>&1
if [ ! $? -eq 0 ];
then
	ls -l ${MOUNTPOINT}/${P_800_UPGRADE_PATTERN}*.tar.bz2 >> /dev/null 2>&1
	if [ ! $? -eq 0 ];
	then
		echo "No upgrade file found" >> ${MOUNTPOINT}/${LOGFILE}
		echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
		echo "Exit code is 4" >> ${MOUNTPOINT}/${LOGFILE}
		exit 50
	else
		UPGRADE_PATTERN=${UPGRADE_PATTERN_800}
		P_ALL_UPGRADE_PATTERN=${P_800_UPGRADE_PATTERN}
	fi
else
	UPGRADE_PATTERN=${UPGRADE_PATTERN_500}
	P_ALL_UPGRADE_PATTERN=${P_500_UPGRADE_PATTERN}
fi

#
# List all file which match upgrade pattern.
# eg:	p_500_upgrade-main-0.0.1.tar.bz2
#	p_500_upgrade-rescue-0.0.1.tar.bz2
#	p_500_upgrade-gnss-0.4.9.tar.bz2
#	p_500_upgrade-ulink-0.0.1.tar.bz2
#	p_500_upgrade-pmu-2.18.0.tar.bz2

#
ls ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2 > ${MOUNTPOINT}/${LISTFILE}
#
# Check for the number of .tar.bz2 files found on the USB key.
# If there are no files, then upgrade procedure is aborted.
#
if [ $(cat ${MOUNTPOINT}/${LISTFILE} | wc -l) -eq 0 ];
then
	echo "No upgrade file found" >> ${MOUNTPOINT}/${LOGFILE}
	echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
	echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
	exit 50
fi

# +==========================================================================+
# +--------------------------------------------------------------------------+
# | UPGRADING DATA FLASH MEMORY   -  MAIN                                    |
# +--------------------------------------------------------------------------+
# +==========================================================================+
grep "${UPGRADE_PATTERN}-${TARGET_MAIN_DATAFLASH}" ${MOUNTPOINT}/${LISTFILE} > /dev/null 2>&1
if [ $? -eq 0 ];
then
	#
	# Look for flash memory upgrade file (outside any filesystem).
	#
	TARGET=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_MAIN_DATAFLASH}*.tar.bz2 | tail -1 | awk -F- '{print $2}')
	VERSION=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_MAIN_DATAFLASH}*.tar.bz2 | tail -1 | awk -F- '{print $3}' | sed -e 's/\.tar.bz2$//g')
	
	if [ -z "${TARGET}" -o -z "${VERSION}" ]
	then
		echo "No valid upgrade file found for flash memory upgrade" >> ${MOUNTPOINT}/${LOGFILE}
	else
		echo "Valid upgrade file found. Processing..." >> ${MOUNTPOINT}/${LOGFILE} 
		echo "Target is '${TARGET}', version is '${VERSION}'" >> ${MOUNTPOINT}/${LOGFILE}
		echo "Processing file ${UPGRADE_PATTERN}-${TARGET_MAIN_DATAFLASH}-${VERSION}.tar.bz2" >> ${MOUNTPOINT}/${LOGFILE}
	
		TEMP_DIR=tmp_df_$$
		if [ -d ${MOUNTPOINT}/${TEMP_DIR} ];
		then
			rm -f ${MOUNTPOINT}/${TEMP_DIR}
		fi
		mkdir ${MOUNTPOINT}/${TEMP_DIR}
	
		echo "Uncompressing archive file '${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_MAIN_DATAFLASH}-${VERSION}.tar.bz2 '" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
		${TARTOOL} xjf ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_MAIN_DATAFLASH}-${VERSION}.tar.bz2 -C ${MOUNTPOINT}/${TEMP_DIR} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
		
		if [ ! $? -eq 0 ];
		then
			#
			# Remove extracted files
			#
			rm -rf ${MOUNTPOINT}/${TEMP_DIR}
			rm -f ${MOUNTPOINT}/${LISTFILE}
			rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
			#
            echo "Error to extract archive !!!" >> ${MOUNTPOINT}/${LOGFILE}
			echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
			echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
			exit 5
		fi
	
		# Check for the 'mapping.txt' file.
		if [ ! -f ${MOUNTPOINT}/${TEMP_DIR}/mapping.txt ];
		then
			echo "Mapping file not found !" >> ${MOUNTPOINT}/${LOGFILE}
		else
			LIST_FILES=${MOUNTPOINT}/${TEMP_DIR}/list_files_$$
			ls ${MOUNTPOINT}/${TEMP_DIR} > ${LIST_FILES} 
			for file in $(cat ${LIST_FILES});
			do
				if [ ! "${file}" = "mapping.txt" ];
				then
					# Write the file into the flash memory, depending on the mapping.
					ADDRESS=$(grep "${file}" ${MOUNTPOINT}/${TEMP_DIR}/mapping.txt | awk '{print $2}')
					if [ ! -z "${file}" -a ! -z "${ADDRESS}" ];
					then
						echo "Processing file ${file}..."
						echo "File: ${file}, Address: ${ADDRESS}" >> ${MOUNTPOINT}/${LOGFILE}
						echo "Programming file '${file}' at address ${ADDRESS}" >> ${MOUNTPOINT}/${LOGFILE}
						if [ -f ${PRINTTOOL} ];
						then
							${PRINTTOOL} --x 40 --y 20 --text "Writing" --clear
							${PRINTTOOL} --x 5 --y 42 --text "${file}"
						fi
						echo "${FLASHTOOL} -d ${DATAFLASHDEV} -a ${ADDRESS} -i ${MOUNTPOINT}/${TEMP_DIR}/${file}" >> ${MOUNTPOINT}/${LOGFILE}
						${FLASHTOOL} --progress -d ${DATAFLASHDEV} -a ${ADDRESS} -i ${MOUNTPOINT}/${TEMP_DIR}/${file} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
					fi
				fi
				#
				# Special operation must be done for some files. A CRC32 must
				# be computed and written at the beginning of the environments area.
				#
				CHECK_CRC=$(grep "${file}" ${MOUNTPOINT}/${TEMP_DIR}/mapping.txt | awk '{print $4}')
				if [ "${CHECK_CRC}" = "crc" ];
				then
					# Compute the CRC32...
					MY_CRC32=$(${CRC32TOOL} --offset=4 --infile=${MOUNTPOINT}/${TEMP_DIR}/${file})
					if [ -z "${MY_CRC32}" ];
					then
						echo "CRC32 calculation error !" >> ${MOUNTPOINT}/${LOGFILE}
						echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
						echo "Exit code is 6" >> ${MOUNTPOINT}/${LOGFILE}
						rm -rf ${MOUNTPOINT}/${TEMP_DIR}
						rm -f ${MOUNTPOINT}/${LISTFILE}
						rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
		
						if [ -f ${PRINTTOOL} ];
						then
							${PRINTTOOL} --x 30 --y 32 --text "CRC error" --clear
						fi				
						exit 6
					fi
					# Write the CRC32 at the beginning of the environment area.
					CRC32ADDRESS=$(grep ${file} ${MOUNTPOINT}/${TEMP_DIR}/mapping.txt | awk '{print $2}')
					echo "${FLASHTOOL} -d ${DATAFLASHDEV} -a ${CRC32ADDRESS} --data=${MY_CRC32}" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
					${FLASHTOOL} -d ${DATAFLASHDEV} -a ${CRC32ADDRESS} --data=${MY_CRC32} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				fi
			done

		fi

		
		# Check for the extra files
		if [ ! -d ${MOUNTPOINT}/${TEMP_DIR}/${EXTRA_DIR} ];
		then
			echo "Extra directory not found !" >> ${MOUNTPOINT}/${LOGFILE}
		else
			if [ -f ${PRINTTOOL} ];
			then
				${PRINTTOOL} --x 40 --y 20 --text "Writing" --clear
				${PRINTTOOL} --x 20 --y 42 --text "Extra Datas"
			fi

			mount | grep "${DEV_CONFIG}" > /dev/null 2>&1
			if [ $? -eq 1 ];
			then
				mount -t jffs2 ${DEV_BLOCK1} ${DEV_CONFIG}
			fi

			rm -R -f ${DEV_CONFIG}/WebLanguages
			rm -R -f ${DEV_CONFIG}/www/*
			rm -R -f ${DEV_CONFIG}/ntripcaster/bin/*
			rm -R -f ${DEV_CONFIG}/6*
			rm -R -f ${DEV_CONFIG}/7*
			rm -R -f ${DEV_CONFIG}/rinex*
			chmod 777 ${MOUNTPOINT}/${TEMP_DIR}/${EXTRA_DIR}/*
			cp -R -f ${MOUNTPOINT}/${TEMP_DIR}/${EXTRA_DIR}/* ${DEV_CONFIG}
		fi
		
		
		rm -rf ${MOUNTPOINT}/${TEMP_DIR}/${EXTRA_DIR}
		rm -rf ${MOUNTPOINT}/${TEMP_DIR}
		rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_MAIN_DATAFLASH}-${VERSION}.tar.bz2 
		${FLASHTOOL} --add=0x3927D --size=1 --data=FF
	fi
fi

# +==========================================================================+
# +--------------------------------------------------------------------------+
# | UPGRADING DATA FLASH MEMORY   -  RESCUE                                  |
# +--------------------------------------------------------------------------+
# +==========================================================================+
grep "${UPGRADE_PATTERN}-${TARGET_RESCUE_DATAFLASH}" ${MOUNTPOINT}/${LISTFILE} > /dev/null 2>&1
if [ $? -eq 0 ];
then
	#
	# Look for flash memory upgrade file (outside any filesystem).
	#
	TARGET=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_RESCUE_DATAFLASH}*.tar.bz2 | tail -1 | awk -F- '{print $2}')
	VERSION=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_RESCUE_DATAFLASH}*.tar.bz2 | tail -1 | awk -F- '{print $3}' | sed -e 's/\.tar.bz2$//g')
	
	if [ -z "${TARGET}" -o -z "${VERSION}" ]
	then
		echo "No valid upgrade file found for flash memory upgrade" >> ${MOUNTPOINT}/${LOGFILE}
	else
		echo "Valid upgrade file found. Processing..." >> ${MOUNTPOINT}/${LOGFILE} 
		echo "Target is '${TARGET}', version is '${VERSION}'" >> ${MOUNTPOINT}/${LOGFILE}
		echo "Processing file ${UPGRADE_PATTERN}-${TARGET_RESCUE_DATAFLASH}-${VERSION}.tar.bz2" >> ${MOUNTPOINT}/${LOGFILE}
	
		TEMP_DIR=tmp_df_$$
		if [ -d ${MOUNTPOINT}/${TEMP_DIR} ];
		then
			rm -f ${MOUNTPOINT}/${TEMP_DIR}
		fi
		mkdir ${MOUNTPOINT}/${TEMP_DIR}
	
		echo "Uncompressing archive file '${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_RESCUE_DATAFLASH}-${VERSION}.tar.bz2 '" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
		${TARTOOL} xjf ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_RESCUE_DATAFLASH}-${VERSION}.tar.bz2 -C ${MOUNTPOINT}/${TEMP_DIR} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
		
		if [ ! $? -eq 0 ];
		then
			echo "Error to extract archive !!!" >> ${MOUNTPOINT}/${LOGFILE}
			#
			# Delete extracted files.
			#
			rm -rf ${MOUNTPOINT}/${TEMP_DIR}
			rm -f ${MOUNTPOINT}/${LISTFILE}
			rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
			#
			echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
			echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
			exit 5
		fi
	

		# Check for the 'mapping.txt' file.
		if [ ! -f ${MOUNTPOINT}/${TEMP_DIR}/mapping.txt ];
		then
			echo "Mapping file not found !" >> ${MOUNTPOINT}/${LOGFILE}
		else
			LIST_FILES=${MOUNTPOINT}/${TEMP_DIR}/list_files_$$
			ls ${MOUNTPOINT}/${TEMP_DIR} > ${LIST_FILES} 
			for file in $(cat ${LIST_FILES});
			do
				if [ ! "${file}" = "mapping.txt" ];
				then
					# Write the file into the flash memory, depending on the mapping.
					ADDRESS=$(grep "${file}" ${MOUNTPOINT}/${TEMP_DIR}/mapping.txt | awk '{print $2}')
					if [ ! -z "${file}" -a ! -z "${ADDRESS}" ];
					then
						echo "Processing file ${file}..."
						echo "File: ${file}, Address: ${ADDRESS}" >> ${MOUNTPOINT}/${LOGFILE}
						echo "Programming file '${file}' at address ${ADDRESS}" >> ${MOUNTPOINT}/${LOGFILE}
						if [ -f ${PRINTTOOL} ];
						then
							${PRINTTOOL} --x 40 --y 20 --text "Writing" --clear
							${PRINTTOOL} --x 5 --y 42 --text "${file}"
						fi
						echo "${FLASHTOOL} -d ${DATAFLASHDEV} -a ${ADDRESS} -i ${MOUNTPOINT}/${TEMP_DIR}/${file}" >> ${MOUNTPOINT}/${LOGFILE}
						${FLASHTOOL} --progress -d ${DATAFLASHDEV} -a ${ADDRESS} -i ${MOUNTPOINT}/${TEMP_DIR}/${file} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
					fi
				fi
				#
				# Special operation must be done for some files. A CRC32 must
				# be computed and written at the beginning of the environments area.
				#
				CHECK_CRC=$(grep "${file}" ${MOUNTPOINT}/${TEMP_DIR}/mapping.txt | awk '{print $4}')
				if [ "${CHECK_CRC}" = "crc" ];
				then
					# Compute the CRC32...
					MY_CRC32=$(${CRC32TOOL} --offset=4 --infile=${MOUNTPOINT}/${TEMP_DIR}/${file})
					if [ -z "${MY_CRC32}" ];
					then
						echo "CRC32 calculation error !" >> ${MOUNTPOINT}/${LOGFILE}
						echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
						echo "Exit code is 6" >> ${MOUNTPOINT}/${LOGFILE}
						rm -rf ${MOUNTPOINT}/${TEMP_DIR}
						rm -f ${MOUNTPOINT}/${LISTFILE}
						rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
						
						if [ -f ${PRINTTOOL} ];
						then
							${PRINTTOOL} --x 30 --y 32 --text "CRC error" --clear
						fi
						exit 6
					fi
					# Write the CRC32 at the beginning of the environment area.
					CRC32ADDRESS=$(grep ${file} ${MOUNTPOINT}/${TEMP_DIR}/mapping.txt | awk '{print $2}')
					echo "${FLASHTOOL} -d ${DATAFLASHDEV} -a ${CRC32ADDRESS} --data=${MY_CRC32}" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
					${FLASHTOOL} -d ${DATAFLASHDEV} -a ${CRC32ADDRESS} --data=${MY_CRC32} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				fi
			done

		fi
		
		rm -rf ${MOUNTPOINT}/${TEMP_DIR}
		rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_RESCUE_DATAFLASH}-${VERSION}.tar.bz2 
		${FLASHTOOL} --add=0x3927D --size=1 --data=FF
	fi
fi


# +==========================================================================+
# +--------------------------------------------------------------------------+
# | UPGRADING GNSS BOARD                                                     |
# +--------------------------------------------------------------------------+
# +==========================================================================+
grep "${UPGRADE_PATTERN}-${TARGET_GNSS}" ${MOUNTPOINT}/${LISTFILE} > /dev/null 2>&1
if [ $? -eq 0 ];
then
	TARGET=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_GNSS}*.tar.bz2 | tail -1 | awk -F- '{print $2}')
	VERSION=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_GNSS}*.tar.bz2 | tail -1 | awk -F- '{print $3}' | sed -e 's/\.tar.bz2$//g')
	if [ -z "${TARGET}" -o -z "${VERSION}" ]
	then
		echo "No valid upgrade file found for GNSS board upgrade" >> ${MOUNTPOINT}/${LOGFILE}
	else
		#
		# Creation of a temporary directory.
		#
		TEMP_DIR=tmp_gnss_$$
		EXTRACT=/usr/local/bin
		if [ -d ${EXTRACT}/${TEMP_DIR} ];
		then
			rm -f ${EXTRACT}/${TEMP_DIR}
		fi
		mkdir ${EXTRACT}/${TEMP_DIR}
		#
		# Uncompressing archive file p_500_upgrade-gnss-x.y.z.tar.bz2
		#
		echo "Uncompressing archive file '${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_GNSS}-${VERSION}.tar.bz2 '" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
		${TARTOOL} xjf ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_GNSS}-${VERSION}.tar.bz2 -C ${EXTRACT}/${TEMP_DIR} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
		
		if [ ! $? -eq 0 ];
		then
			echo "Error to extract archive !!!" >> ${MOUNTPOINT}/${LOGFILE}
			#
			# Remove extracted files.
			#
			rm -rf ${EXTRACT}/${TEMP_DIR}
			rm -f ${MOUNTPOINT}/${LISTFILE}
			rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
			#
			echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
			echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
			exit 5
		fi
		
		#
		# Detect GNSS Board
		#
		if [ -f ${EXTRACT}/${TEMP_DIR}/${GNSSDETECTOR_NAME} ];
		then
			echo "Find tool 'GNSS Detector on /usr/local/bin'" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
			chmod 777 ${EXTRACT}/${TEMP_DIR}/${GNSSDETECTOR_NAME}
			GNSSDETECTOR=${EXTRACT}/${TEMP_DIR}/${GNSSDETECTOR_NAME}
		
			/usr/local/bin/oled_print --x 35 --y 20 --text "Detecting" --clear
			/usr/local/bin/oled_print --x 45 --y 40 --text "GNSS"
			#
			# Checking PM500 or PF500
			#
			if [ -f /etc/PM_PF_Detector ];
			then
				/etc/PM_PF_Detector
				PRODUCT=$?
			else
				PRODUCT=0
			fi
			
			if [ ! ${PRODUCT} == 0 ];
			then
				cd ${EXTRACT}/${TEMP_DIR} && ${GNSSDETECTOR} -p pf -i ${EXTRACT}/${TEMP_DIR} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
			else
				cd ${EXTRACT}/${TEMP_DIR} && ${GNSSDETECTOR} -p pm -i ${EXTRACT}/${TEMP_DIR} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
			fi
			
		else
			echo "Unable to find tool 'GNSS Detector'. Exiting" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
			#
			# Remove extracted files.
			#
			rm -rf ${EXTRACT}/${TEMP_DIR}
			rm -f ${MOUNTPOINT}/${LISTFILE}
			rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
			#
			echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
			echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
			exit 5
		fi

		#
		# GNSS upgrade, only if filesystem upgrade.
		#
		GNSS_UPGRADE_FILE=${EXTRACT}/${TEMP_DIR}/gnss_fw.bin
		CGNSS_NEW_TOOL_DIR=${TOOLSDIR}
		if [ -f ${GNSS_UPGRADE_FILE} ];
		then
			if [ -f ${EXTRACT}/${TEMP_DIR}/${PM4LOADER_NAME} ];
			then
			echo "Find tool 'pm4loader on /usr/local/bin'" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
			chmod 777 ${EXTRACT}/${TEMP_DIR}/${PM4LOADER_NAME}
			PM4LOADER=${EXTRACT}/${TEMP_DIR}/${PM4LOADER_NAME}
			CGNSS_NEW_TOOL_DIR=${EXTRACT}/${TEMP_DIR}
			fi

			if [ -f ${PM4LOADER} ];
			then
				/usr/local/bin/oled_print --x 35 --y 20 --text "Upgrading" --clear
				/usr/local/bin/oled_print --x 45 --y 40 --text "GNSS"
				cd ${CGNSS_NEW_TOOL_DIR} && ${PM4LOADER} -v >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				echo "Upgrading GNSS" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				cd ${CGNSS_NEW_TOOL_DIR} && ${PM4LOADER} -f ${GNSS_UPGRADE_FILE} -p /dev/ttyS2 >> ${EXTRACT}/${TEMP_DIR}/gnss.log 2>&1
				if [ ! $? -eq 0 ];
				then
					echo "Error to upgrade GNSS !!!" >> ${MOUNTPOINT}/${LOGFILE}
					#
					# Remove extracted files.
					#
					cat ${EXTRACT}/${TEMP_DIR}/gnss.log >> ${MOUNTPOINT}/${LOGFILE}

					rm -rf ${EXTRACT}/${TEMP_DIR}
					rm -f ${MOUNTPOINT}/${LISTFILE}
					rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
					#
					echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
					echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
					exit 5
				fi
				${FLASHTOOL} -d ${DATAFLASHDEV} -a 0x3927E --data=FF
			else
				echo "Unable to find tool 'pm4loader'. Exiting" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				#
				# Remove extracted files.
				#
				rm -rf ${EXTRACT}/${TEMP_DIR}
				rm -f ${MOUNTPOINT}/${LISTFILE}
				rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
				#
				echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
				echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
				exit 5
			fi
		fi
		
		#
		# Head GNSS upgrade, only if filesystem upgrade.
		#
		GNSS_UPGRADE_FILE=${EXTRACT}/${TEMP_DIR}/head_gnss_fw.bin
		CGNSS_NEW_TOOL_DIR=${TOOLSDIR}
		if [ -f ${GNSS_UPGRADE_FILE} ];
		then
			if [ -f ${EXTRACT}/${TEMP_DIR}/${PM4LOADER_NAME} ];
			then
			echo "Find tool 'pm4loader on /usr/local/bin'" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
			chmod 777 ${EXTRACT}/${TEMP_DIR}/${PM4LOADER_NAME}
			PM4LOADER=${EXTRACT}/${TEMP_DIR}/${PM4LOADER_NAME}
			CGNSS_NEW_TOOL_DIR=${EXTRACT}/${TEMP_DIR}
			fi

			if [ -f ${PM4LOADER} ];
			then
				/usr/local/bin/oled_print --x 35 --y 20 --text "Upgrading" --clear
				/usr/local/bin/oled_print --x 35 --y 40 --text "Head GNSS"
				cd ${CGNSS_NEW_TOOL_DIR} && ${PM4LOADER} -v >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				echo "Upgrading GNSS Heading" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				cd ${CGNSS_NEW_TOOL_DIR} && ${PM4LOADER} -f ${GNSS_UPGRADE_FILE} -p /dev/ttyUSB3 >> ${EXTRACT}/${TEMP_DIR}/gnss.log 2>&1
				if [ ! $? -eq 0 ];
				then
					echo "Error to upgrade Head GNSS !!!" >> ${MOUNTPOINT}/${LOGFILE}
					#
					# Remove extracted files.
					#
					cat ${EXTRACT}/${TEMP_DIR}/gnss.log >> ${MOUNTPOINT}/${LOGFILE}

					rm -rf ${EXTRACT}/${TEMP_DIR}
					rm -f ${MOUNTPOINT}/${LISTFILE}
					rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
					#
					echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
					echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
					exit 5
				fi
				${FLASHTOOL} -d ${DATAFLASHDEV} -a 0x3927E --data=FF
			else
				echo "Unable to find tool 'pm4loader'. Exiting" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				#
				# Remove extracted files.
				#
				rm -rf ${EXTRACT}/${TEMP_DIR}
				rm -f ${MOUNTPOINT}/${LISTFILE}
				rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
				#
				echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
				echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
				exit 5
			fi
		fi
		#
		# Removing temporary directory.
		#
		rm -rf ${EXTRACT}/${TEMP_DIR}
		rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_GNSS}-${VERSION}.tar.bz2 
	fi
fi

# +==========================================================================+
# +--------------------------------------------------------------------------+
# | UPGRADING FPGA                                                           |
# +--------------------------------------------------------------------------+
# +==========================================================================+
grep "${UPGRADE_PATTERN}-${TARGET_FPGA}" ${MOUNTPOINT}/${LISTFILE} > /dev/null 2>&1
if [ $? -eq 0 ];
then
	TARGET=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_FPGA}*.tar.bz2 | tail -1 | awk -F- '{print $2}')
	VERSION=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_FPGA}*.tar.bz2 | tail -1 | awk -F- '{print $3}' | sed -e 's/\.tar.bz2$//g')
	if [ -z "${TARGET}" -o -z "${VERSION}" ]
	then
		echo "No valid upgrade file found for GNSS board FPGA upgrade" >> ${MOUNTPOINT}/${LOGFILE}
	else
		#
		# Creation of a temporary directory.
		#
		TEMP_DIR=tmp_gnss_$$
		EXTRACT=/usr/local/bin
		if [ -d ${EXTRACT}/${TEMP_DIR} ];
		then
			rm -f ${EXTRACT}/${TEMP_DIR}
		fi
		mkdir ${EXTRACT}/${TEMP_DIR}
		#
		# Uncompressing archive file p_500_upgrade-fpga-x.y.z.tar.bz2
		#
		echo "Uncompressing archive file '${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_FPGA}-${VERSION}.tar.bz2 '" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
		${TARTOOL} xjf ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_FPGA}-${VERSION}.tar.bz2 -C ${EXTRACT}/${TEMP_DIR} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
		
		if [ ! $? -eq 0 ];
		then
			echo "Error to extract archive !!!" >> ${MOUNTPOINT}/${LOGFILE}
			#
			# Remove extracted files.
			#
			rm -rf ${EXTRACT}/${TEMP_DIR}
			rm -f ${MOUNTPOINT}/${LISTFILE}
			rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
			#
			echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
			echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
			exit 5
		fi
		
		#
		# Detect GNSS Board
		#
		if [ -f ${EXTRACT}/${TEMP_DIR}/${GNSSDETECTOR_NAME} ];
		then
			echo "Find tool 'GNSS Detector on /usr/local/bin'" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
			chmod 777 ${EXTRACT}/${TEMP_DIR}/${GNSSDETECTOR_NAME}
			GNSSDETECTOR=${EXTRACT}/${TEMP_DIR}/${GNSSDETECTOR_NAME}
		
			/usr/local/bin/oled_print --x 35 --y 20 --text "Detecting" --clear
			/usr/local/bin/oled_print --x 45 --y 40 --text "GNSS"
			#
			# Checking PM500 or PF500
			#
			if [ -f /etc/PM_PF_Detector ];
			then
				/etc/PM_PF_Detector
				PRODUCT=$?
			else
				PRODUCT=0
			fi
			
			if [ ! ${PRODUCT} == 0 ];
			then
				cd ${EXTRACT}/${TEMP_DIR} && ${GNSSDETECTOR} -p pf -i ${EXTRACT}/${TEMP_DIR} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
			else
				cd ${EXTRACT}/${TEMP_DIR} && ${GNSSDETECTOR} -p pm -i ${EXTRACT}/${TEMP_DIR} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
			fi
			
		else
			echo "Unable to find tool 'GNSS Detector'. Exiting" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
			#
			# Remove extracted files.
			#
			rm -rf ${EXTRACT}/${TEMP_DIR}
			rm -f ${MOUNTPOINT}/${LISTFILE}
			rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
			#
			echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
			echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
			exit 5
		fi

		#
		# GNSS FPGA upgrade, only if filesystem upgrade.
		#
		GNSS_FPGA_UPGRADE_FILE=${EXTRACT}/${TEMP_DIR}/fpga.bin
		GNSS_FPGA_UPGRADE_SFL=${EXTRACT}/${TEMP_DIR}/sfld_fpg.bin
		CGNSS_NEW_TOOL_DIR=${TOOLSDIR}
		if [ -f ${GNSS_FPGA_UPGRADE_FILE} ];
		then
			if [ -f ${EXTRACT}/${TEMP_DIR}/${PM4LOADER_NAME} ];
			then
			echo "Find tool 'pm4loader on /usr/local/bin'" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
			chmod 777 ${EXTRACT}/${TEMP_DIR}/${PM4LOADER_NAME}
			PM4LOADER=${EXTRACT}/${TEMP_DIR}/${PM4LOADER_NAME}
			CGNSS_NEW_TOOL_DIR=${EXTRACT}/${TEMP_DIR}
			fi

			if [ -f ${PM4LOADER} ];
			then
				/usr/local/bin/oled_print --x 35 --y 20 --text "Upgrading" --clear
				/usr/local/bin/oled_print --x 35 --y 40 --text "GNSS FPGA"
				cd ${CGNSS_NEW_TOOL_DIR} && ${PM4LOADER} -v >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				echo "Upgrading GNSS FPGA" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				cd ${CGNSS_NEW_TOOL_DIR} && ${PM4LOADER} -f ${GNSS_FPGA_UPGRADE_FILE} -s ${GNSS_FPGA_UPGRADE_SFL} -p /dev/ttyS2 >> ${EXTRACT}/${TEMP_DIR}/gnss.log 2>&1
				if [ ! $? -eq 0 ];
				then
					echo "Error to upgrade GNSS FPGA!!!" >> ${MOUNTPOINT}/${LOGFILE}
					#
					# Remove extracted files.
					#
					cat ${EXTRACT}/${TEMP_DIR}/gnss.log >> ${MOUNTPOINT}/${LOGFILE}

					rm -rf ${EXTRACT}/${TEMP_DIR}
					rm -f ${MOUNTPOINT}/${LISTFILE}
					rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
					#
					echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
					echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
					exit 5
				fi
			else
				echo "Unable to find tool 'pm4loader'. Exiting" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				#
				# Remove extracted files.
				#
				rm -rf ${EXTRACT}/${TEMP_DIR}
				rm -f ${MOUNTPOINT}/${LISTFILE}
				rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
				#
				echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
				echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
				exit 5
			fi
		else
			echo "No FPGA to upgrade on GNSS detected" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
		fi
		
		GNSS_HEAD_FPGA_UPGRADE_FILE=${EXTRACT}/${TEMP_DIR}/head_fpga.bin
		if [ -f ${GNSS_HEAD_FPGA_UPGRADE_FILE} ];
		then
			if [ -f ${EXTRACT}/${TEMP_DIR}/${PM4LOADER_NAME} ];
			then
			echo "Find tool 'pm4loader on /usr/local/bin'" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
			chmod 777 ${EXTRACT}/${TEMP_DIR}/${PM4LOADER_NAME}
			PM4LOADER=${EXTRACT}/${TEMP_DIR}/${PM4LOADER_NAME}
			CGNSS_NEW_TOOL_DIR=${EXTRACT}/${TEMP_DIR}
			fi

			if [ -f ${PM4LOADER} ];
			then
				/usr/local/bin/oled_print --x 35 --y 20 --text "Upgrading" --clear
				/usr/local/bin/oled_print --x 10 --y 40 --text "Head GNSS FPGA"
				echo "Upgrading GNSS Heading FPGA" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				cd ${CGNSS_NEW_TOOL_DIR} && ${PM4LOADER} -f ${GNSS_HEAD_FPGA_UPGRADE_FILE} -s ${GNSS_FPGA_UPGRADE_SFL} -p /dev/ttyUSB3 >> ${EXTRACT}/${TEMP_DIR}/gnss.log 2>&1
				if [ ! $? -eq 0 ];
				then
					echo "Error to upgrade Head GNSS FPGA !!!" >> ${MOUNTPOINT}/${LOGFILE}
					#
					# Remove extracted files.
					#
					cat ${EXTRACT}/${TEMP_DIR}/gnss.log >> ${MOUNTPOINT}/${LOGFILE}

					rm -rf ${EXTRACT}/${TEMP_DIR}
					rm -f ${MOUNTPOINT}/${LISTFILE}
					rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
					#
					echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
					echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
					exit 5
				fi
			else
				echo "Unable to find tool 'pm4loader'. Exiting" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				#
				# Remove extracted files.
				#
				rm -rf ${EXTRACT}/${TEMP_DIR}
				rm -f ${MOUNTPOINT}/${LISTFILE}
				rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
				#
				echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
				echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
				exit 5
			fi
		else
			echo "No FPGA to upgrade on Heading GNSS detected" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
		fi
		${FLASHTOOL} -d ${DATAFLASHDEV} -a 0x3927E --data=FF
		#
		# Removing temporary directory.
		#
		rm -rf ${EXTRACT}/${TEMP_DIR}
		rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_FPGA}-${VERSION}.tar.bz2 
	fi
fi

# +==========================================================================+
# +--------------------------------------------------------------------------+
# | UPGRADING ULINK RADIO                                                     |
# +--------------------------------------------------------------------------+
# +==========================================================================+
grep "${UPGRADE_PATTERN}-${TARGET_ULINK}" ${MOUNTPOINT}/${LISTFILE} > /dev/null 2>&1
if [ $? -eq 0 ];
then
	TARGET=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_ULINK}*.tar.bz2 | tail -1 | awk -F- '{print $2}')
	PORT=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_ULINK}*.tar.bz2 | tail -1 | awk -F- '{print $3}')
	RS=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_ULINK}*.tar.bz2 | tail -1 | awk -F- '{print $4}')
	VERSION=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_ULINK}*.tar.bz2 | tail -1 | awk -F- '{print $5}' | sed -e 's/\.tar.bz2$//g')

	echo "Updated ULINK on port " ${PORT} " with RS" ${RS} >>  ${MOUNTPOINT}/${LOGFILE}

	if [ -z "${TARGET}" -o -z "${PORT}" -o -z "${RS}" -o -z "${VERSION}" ]
	then
		echo "No valid upgrade file found for ULINK radio upgrade" >> ${MOUNTPOINT}/${LOGFILE}
		echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
		echo "Exit code is 1" >> ${MOUNTPOINT}/${LOGFILE}
		#
		# Remove temporary resource
		#
		rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
		rm -f ${MOUNTPOINT}/${LISTFILE}
		exit 1
	elif [ "${PORT}" != "A" -a "${PORT}" != "B" -a "${PORT}" != "D" -a "${PORT}" != "F" ]
	then
		echo "No valid upgrade port found for ULINK radio upgrade" >> ${MOUNTPOINT}/${LOGFILE}
		echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
		echo "Exit code is 1" >> ${MOUNTPOINT}/${LOGFILE}
		#
		# Remove temporary resource
		#
		rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
		rm -f ${MOUNTPOINT}/${LISTFILE}
		exit 1
	elif [ "${RS}" != "232" -a "${RS}" != "422" ]
	then
		echo "No valid upgrade RS found for ULINK radio upgrade" >> ${MOUNTPOINT}/${LOGFILE}
		echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
		echo "Exit code is 1" >> ${MOUNTPOINT}/${LOGFILE}
		#
		# Remove temporary resource
		#
		rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
		rm -f ${MOUNTPOINT}/${LISTFILE}
		exit 1
	else
		#
		# Creation of a temporary directory.
		#
		TEMP_DIR=tmp_ulink_$$
		if [ -d ${MOUNTPOINT}/${TEMP_DIR} ];
		then
			rm -f ${MOUNTPOINT}/${TEMP_DIR}
		fi
		mkdir ${MOUNTPOINT}/${TEMP_DIR}
		#
		# Uncompressing archive file pm4_upgrade-ulink-A-422-x.y.z.tar.bz2
		#
		echo "Uncompressing archive file '${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_ULINK}-${PORT}-${RS}-${VERSION}.tar.bz2 '" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
		${TARTOOL} xjf ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_ULINK}-${PORT}-${RS}-${VERSION}.tar.bz2 -C ${MOUNTPOINT}/${TEMP_DIR} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
		
		if [ ! $? -eq 0 ];
		then
			echo "Error to extract archive !!!" >> ${MOUNTPOINT}/${LOGFILE}
			#
			# Remove extracted files.
			#
			rm -rf ${MOUNTPOINT}/${TEMP_DIR}
			rm -f ${MOUNTPOINT}/${LISTFILE}
			rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
			#
			echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
			echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
			exit 5
		fi

		#
		# ULINK upgrade, only if filesystem upgrade.
		#
		ULINK_UPGRADE_FILE=${MOUNTPOINT}/${TEMP_DIR}/ulink_fw.hex
		ULINK_NEW_TOOL_DIR=${TOOLSDIR}
		if [ -f ${ULINK_UPGRADE_FILE} ];
		then
			if [ -f ${MOUNTPOINT}/${TEMP_DIR}/${ULINK_LOADER_NAME} ];
			then
			echo "Find tool 'ulink loader on USB'" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
			chmod 777 ${MOUNTPOINT}/${TEMP_DIR}/${ULINK_LOADER_NAME}
			ULINK_LOADER=${MOUNTPOINT}/${TEMP_DIR}/${ULINK_LOADER_NAME}
			ULINK_NEW_TOOL_DIR=${MOUNTPOINT}/${TEMP_DIR}
			fi
			
			if [ -f ${ULINK_LOADER} ];
			then
				/usr/local/bin/oled_print --x 35 --y 20 --text "Upgrading" --clear
				/usr/local/bin/oled_print --x 45 --y 40 --text "ULINK"
				cd ${ULINK_NEW_TOOL_DIR} && ${ULINK_LOADER} ${ULINK_UPGRADE_FILE} ${PORT} 38400 ${RS} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				if [ ! $? -eq 0 ];
				then
					echo "Error to upgrade ULINK radio !!!" >> ${MOUNTPOINT}/${LOGFILE}
					#
					# Remove extracted Files.
					#
					rm -rf ${MOUNTPOINT}/${TEMP_DIR}
					rm -f ${MOUNTPOINT}/${LISTFILE}
					rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
					#
					echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
					echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
					exit 5
				fi
				${FLASHTOOL} -d ${DATAFLASHDEV} -a 0x3927E --data=FF
			else
				echo "Unable to find tool 'ulink_loader'. Exiting" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				#
				# Remove extracted Files.
				#
				rm -rf ${MOUNTPOINT}/${TEMP_DIR}
				rm -f ${MOUNTPOINT}/${LISTFILE}
				rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
				#
				echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
				echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
				exit 5
			fi
		fi
		#
		# Removing temporary directory.
		#
		rm -rf ${MOUNTPOINT}/${TEMP_DIR}
		rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_ULINK}-*.tar.bz2 
	fi
fi

# +==========================================================================+
# +--------------------------------------------------------------------------+
# | UPGRADING GSM		                                                     |
# +--------------------------------------------------------------------------+
# +==========================================================================+
grep "${UPGRADE_PATTERN}-${TARGET_GSM}" ${MOUNTPOINT}/${LISTFILE} > /dev/null 2>&1
if [ $? -eq 0 ];
then
	TARGET=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_GSM}*.tar.bz2 | tail -1 | awk -F- '{print $2}')
	VERSION=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_GSM}*.tar.bz2 | tail -1 | awk -F- '{print $3}' | sed -e 's/\.tar.bz2$//g')
	if [ -z "${TARGET}" -o -z "${VERSION}" ]
	then
		echo "No valid upgrade file found for GSM upgrade" >> ${MOUNTPOINT}/${LOGFILE}
	else
		#
		# Creation of a temporary directory.
		#
		TEMP_DIR=tmp_gsm_$$
		if [ -d ${MOUNTPOINT}/${TEMP_DIR} ];
		then
			rm -f ${MOUNTPOINT}/${TEMP_DIR}
		fi
		mkdir ${MOUNTPOINT}/${TEMP_DIR}
		#
		# Uncompressing archive file pf500_upgrade-gsm-x.y.z.tar.bz2
		#
		echo "Uncompressing archive file '${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_GSM}-${VERSION}.tar.bz2 '" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
		${TARTOOL} xjf ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_GSM}-${VERSION}.tar.bz2 -C ${MOUNTPOINT}/${TEMP_DIR} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
		
		if [ ! $? -eq 0 ];
		then
			echo "Error to extract archive !!!" >> ${MOUNTPOINT}/${LOGFILE}
			#
			# Remove extracted Files
			#
			rm -rf ${MOUNTPOINT}/${TEMP_DIR}
			rm -f ${MOUNTPOINT}/${LISTFILE}
			rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
			#
			echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
			echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
			exit 5
		fi

		#
		# GSM upgrade, only if filesystem upgrade.
		#
		GSM_UPGRADE_PATH=${MOUNTPOINT}/${TEMP_DIR}
		GSM_NEW_TOOL_DIR=${TOOLSDIR}
		if [ -f ${GSM_UPGRADE_FILE} ];
		then
			if [ -f ${MOUNTPOINT}/${TEMP_DIR}/${GSM_LOADER_NAME} ];
			then
			echo "Find tool 'ulink loader on USB'" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
			chmod 777 ${MOUNTPOINT}/${TEMP_DIR}/${GSM_LOADER_NAME}
			GSM_LOADER=${MOUNTPOINT}/${TEMP_DIR}/${GSM_LOADER_NAME}
			GSM_NEW_TOOL_DIR=${MOUNTPOINT}/${TEMP_DIR}
			fi
			
			if [ -f ${GSM_LOADER} ];
			then
				cd ${GSM_NEW_TOOL_DIR} && ${GSM_LOADER} ${GSM_UPGRADE_PATH} /dev/ttyS1 115200 >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				if [ ! $? -eq 0 ];
				then
					echo "Error to upgrade GSM !!!" >> ${MOUNTPOINT}/${LOGFILE}
					#
					# Remove extracted Files.
					#
					rm -rf ${MOUNTPOINT}/${TEMP_DIR}
					rm -f ${MOUNTPOINT}/${LISTFILE}
					rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
					#
					echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
					echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
					exit 5
				fi
				${FLASHTOOL} -d ${DATAFLASHDEV} -a 0x3927E --data=FF
			else
				echo "Unable to find tool 'gsmloader'. Exiting" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				#
				# Remove extracted Files.
				#
				rm -rf ${MOUNTPOINT}/${TEMP_DIR}
				rm -f ${MOUNTPOINT}/${LISTFILE}
				rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
				#
				echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
				echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
				exit 5
			fi
		fi
		#
		# Removing temporary directory.
		#
		rm -rf ${MOUNTPOINT}/${TEMP_DIR}
		rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_GSM}-${VERSION}.tar.bz2 
	fi
fi

# +==========================================================================+
# +--------------------------------------------------------------------------+
# | UPGRADING CAN CONTROLLER                                                     |
# +--------------------------------------------------------------------------+
# +==========================================================================+
grep "${UPGRADE_PATTERN}-${TARGET_CAN}" ${MOUNTPOINT}/${LISTFILE} > /dev/null 2>&1
if [ $? -eq 0 ];
then
	TARGET=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_CAN}*.tar.bz2 | tail -1 | awk -F- '{print $2}')
	VERSION=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_CAN}*.tar.bz2 | tail -1 | awk -F- '{print $3}' | sed -e 's/\.tar.bz2$//g')
	if [ -z "${TARGET}" -o -z "${VERSION}" ]
	then
		echo "No valid upgrade file found for CAN controller upgrade" >> ${MOUNTPOINT}/${LOGFILE}
	else
		#
		# Creation of a temporary directory.
		#
		TEMP_DIR=tmp_can_$$
		if [ -d ${MOUNTPOINT}/${TEMP_DIR} ];
		then
			rm -f ${MOUNTPOINT}/${TEMP_DIR}
		fi
		mkdir ${MOUNTPOINT}/${TEMP_DIR}
		#
		# Uncompressing archive file pf500_upgrade-can-x.y.z.tar.bz2
		#
		echo "Uncompressing archive file '${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_CAN}-${VERSION}.tar.bz2 '" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
		${TARTOOL} xjf ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_CAN}-${VERSION}.tar.bz2 -C ${MOUNTPOINT}/${TEMP_DIR} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
		
		if [ ! $? -eq 0 ];
		then
			echo "Error to extract archive !!!" >> ${MOUNTPOINT}/${LOGFILE}
			#
			# Remove extracted Files.
			#
			rm -rf ${MOUNTPOINT}/${TEMP_DIR}
			rm -f ${MOUNTPOINT}/${LISTFILE}
			rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
			#
			echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
			echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
			exit 5
		fi

		#
		# CAN upgrade, only if filesystem upgrade.
		#
		CAN_UPGRADE_FILE=${MOUNTPOINT}/${TEMP_DIR}/can_fw.hex
		CAN_NEW_TOOL_DIR=${TOOLSDIR}
		if [ -f ${CAN_UPGRADE_FILE} ];
		then
			if [ -f ${MOUNTPOINT}/${TEMP_DIR}/${CAN_LOADER_NAME} ];
			then
			echo "Find tool 'ulink loader on USB'" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
			chmod 777 ${MOUNTPOINT}/${TEMP_DIR}/${CAN_LOADER_NAME}
			CAN_LOADER=${MOUNTPOINT}/${TEMP_DIR}/${CAN_LOADER_NAME}
			CAN_NEW_TOOL_DIR=${MOUNTPOINT}/${TEMP_DIR}
			fi
			
			if [ -f ${CAN_LOADER} ];
			then
				/usr/local/bin/oled_print --x 35 --y 20 --text "Upgrading" --clear
				/usr/local/bin/oled_print --x 45 --y 40 --text "CAN"
				cd ${CAN_NEW_TOOL_DIR} && ${CAN_LOADER} ${CAN_UPGRADE_FILE} /dev/ttyUSB2 115200 >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				if [ ! $? -eq 0 ];
				then
					echo "Error to upgrade CAN controller !!!" >> ${MOUNTPOINT}/${LOGFILE}
					#
					# Remove extracted Files.
					#
					rm -rf ${MOUNTPOINT}/${TEMP_DIR}
					rm -f ${MOUNTPOINT}/${LISTFILE}
					rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
					#
					echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
					echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
					exit 5
				fi
				${FLASHTOOL} -d ${DATAFLASHDEV} -a 0x3927E --data=FF
			else
				echo "Unable to find tool 'ulink_loader'. Exiting" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				#
				# Remove extracted Files.
				#
				rm -rf ${MOUNTPOINT}/${TEMP_DIR}
				rm -f ${MOUNTPOINT}/${LISTFILE}
				rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
				#
				echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
				echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
				exit 5
			fi
		fi
		#
		# Removing temporary directory.
		#
		rm -rf ${MOUNTPOINT}/${TEMP_DIR}
		rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_CAN}-${VERSION}.tar.bz2 
	fi
fi

# +==========================================================================+
# +--------------------------------------------------------------------------+
# | UPGRADING PF_PMU (only for pf800)                                        |
# +--------------------------------------------------------------------------+
# +==========================================================================+
grep "${UPGRADE_PATTERN}-${TARGET_PF_PMU}" ${MOUNTPOINT}/${LISTFILE} > /dev/null 2>&1
if [ $? -eq 0 ];
then
	#
	# Checking p_800_upgrade-pfpmu
	#
	grep "${UPGRADE_PATTERN_800}-${TARGET_PF_PMU}" ${MOUNTPOINT}/${LISTFILE} > /dev/null 2>&1
	if [ $? -eq 0 ];
	then
		#
		# Checking PF or PM
		#
		if [ -f /etc/PM_PF_Detector ];
		then
			/etc/PM_PF_Detector
			PRODUCT=$?
		else
			PRODUCT=0
		fi
		
		if [ ! ${PRODUCT} == 0 ];
		then
			TARGET=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_PF_PMU}*.tar.bz2 | tail -1 | awk -F- '{print $2}')
			VERSION=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_PF_PMU}*.tar.bz2 | tail -1 | awk -F- '{print $3}' | sed -e 's/\.tar.bz2$//g')
			if [ -z "${TARGET}" -o -z "${VERSION}" ]
			then
				echo "No valid upgrade file found for PF_PMU upgrade" >> ${MOUNTPOINT}/${LOGFILE}
			else
				#
				# Creation of a temporary directory.
				#
				TEMP_DIR=tmp_pf_pmu_$$
				if [ -d ${MOUNTPOINT}/${TEMP_DIR} ];
				then
					rm -f ${MOUNTPOINT}/${TEMP_DIR}
				fi
				mkdir ${MOUNTPOINT}/${TEMP_DIR}
				#
				# Uncompressing archive file p_x00_upgrade-pfpmu-x.y.z.tar.bz2
				#
				echo "Uncompressing archive file '${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_PF_PMU}-${VERSION}.tar.bz2 '" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				${TARTOOL} xjf ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_PF_PMU}-${VERSION}.tar.bz2 -C ${MOUNTPOINT}/${TEMP_DIR} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
				
				if [ ! $? -eq 0 ];
				then
					echo "Error to extract archive !!!" >> ${MOUNTPOINT}/${LOGFILE}
					#
					# Remove extracted Files.
					#
					rm -rf ${MOUNTPOINT}/${TEMP_DIR}
					rm -f ${MOUNTPOINT}/${LISTFILE}
					rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
					#
					echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
					echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
					exit 5
				fi

				#
				# PF_PMU upgrade, only if filesystem upgrade.
				#
				PF_PMU_UPGRADE_FILE=${MOUNTPOINT}/${TEMP_DIR}/pfpmu_fw.hex
				PF_PMU_NEW_TOOL_DIR=${TOOLSDIR}
				if [ -f ${PF_PMU_UPGRADE_FILE} ];
				then
					if [ -f ${MOUNTPOINT}/${TEMP_DIR}/${PF_PMU_LOADER_NAME} ];
					then
					echo "Find tool 'pfpmu loader on USB'" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
					chmod 777 ${MOUNTPOINT}/${TEMP_DIR}/${PF_PMU_LOADER_NAME}
					PF_PMU_LOADER=${MOUNTPOINT}/${TEMP_DIR}/${PF_PMU_LOADER_NAME}
					PF_PMU_NEW_TOOL_DIR=${MOUNTPOINT}/${TEMP_DIR}
					fi
					
					if [ -f ${PF_PMU_LOADER} ];
					then
						/usr/local/bin/oled_print --x 35 --y 20 --text "Upgrading" --clear
						/usr/local/bin/oled_print --x 45 --y 40 --text "PF_PMU"
						cd ${PF_PMU_NEW_TOOL_DIR} && ${PF_PMU_LOADER} ${PF_PMU_UPGRADE_FILE} /dev/ttyUSB4 115200 >> ${MOUNTPOINT}/${LOGFILE} 2>&1
						if [ ! $? -eq 0 ];
						then
							echo "Error to upgrade PF_PMU !!!" >> ${MOUNTPOINT}/${LOGFILE}
							#
							# Remove extracted Files.
							#
							rm -rf ${MOUNTPOINT}/${TEMP_DIR}
							rm -f ${MOUNTPOINT}/${LISTFILE}
							rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
							#
							echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
							echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
							exit 5
						fi
						${FLASHTOOL} -d ${DATAFLASHDEV} -a 0x3927E --data=FF
					else
						echo "Unable to find tool 'pfpmu_loader'. Exiting" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
						#
						# Remove extracted Files.
						#
						rm -rf ${MOUNTPOINT}/${TEMP_DIR}
						rm -f ${MOUNTPOINT}/${LISTFILE}
						rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
						#
						echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
						echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
						exit 5
					fi
				fi
				#
				# Removing temporary directory.
				#
				rm -rf ${MOUNTPOINT}/${TEMP_DIR}
			fi
		fi
	fi
	#
	# Removing upgrade file
	#
	rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_PF_PMU}-${VERSION}.tar.bz2 
fi

# +==========================================================================+
# +--------------------------------------------------------------------------+
# | UPGRADING POWER MANAGEMENT UNIT SOFTWARE                                 |
# +--------------------------------------------------------------------------+
# +==========================================================================+
grep "${UPGRADE_PATTERN}-${TARGET_PMU}" ${MOUNTPOINT}/${LISTFILE} > /dev/null 2>&1
if [ $? -eq 0 ];
then
	#
	# Look for filesystem upgrade file.
	#
	TARGET=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_PMU}*.tar.bz2 | tail -1 | awk -F- '{print $2}')
	VERSION=$(ls -rt ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_PMU}*.tar.bz2 | tail -1 | awk -F- '{print $3}' | sed -e 's/\.tar.bz2$//g')

	echo "Target is '${TARGET}', version is '${VERSION}'" >> ${MOUNTPOINT}/${LOGFILE}
	#
	# ${TARGET} and ${VERSION} must not be empty.
	#
	if [ -z "${TARGET}" -o -z "${VERSION}" ]
	then
    	echo "No valid upgrade file found for filesystem upgrade" >> ${MOUNTPOINT}/${LOGFILE}
  	else
    	#
    	# Create the temporary directory. If it already exists, it is removed
    	# and created again to start with an empty directory.
    	#
    	TEMP_DIR=tmp_pmu_$$
    	if [ -d ${MOUNTPOINT}/${TEMP_DIR} ];
    	then
      		rm -f ${MOUNTPOINT}/${TEMP_DIR}
    	fi
    	mkdir ${MOUNTPOINT}/${TEMP_DIR}
    	#
    	# Extract archive.
    	#
    	echo "Installing archive '${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET}-${VERSION}.tar.bz2 '" >> ${MOUNTPOINT}/${LOGFILE}
    	${TARTOOL} xjf ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET}-${VERSION}.tar.bz2 -C ${MOUNTPOINT}/${TEMP_DIR} >> ${MOUNTPOINT}/${LOGFILE} 2>&1
    	
	if [ ! $? -eq 0 ];
	then
		echo "Error to extract archive !!!" >> ${MOUNTPOINT}/${LOGFILE}
		#
		# Remove extracted Files.
		#
		rm -rf ${MOUNTPOINT}/${TEMP_DIR}
		rm -f ${MOUNTPOINT}/${LISTFILE}
		rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
		#
		echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
		echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
		exit 5
	fi

	rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-${TARGET_PMU}-${VERSION}.tar.bz2

	   	ls ${MOUNTPOINT}/${TEMP_DIR}/pmu*.hex > /dev/null 2>&1
    	if [ $? -eq 0 ];
    	then
    		if [ -f ${MOUNTPOINT}/${TEMP_DIR}/${PMU_LOADER_NAME} ];
			then
			echo "Find tool 'pmu_loader on USB'" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
			chmod 777 ${MOUNTPOINT}/${TEMP_DIR}/${PMU_LOADER_NAME}
			PMU_LOADER=${MOUNTPOINT}/${TEMP_DIR}/${PMU_LOADER_NAME}
			PMU_NEW_TOOL_DIR=${MOUNTPOINT}/${TEMP_DIR}
			fi

			if [ -f ${PMU_LOADER} ];
    		then
      			cd ${PMU_NEW_TOOL_DIR} && ${PMU_LOADER} $(ls ${MOUNTPOINT}/${TEMP_DIR}/pmu*.hex | tail -1) >> ${MOUNTPOINT}/${LOGFILE} 2>&1
    		else
      			echo "Unable to find tool 'pmu_loader'. Exiting" >> ${MOUNTPOINT}/${LOGFILE} 2>&1
	  			#
				# Remove extracted Files.
				#
				rm -rf ${MOUNTPOINT}/${TEMP_DIR}
				rm -f ${MOUNTPOINT}/${LISTFILE}
				rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
				#
				echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
				echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
				exit 5
    		fi
    	else
			#
			# Remove extracted Files.
			#
			rm -rf ${MOUNTPOINT}/${TEMP_DIR}
			rm -f ${MOUNTPOINT}/${LISTFILE}
			rm -f ${MOUNTPOINT}/${UPGRADE_PATTERN}-*.tar.bz2
			#
			echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
			echo "Exit code is 5" >> ${MOUNTPOINT}/${LOGFILE}
			exit 5
    	fi
    	
    	rm -rf ${MOUNTPOINT}/${TEMP_DIR}
  	fi
fi

#
# Remove temporary resource
#
rm -f ${MOUNTPOINT}/${LISTFILE}

${PRINTTOOL} --x=20 --y=32 --text="Upgrade done" --clear

echo "Ending USB script at " $(date) >> ${MOUNTPOINT}/${LOGFILE}
echo "Exit code is 0" >> ${MOUNTPOINT}/${LOGFILE}

# 
# Terminate the script with success return code.
#
# if MOUNTPOINT == /mnt/datas
if [ ${MOUNTPOINT} == ${UPGRADE} ];
then
	echo "MOUNTPOINT == /mnt/datas"
	#
	# if upgrade file is present,remove it.
	#
	rm -f ${MOUNTPOINT}/*.tar.bz2
	echo "upgrade_script  Upgrade file is removed."
fi
exit 0
# +--------------------------------------------------------------------------+
# | End of script.                                                           |
# +--------------------------------------------------------------------------+

