#! /bin/sh
# a copier à la racine de la carte SD sous "ttn"
#
###############################################################################
# This is free and unencumbered software released into the public domain.
# 
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
# 
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to: 
# https://www.aplu.fr/v2/post/2013/08/22/Problème-TomTom-XL-IQ-Routes-Edition
#
###############################################################################

mount_flash() {
	flashdir=$1
	flash_dev=$2
	flash_opts=$3
	echo "WARNING: ${flashdir} seems to be corrupt, reinitializing..."
	if ! umount ${flashdir}; then
		echo "WARNING: Unmounting ${flashdir} failed!"
		return 1
	fi
	if ! flash_erase /dev/${flash_dev} 0x40000 0x40000; then
		echo "WARNING: Erasing ${flashdir} failed!"
		return 1
	fi
	if ! mount -n -t ngffs ${flash_dev} ${flashdir}; then
		echo "WARNING: Mounting ${flash_dev} on ${flashdir} failed again!"
		return 1
	fi
	echo "Successfully reinitalized ${flashdir}."
	return 0
}
 
touch /mnt/sdcard/usecmdlog.dat
if mount_flash /mnt/flash mtd0 rw,sync,noatime,nosuid,nodev ; then
	umount /mnt/flash
fi
rm -f /mnt/sdcard/usecmdlog.dat
rm -f /mnt/sdcard/ttn
/sbin/reboot
