# FirmwareUpdateResetHelper.sh
# This script is called by SystemMonitor after the SOM resets during
# the firmware update process to do any cleanup needed.
echo 
echo "Running FirmwareUpdateResetHelper.sh"
echo 

SD_PATH=/run/media/mmcblk1p1/
DOWNLOAD_PATH=${SD_PATH}Downloads/
BACKUPS_PATH=${SD_PATH}Backups/
NETWORK_PATH=/etc/network/

# Restore settings from backup
tar -xvf ${BACKUPS_PATH}InsightBackup.tar
cp /Insight/etc/wpa_supplicant.conf /etc
cp /Insight/etc/network/interfaces /etc/network
rm -rf /Insight/etc

# Kill udpcpc if it is running
pkill udhcpc

# cleanup
rm ${SD_PATH}/*.ubifs
rm ${SD_PATH}/*.imx
rm ${SD_PATH}/*.scr
# Download path removed in SystemMonitor?? FileManagement::USBMonitorThreadHandler()
# rm -rf ${DOWNLOAD_PATH}

##### Load hex files after SOM update ####
# To Update the SOM first, the InsightApp.hex and InsightBoot.hex must be missing.  This will cause
# their update to be skipped and go directly to the SOM update.  When the SOM update finishes, this 
# script is run.  If we detect the file InsightBoot_LoadLast.hex, this signals that the files
# InsightApp_LoadLast.hex and InsightBoot_LoadLast.hex are to be renamed to InsightApp.hex and 
# InsightBoot.hex and moved to the backup folder since the application will delete the downloads folder.  
# The script then calls reboot.  Upon program start the hex files will be copied back to the downloads
# folder by FirmwareUpdateHelper.sh but the SOM files will not be found but the hex files will so they
# will load.
echo "locating _LoadLast.hex files in backup folder"
count=`ls -1 ${BACKUPS_PATH}*_LoadLast.hex 2>/dev/null | wc -l` # wild card file detection
echo "${count} _LoadLast.hex files found"
if [ $count != 0 ]; then  # This is the E and F version update reset action
	# _LoadLast files found in the backup folder indicating a reverse bootload 
	# where kernel/fs was loaded first.  Now copy the hex files to the download
	# folder to load next.
    echo "$FILE exists."
	echo "Rename *_LoadLast.hex update files and restart..."
	mv ${BACKUPS_PATH}PreBoot_LoadLast.hex ${DOWNLOAD_PATH}PreBoot.hex
	mv ${BACKUPS_PATH}InsightApp_LoadLast.hex ${DOWNLOAD_PATH}InsightApp.hex
	mv ${BACKUPS_PATH}InsightBoot_LoadLast.hex ${DOWNLOAD_PATH}InsightBoot.hex

	echo "InsightApp/Boot_LoadLast.hex files renamed and copied to Download path.  Now continuing to app start..."
	# Continue to application startup which will perform the hex file load to carrier
else # This is the G and beyond version update reset action
	# Perform normal bootload cleanup
	echo "Removing backup path"
	rm -rf ${BACKUPS_PATH}

	# Remove carrier files so they don't reload
	echo "Removing Download carrier files"
	rm -f ${DOWNLOAD_PATH}InsightApp*
	rm -f ${DOWNLOAD_PATH}InsightBoot*

	# We are finished with the Download folder so remove it
	rm -rf ${DOWNLOAD_PATH}

 	# check for USB presence (typically /run/media/sda )
	USB_PRESENCE=$(ls -l /run/media/ |grep -c sd)
	if [[ ${USB_PRESENCE} = 1 ]]; then
		echo "Reset helper script detected that USB is present"
		echo "Inserting /Insight/UpdateFinished_USB_Present update inhibit file"
		# Add inhibit file to prevent restart of firmware update
		echo -n > /Insight/UpdateFinished_USB_Present
	fi
	
	# update interfaces to include startup parameters for udhcpc where dhcp is configured
	# First grep for existence of the startup parameters
	UDHCPC=$(grep -A3 "iface\s*eth1" $NETWORK_PATH/interfaces | grep -c udhcpc_opts)
	# Insert command line options only if grep did not find them
	if [ ${UDHCPC} == 0 ]; then
			# Insert command line parameters after the eth1 interface designation
			sed -i '/iface\s*eth1\s*inet\s*dhcp/a \
			# DHCP options -t=max_tries -A=timeout -n=exit_if_no_lease -q=exit_if_lease -b=run_in_bacgroud \
			udhcpc_opts -t 3 -A 20 -nqb' \
			$NETWORK_PATH/interfaces
	fi

fi