#!/bin/sh
# Sanstreak Corporation provides this source code solely on the terms and
# conditions set forth in the edgertronic by Sanstreak Usage Agreement and
# on the condition that you accept and comply with them.  You may view the
# most recent version of the Usage Agreement at:
# http://wiki.edgertronic.com/index.php/Usage_Agreement.
#
# Delete unmodified configuration files before creating settings.tar.gz file
#
# Todd Fischer, RidgeRun April 2022

UPDATE_TARBALL_FILE=$1

RW_MOUNT=/mnt/rw
SD_MOUNT=/mnt/sdcard
CONFIG_FILE_LIST=/usr/local/share/config-file-list


update_log()
{
	echo "$*" >> $UPDATE_LOG
}


setup_log()
{
	TARBALL=$1
	VERSION=`echo $TARBALL | cut -f7 -d.`

	if [ "${VERSION:0:1}" != "v" ] ; then
		VERSION=`echo $TARBALL | cut -f3 -d.` #use datecode
	fi

	export UPDATE_LOG=$SD_MOUNT/installed/update_$VERSION.log
	mkdir -p $SD_MOUNT/installed
	update_log "Update script running from update tarball: $TARBALL"
	update_log "Update script running before installing software version: $VERSION"
}


delete_unmodified_config_files()
{
	update_log "Deleting all configuration files from the read/write file system that have not been modified"
	if [ -r $CONFIG_FILE_LIST ] ; then
		for FILE in `cat $CONFIG_FILE_LIST` ; do
			if [ -r "$FILE.default" -a -r "$RW_MOUNT/$FILE" ] ; then
				if cmp -s "$FILE.default" "$RW_MOUNT/$FILE" ; then
					rm -f "$RW_MOUNT/$FILE"
				else
					update_log "User modified configuration file: $FILE"
				fi
			fi
		done
	fi
}


# main()

setup_log "$UPDATE_TARBALL_FILE"
update_log "Starting execution of update-script provided in update tarball"
delete_unmodified_config_files
update_log "Finished execution of update-script provided in update tarball"
