#!/bin/sh
set -xv

# shellscript for starting TomTom Open PixelMap (TTOPM)

# Because the ttopm binary looks in the current directory
# for maps, it is necessary to change the working directory
# to /mnt/sdcard/<ttopm-dir>/

# But first we have to find out <ttopm-dir>.
# $0 contains the fully path to this script, for example
# /mnt/sdcard/ttpm2/ttopm.sh
# In this example the script has to change the directory 
# to /mnt/sdcard/ttopm/

# This is only necessary if the script is call with 
# full pathname (for example /mnt/sdcard/ttopm/ttopm.sh)
# This is the case when the application is started with
# the icon in the native TomTom software.

echo $0|grep ^/
if [ $? = 0 ]
then
  filename=`echo "$0"|sed 's#^/.*/##g'`
  path=`echo $0|sed "s/$filename//"`
  cd $path
fi

destination="/lib"

mkdir -p "$destination" 2>/dev/null

ln -s /mnt/sdcard/ttopm/libfreetype.la /lib/libfreetype.la
ln -s /mnt/sdcard/ttopm/libgd.la /lib/libgd.la
ln -s /mnt/sdcard/ttopm/libjpeg.la /lib/libjpeg.la
ln -s /mnt/sdcard/ttopm/libpng12.la /lib/libpng12.la
ln -s /mnt/sdcard/ttopm/libpng.la /lib/libpng.la
ln -s /mnt/sdcard/ttopm/libz.a /lib/libz.a
ln -s /mnt/sdcard/ttopm/libgd.so.2.0.0 /lib/libgd.so.2.0.0
ln -s /mnt/sdcard/ttopm/libpng12.so.0.24.0 /lib/libpng12.so.0.24.0
ln -s /mnt/sdcard/ttopm/libpng.so.3.24.0 /lib/libpng.so.3.24.0
ln -s /mnt/sdcard/ttopm/libpng12.a /lib/libpng12.a
ln -s /mnt/sdcard/ttopm/libgd.a /lib/libgd.a
ln -s /mnt/sdcard/ttopm/libjpeg.so.62.0.0 /lib/libjpeg.so.62.0.0
ln -s /mnt/sdcard/ttopm/libjpeg.a /lib/libjpeg.a
ln -s /mnt/sdcard/ttopm/libfreetype.so.6.3.16 /lib/libfreetype.so.6.3.16
ln -s /mnt/sdcard/ttopm/libfreetype.a /lib/libfreetype.a

ln -s /mnt/sdcard/ttopm/libfreetype.so.6.3.16 /lib/libfreetype.so
ln -s /mnt/sdcard/ttopm/libfreetype.so.6.3.16 /lib/libfreetype.so.6
ln -s /mnt/sdcard/ttopm/libjpeg.so.62.0.0 /lib/libjpeg.so.62
ln -s /mnt/sdcard/ttopm/libjpeg.so.62.0.0 /lib/libjpeg.so
ln -s /mnt/sdcard/ttopm/libpng12.a /lib/libpng.a
ln -s /mnt/sdcard/ttopm/libpng.so.3.24.0 /lib/libpng.so.3
ln -s /mnt/sdcard/ttopm/libpng12.so.0.24.0 /lib/libpng.so
ln -s /mnt/sdcard/ttopm/libpng12.so.0.24.0 /lib/libpng12.so
ln -s /mnt/sdcard/ttopm/libpng12.so.0.24.0 /lib/libpng12.so.0
ln -s /mnt/sdcard/ttopm/libgd.so.2.0.0 /lib/libgd.so
ln -s /mnt/sdcard/ttopm/libgd.so.2.0.0 /lib/libgd.so.2


# the shellscript looks for configfiles with 
# a two-digit number in it: config-xx.txt
# It is not bad if there is no such configfile,
# because the ttopm binary look by default for 
# config.txt

first=`ls config-[0-9][0-9].txt|sort|head -1`
last=`ls config-[0-9][0-9].txt|sort|tail -1`

find_succ()
{
# This function finds the configfile with the closest
# larger number in it ($succ = successor)
# Most likely this is used for zoom in.
     if [ $current = $last  ]
     then
       succ=$current
     else
       found="0"
       for i in `ls config-[0-9][0-9].txt|sort`
       do
        if [ $found = 1 ]
          then succ=$i ; break
        fi
        if [ $i = $current ]
        then
          found="1"
        fi
       done
     fi
}

# This function finds the previous configfile with the 
# closest smaller number in it. ($prev = previous)
# Most likely this is used for zoom out.
find_prev()
{
prev=$current
for i in `ls config-*.txt|sed 's/-\([0-9]\)\./-0\1./g'|sort`
do
 if [ $i = $current ]
 then
  ergebnis=$prev
  break
 fi
 prev=$i
done
}


# start with the config file with the smallest number 
# in it.
current=$first

while true; do
  ./ttopm $current >stdout.log 2>./err.log
  statuscode=$?
  if [ $statuscode = 0 ]
  then  # leave ttopm
    exit 0
  fi
  if [ $statuscode = 2 ]
  then # zoom in
    find_succ
    current=$succ
  fi
  if [ $statuscode = 3 ]
  then # zoom out
    find_prev
    current=$prev
  fi
done



