#!/bin/bash

# bash script for working with a patched gpsbabel version to
# set/get logs/points of the Navin MiniHomer GPS device
# mail@georgruss.de 2011-08-17

GPSB=/home/russ/bin/gpsbabel
TTY=/dev/ttyUSB0
HOME=/home/russ

# for erasing the miniHomer's log
if [ $1 = "erase" ]; then
				echo "erasing miniHomer log"
				$GPSB -i miniHomer,baud=115200,erase -f $TTY
fi

# for setting the five coordinates
if [ $1 = "set" ]; then

				if [ $2 = "1" ]; then
					$GPSB -i miniHomer,baud=38400,Home=$3:$4,no-output -f $TTY
				fi

				if [ $2 = "2" ]; then
					$GPSB -i miniHomer,baud=38400,Car=$3:$4,no-output -f $TTY
				fi

				if [ $2 = "3" ]; then
					$GPSB -i miniHomer,baud=38400,Boat=$3:$4,no-output -f $TTY
				fi

				if [ $2 = "4" ]; then
					$GPSB -i miniHomer,baud=38400,Heart=$3:$4,no-output -f $TTY
				fi

				if [ $2 = "5" ]; then
					$GPSB -i miniHomer,baud=38400,Bar=$3:$4,no-output -f $TTY
				fi
fi

# for reading the tracks from the logs

if [ $1 = "read" ]; then

				#$GPSB -t -i miniHomer,baud=38400,read-at-once=16 -f $TTY -o gpx -F ~/tmp/tracks.gpx 
				$GPSB -t -i miniHomer,baud=38400 -f $TTY -x track,split,title="gpslog-%Y%m%d" -o gpx -F ~/tmp/tracks.gpx 
				#$GPSB -i skytraq,baud=38400 -f $TTY -o gpx -F ~/tmp/tracks.gpx 

fi

# for initializing the device, may help

if [ $1 = "init" ]; then

				$GPSB -i miniHomer,initbaud=0,no-output -f $TTY

fi


# dump and convert log to GPX
# correct for wrong logging date
# YES, the first command DOES segfault, but still correctly dumps the log
# which can be read by gpsbabel
if [ $1 = "dump" ]; then

				$GPSB -i miniHomer,baud=38400,dump-file=$HOME/tmp/logdump -f $TTY
				$GPSB -t -i skytraq-bin -o gpx $HOME/tmp/logdump $HOME/tmp/log.gpx
				$GPSB -t -i gpx -f ~/tmp/log.gpx -x track,move=-172032h -o gpx -F ~/tmp/log.gpx

fi
