# FirmwareUpdateHelper_dev.sh (equivalent to FirmwareUpdateHelper.sh but for develpment releases)
# This script is called by SystemMonitor during the firmware update process
# after copying and extracting the tar file to the sd card /Downloads folder
echo 
echo "Running FirmwareUpdateHelper_dev.sh"
echo 

SD_PATH=/run/media/mmcblk1p1/
DEFAULT_PATH=/dev/Downloads/
DOWNLOAD_PATH=${SD_PATH}Downloads/
BACKUPS_PATH=${SD_PATH}Backups/

echo "Killing all Insight processes..."
pkill Insight
echo "done"

sleep 2

pkill -9 Insight

# Restart dropbear to prevent dropbear from holding up the UART
echo "Force kill of dropbear..."
pkill -9 dropbear

## Delete stored data [Causes Backlogged data loss]
## 	MAG enabled these for develpment 11/5/2019
echo "Deleting database..."
rm -f /Insight/log/repo.db
rm -f /Insight/core_accumulator.nv
rm -f /run/media/mmcblk1p1/repo.db
rm -f /run/media/mmcblk1p1/core_accumulator.nv

echo "Backing up settings..."
mkdir -p ${BACKUPS_PATH}
# MAG - 10-7-2021 - Since we are allowing the kernel update to replace the apps, we are no longer backing them up
# Instead we are only backing up the settings unique to this device.
# cp -r /Insight/{Applications,lib,Scripts,keys,repo.config,config.json} ${SD_PATH}/Backups
# These files need to be restored at the next startup.  FirmwareUpdateResetHelper.sh will restore these files.
tar -cvf ${BACKUPS_PATH}InsightBackup.tar -X /Insight/BackupExclusions.txt /Insight /etc/wpa_supplicant.conf /etc/network/interfaces 
echo "done backing up settings"

# echo "Replacing old binaries..."
# MAG - 10-7-2021 - This was removed for the Rev D update since there is no plan to replace the 
# applications independantly of the kernel.  This can be restored for an incremental app update in
# the future.
# if [ -d "$SD_PATH" ]; then
# 	cp -a ${SD_PATH}Insight/{Applications,lib,Scripts,keys,repo.config,config.json} /Insight/ 2>/dev/null
# 	rm -rf ${SD_PATH}Insight/{Applications,lib,Scripts,keys,repo.config,config.json}
# 	rm -f ${SD_PATH}*.swu
# 	cp -a ${SD_PATH}Downloads/Insight/{Applications,lib,Scripts,keys,repo.config,config.json} /Insight/
# 	echo "Copied binaries from $SD_PATH"
# 	echo "Copy psudo SystemMonitor script from SD (${SD_PATH}) to Applcations"
# 	cp -a ${SD_PATH}Insight/Scripts/psudoSystemMonitor.sh /Insight/Applications/SystemMonitor
# else
# 	cp -a ${DEFAULT_PATH}Insight/{Applications,lib,Scripts,keys,repo.config,config.json} /Insight/ 2>/dev/null
# 	echo "Copied binaries from $DEFAULT_PATH"
# 	echo "Copy psudo SystemMonitor script from default path (${DEFAULT_PATH}) to Applcations"
# 	cp -a ${DEFAULT_PATH}Insight/Scripts/psudoSystemMonitor.sh /Insight/Applications/SystemMonitor
# fi

# Delete logs to accommodate new log file size. Remove this for releases after Rev C
rm /Insight/log/*
echo "Deleted all existing logs !"

# copy kernel/fs update files and scripts (.ubifs, .imx, .scr) to SD root
echo "Copying update files to SD: this will take a few moments"
tar -xvf ${DOWNLOAD_PATH}KernelFSImageFiles.tar -C ${SD_PATH}
#rm -f ${SD_PATH}Downloads/KernelFSImageFiles.tar
rm -f ${SD_PATH}Downloads/*.tar

# Move *_LoadLast.hex files to the backup directory 
# They will be retrieved by FirmwareUpdateHelper.sh
# and moved to Download folder for later install
echo "Check for *_LoadLast.hex files for reverse order bootload"
count=`ls -1 ${DOWNLOAD_PATH}*_LoadLast.hex  2>/dev/null | wc -l` # wild card file detection
if [ $count != 0 ]; then
    echo "_LoadLast.hex files exists."
	echo "Rename *_LoadLast.hex update files proceed with kernel update"
	mv ${DOWNLOAD_PATH}*_LoadLast.hex ${BACKUPS_PATH}
fi

# MAG 10-7-2021 - This is the new firmware update method using the SD script
# The .swu method is no longer supported accross kernel versions.
echo "***** set u-boot command and reboot ******"
fw_setenv bootcmd "run install_linux_fw_sd"
sync 
reboot
#----------------------------------------------------------------------------------
# MAG 10-7-2021 - this is the old app restart methods when replacing only the apps
# This is retained here for reference in case we want to do an app only (non-kernel)
# update in the future.
# echo "Restarting processes"
# chmod +x /Insight/Applications/* /Insight/Scripts/*
# /Insight/Applications/SystemMonitor -x
# echo "done"

# echo "Waiting for SystemMonitor to start and terminate this script"
# sleep 5
# # Check for file generated by system monitor to signal termination of the script
# FILE=/Insight/Scripts/terminateFirmwareUpdateHelper
# if test -f "$FILE"; then
#     echo "$FILE exists. Terminate this firmware update helper script"
# 	rm "$FILE" # remove the file that signals script termination, then exit
# 	exit 1
# else
#     echo "$FILE does not exist. Continue execution of firmware update helper script "
# fi
# echo "done"

# ############################################################
# # Firmware update has failed. Revert back to old firmware
# ############################################################

# echo "!!! Upgrade failed. Killing processes..."
# # Kill all Insight processes 
# pkill -9 Insight
# echo "done"

# echo "Reverting to old files..."
# # revert to old files
# cp -a /dev/Backups/* /Insight/
# echo "done"

# echo "Restarting processes"
# # Restart processes
# /Insight/Applications/SystemMonitor -x
# echo "done"
