#!/bin/sh
##############################################################################
# Configuration
##############################################################################
product="TomTom GO"
fstype="vfat"
fsdevs="ttsd mmcblk0 hda"
mntopt="rw,sync,exec,noatime,nodev,nosuid"
devbase="/dev"
sdmnt="/mnt/sdcard"
flashdir="/mnt/flash"
ttnapp="ttn"
ttndir="/bin"
ttpnpdapp="ttpnpd"
ttpnpddir="/bin"

#Change for GL gps receiver
gl_baudrate="115200"
gl_asic="/dev/gpsdata"
flash_gl=/mnt/flash/extra_settings

##############################################################################
# Functions
##############################################################################

start_clm() {
  # Determine whether we want to start up the CLM daemon
  if test "${hw_gpsephemeris}" = "1"; then
    if test -f /var/run/sirfreset.pid; then
      echo "* Waiting for application to finish..."
      wait $(cat /var/run/sirfreset.pid)
      echo "*  ...finished"
    fi
    # Check the clmdata file and copy to ramdisk
    if test -f /mnt/sdcard/clmdata; then
      cp /mnt/sdcard/clmdata /var/run/
      sync
    fi
    echo "* Starting clmapp"
    clmapp &
  fi
}

start_ttn() {
  if test "${hw_gldetected}" = "0"; then
    if test "${hw_gpsephemeris}" = "0" -a -f /var/run/sirfreset.pid; then
      echo "* Waiting for application to finish..."
      wait $(cat /var/run/sirfreset.pid)
      echo "*  ...finished"
    fi
  fi
  if test -f /etc/debug; then
    if test -f ${sdmnt}/${ttnapp}; then
      appdir=${sdmnt}
    else
      appdir=${ttndir}
    fi
    echo "* Starting ${appdir}/${ttnapp}"
    ${appdir}/${ttnapp} &
  else
    echo "* Starting ${product}"
    ${ttndir}/${ttnapp} </dev/null >/dev/null 2>&1 &
  fi
}

##############################################################################

cd /mnt/$1/ttMaps/
running=0
while [ "$running" -eq "0" ] ; do
	ps | grep -v grep | grep startttmaps.sh
	running=$?
done

killall ttn >ttmaps.log 2>&1

running=0
while [ "$running" -eq "0" ] ; do
	ps | grep -v grep | grep ttn
	running=$?
done

# Launch ttMaps
PROJ_LIB=/mnt/$1/ttMaps/proj GCONV_PATH=/mnt/$1/ttMaps LOCPATH=/mnt/$1/ttMaps /mnt/$1/ttMaps/ttmaps >>ttmaps.log 2>&1
start_ttn
