#!/bin/sh
if [ ! -d /mnt/sdcard/photos ]; then
        mkdir /mnt/sdcard/photos
fi

# Test on which SD card ttMaps has been installed
if [ -f /mnt/movinand/ttMaps/ttmaps ]; then
     ttmapsdir="/mnt/movinand/ttMaps"
else
     ttmapsdir="/mnt/sdcard/ttMaps"
fi

# Test TomTom Application version
if [ -f /mnt/sdcard/ttgo.ori ] ; then
	VersionString=$(grep ApplicationVersionVersionNumber /mnt/sdcard/ttgo.ori)
else
	VersionString=$(grep ApplicationVersionVersionNumber /mnt/sdcard/ttgo.bif)
fi
AppMainVersion=$(echo $VersionString | cut -c 33)
AppVersion=$(echo $VersionString | cut -c 33-34)
echo "Application Version="$AppVersion > $ttmapsdir/ttmaps.log
echo "Main Version="$AppMainVersion >> $ttmapsdir/ttmaps.log
if [ "$AppMainVersion" = "" ] ; then
	echo "The ttgo.bif file is missing."   >> /mnt/$1/ttMaps/ttmaps.log
elif [ "$AppMainVersion" = "4" ] || [ "$AppMainVersion" = "5" ]  || [ "$AppMainVersion" = "6" ]  ; then
	echo "The TomTom application you are using is too old, please update it with TomTom Home"   >> $ttmapsdir/ttmaps.log
else
	cd $ttmapsdir/
	# Launch ttMaps
	if [ "$AppMainVersion" = "7" ]  || [ "$AppVersion" = "80" ] || [ "$AppVersion" = "81" ] ; then
		PROJ_LIB=$ttmapsdir/proj GCONV_PATH=$ttmapsdir LOCPATH=$ttmapsdir $ttmapsdir/ttmaps -p -s >>ttmaps.log 2>&1
	else
		PROJ_LIB=$ttmapsdir/proj GCONV_PATH=$ttmapsdir LOCPATH=$ttmapsdir $ttmapsdir/ttmaps -s >>ttmaps.log 2>&1
	fi
	rc=$?
	killall ttpnpd
	killall clmapp
	killall gltt
	killall glgps
	echo "/etc/showopenfiles" >> ttmaps.log
	/etc/showopenfiles >> ttmaps.log 2>&1

	if [ $rc -eq 32 ] || [ $rc -eq 33 ]; then
		echo "Power off" >> ttmaps.log 
		cd /
		umount /mnt/$1
		umount /mnt/sdcard
		sync
		sync
		ln -s /sbin/sysreboot /bin/poweroff
		sleep 1
		/bin/poweroff
	else
		echo "Reboot" >> ttmaps.log 
		cd /
		umount /mnt/movinand
		umount /mnt/sdcard
		sync
		sync
		/sbin/reboot
	fi
fi


