#!/bin/bash

# this is the fourth script in the RCP startup sequence
# it's called from launcher 
# it monitors for an update file and
# starts the update process when it sees it
# NB: As currently written, once it sees an update file
#     and triggers the update process, it is no longer
#     running; if the user declines the update, they 
#     will not be prompted again until they've rebooted
#     the unit (ditto even if they accept the update). 
#     This script could re-launch itself after a suitable
#     delay (e.g., 60 seconds) as it exits so the user
#     would be periodically re-prompted to do the update
#     so long as the USB is in place, which seems like
#     the right thing to do unless there are other reasons
#     to have a USB containing an updated script installed?
#     To implement this behavior, uncomment lines at end.

# test whether we're logging by existence of the logfile
logfile=/home/pi/rcplog.txt
logging=$(test -e $logfile && echo 1)

# update filename
updateprefix=IURCP
updateext=tar


# monitor for presence of update file on mounted devices (e.g., USB)
while [ ! -f /media/pi/*/$updateprefix*.$updateext ]; do
	sleep 1
done

# when found, launch GUI app to get user confirmation to proceed w/ update
[ $logging ] && sudo echo "updatemonitor starting UpdateConfirm" >> $logfile
sudo /home/pi/UpdateConfirm &

# restart self to continue monitoring whether or not user installed update
sleep 60
[ $logging ] && sudo echo "updatemonitor restarting itself" >> $logfile
sudo /bin/bash /home/pi/bin/updatemonitor &

