#!/bin/sh

# Geocaching .loc
rm -f ${TMPDIR}/gl.loc
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o geo -F ${TMPDIR}/gl.loc
compare ${TMPDIR}/gl.loc ${REFERENCE}

# GPSUtil
rm -f ${TMPDIR}/gu.wpt ${TMPDIR}/1.gpx ${TMPDIR}/2.gpx
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o gpsutil -F ${TMPDIR}/gu.wpt
compare ${TMPDIR}/gu.wpt ${REFERENCE}
gpsbabel -i gpsutil -f ${TMPDIR}/gu.wpt -o gpx -F ${TMPDIR}/1.gpx
gpsbabel -i gpsutil -f ${REFERENCE}/gu.wpt -o gpx -F ${TMPDIR}/2.gpx
compare ${TMPDIR}/1.gpx ${TMPDIR}/2.gpx

# GPSman 
rm -f ${TMPDIR}/gm.gm ${TMPDIR}/gm.gm+
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o gpsman -F ${TMPDIR}/gm.gm
gpsbabel -i gpsman -f ${TMPDIR}/gm.gm -o gpsutil -F ${TMPDIR}/gm.gm+
compare ${TMPDIR}/gm.gm+ ${TMPDIR}/gu.wpt

# GPX
rm -f ${TMPDIR}/gl.gpx ${TMPDIR}/gpx.gpx
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o gpx -F ${TMPDIR}/gl.gpx
gpsbabel -i gpx -f ${TMPDIR}/gl.gpx -o gpsutil -F ${TMPDIR}/gpx.gpx
compare ${TMPDIR}/gpx.gpx ${TMPDIR}/gu.wpt

# GTM
rm -f ${TMPDIR}/gl.gpx ${TMPDIR}/gpx.gpx
gpsbabel -i gtm -f ${REFERENCE}/sample.gtm -o gpx -F ${TMPDIR}/gtm1.gpx
gpsbabel -i gpx -f ${TMPDIR}/gtm1.gpx -o gtm -F ${TMPDIR}/gtm.gtm
gpsbabel -i gtm -f ${TMPDIR}/gtm.gtm -o gpx -F ${TMPDIR}/gtm2.gpx
compare ${TMPDIR}/gtm1.gpx ${TMPDIR}/gtm2.gpx
bincompare ${TMPDIR}/gtm.gtm ${REFERENCE}/sample.gtm
#
# GTM compressed files
# ... do the same as above but with gzipped gtm files
#
rm -f ${TMPDIR}/gl.gpx ${TMPDIR}/gpx.gpx
gpsbabel -i gtm -f ${REFERENCE}/sample.gtm.gz -o gpx -F ${TMPDIR}/gtm1.gpx
gpsbabel -i gpx -f ${TMPDIR}/gtm1.gpx -o gtm -F ${TMPDIR}/gtm.gtm.gz
gpsbabel -i gtm -f ${TMPDIR}/gtm.gtm.gz -o gpx -F ${TMPDIR}/gtm2.gpx
compare ${TMPDIR}/gtm1.gpx ${TMPDIR}/gtm2.gpx
bincompare ${TMPDIR}/gtm.gtm.gz ${REFERENCE}/sample.gtm.gz
gunzip -c ${TMPDIR}/gtm.gtm.gz > ${TMPDIR}/gtm.gtm
gunzip -c ${REFERENCE}/sample.gtm.gz > ${TMPDIR}/sample.gtm
bincompare ${TMPDIR}/gtm.gtm ${REFERENCE}/sample.gtm

# Magellan Mapsend
rm -f ${TMPDIR}/mm.mapsend ${TMPDIR}/mm.gps
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o mapsend -F ${TMPDIR}/mm.mapsend
gpsbabel -i mapsend -f ${TMPDIR}/mm.mapsend -o gpsutil -F ${TMPDIR}/mm.gps
compare ${TMPDIR}/mm.gps ${TMPDIR}/gu.wpt

# Magellan serial
# TODO

# Tiger
# This one is a little tacky, becuase it's a very lossy format.
# so we simply test we can write it, and then read it and write it and
# get an identical file back.
rm -f ${TMPDIR}/tiger
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o tiger -F ${TMPDIR}/tiger
gpsbabel -i tiger -f ${TMPDIR}/tiger -o tiger -F ${TMPDIR}/tiger2
compare ${TMPDIR}/tiger ${TMPDIR}/tiger2

#
# Lowrance USR.  Binary, and also slightly lossy because of the math to
# convert lat/long.  It also doesn't support description, which makes it
# awkward  to test.
#
rm -f ${TMPDIR}/lowrance1.usr
rm -f ${TMPDIR}/enchilada1.usr
rm -f ${TMPDIR}/enchilada.gpx
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o lowranceusr -F ${TMPDIR}/lowrance1.usr
bincompare ${TMPDIR}/lowrance1.usr ${REFERENCE}/lowrance.usr
gpsbabel -i lowranceusr -f ${TMPDIR}/lowrance1.usr -o lowranceusr -F ${TMPDIR}/lowrance1.usr
# And because of the FP rounding, we can't even read our file, write it back
# and get the same data.  Sigh. 
# bincompare ${REFERENCE}/lowrance.usr  ${TMPDIR}/lowrance1.usr
gpsbabel -i lowranceusr -f ${REFERENCE}/all.usr -o gpx -F ${TMPDIR}/enchilada.gpx
gpsbabel -i gpx -f ${TMPDIR}/enchilada.gpx -o lowranceusr -F ${TMPDIR}/enchilada1.usr
bincompare ${TMPDIR}/enchilada1.usr ${REFERENCE}/enchilada.usr
# Don't convert icons as waypts
gpsbabel -i lowranceusr,ignoreicons -f ${REFERENCE}/all.usr -o gpx -F ${TMPDIR}/enchilada.gpx
gpsbabel -i gpx -f ${TMPDIR}/enchilada.gpx -o lowranceusr -F ${TMPDIR}/enchilada1.usr
bincompare ${TMPDIR}/enchilada1.usr ${REFERENCE}/ignoreicons.usr

#
# Another variation of Lowrance.  Compare v2 and v3.  These original
# files were saved on the same units as v2 and v3.
#
${PNAME} -i lowranceusr -f ${REFERENCE}/lowrance-v2.usr -o unicsv,utc=0 -F ${TMPDIR}/lowrance-v2-unicsv.txt
${PNAME} -i lowranceusr -f ${REFERENCE}/lowrance-v3.usr -o unicsv,utc=0 -F ${TMPDIR}/lowrance-v3-unicsv.txt
compare ${REFERENCE}/lowrance-v2-unicsv.txt ${TMPDIR}/lowrance-v2-unicsv.txt
compare ${REFERENCE}/lowrance-v3-unicsv.txt ${TMPDIR}/lowrance-v3-unicsv.txt
# Ideally, there'd be a test for v2 vs. v3 writes, but their numeric
# instability makes this icky.

#
# CSV (Comma separated value) data.

gpsbabel  -i geo -f ${REFERENCE}/../geocaching.loc -o csv -F ${TMPDIR}/csv.csv
gpsbabel  -i csv -f ${TMPDIR}/csv.csv -o csv -F ${TMPDIR}/csv2.csv
compare ${TMPDIR}/csv2.csv ${TMPDIR}/csv.csv 
# And the alternate command line version.
gpsbabel  -i csv -o gpx ${TMPDIR}/csv.csv ${TMPDIR}/csv3.gpx
gpsbabel  -i gpx -o csv ${TMPDIR}/csv3.gpx ${TMPDIR}/csv4.csv
compare ${TMPDIR}/csv.csv ${TMPDIR}/csv4.csv 

#
# Delorme TopoUSA 4 is a CSV strain.  
#
rm -f ${TMPDIR}/xmap-1.gpx ${TMPDIR}/xmap-2.gpx ${TMPDIR}/xmap
gpsbabel -i xmap -f ${REFERENCE}/xmap -o xmap -F ${TMPDIR}/xmap
gpsbabel -i xmap -f ${REFERENCE}/xmap -o gpx -F ${TMPDIR}/xmap-1.gpx
gpsbabel -i xmap -f ${TMPDIR}/xmap -o gpx -F ${TMPDIR}/xmap-2.gpx
compare ${TMPDIR}/xmap-1.gpx ${TMPDIR}/xmap-2.gpx
compare ${REFERENCE}/xmap ${TMPDIR}/xmap

# PCX (Garmin mapsource import) file format
rm -f ${TMPDIR}/mm.pcx ${TMPDIR}/pcx.gps
gpsbabel -i gpx -f ${REFERENCE}/geocaching.gpx -o pcx -F ${TMPDIR}/mm.pcx
gpsbabel -i pcx -f ${TMPDIR}/mm.pcx -o gpsutil -F ${TMPDIR}/pcx.gps
compare ${TMPDIR}/mm.gps ${TMPDIR}/gu.wpt
gpsbabel -t -i gpx -f ${REFERENCE}/track/tracks.gpx -o pcx -F ${TMPDIR}/pcx.trk
gpsbabel -t -i pcx -f ${REFERENCE}/track/pcx.trk -o pcx -F ${TMPDIR}/pcx2.trk
compare ${TMPDIR}/pcx.trk ${TMPDIR}/pcx2.trk 
# GPSUtil strain - hand crafted, but based on problem report.
gpsbabel -i pcx -f ${REFERENCE}/gpsutil-1.pcx -o pcx -F ${TMPDIR}/mm-2.pcx
compare ${TMPDIR}/mm-2.pcx ${TMPDIR}/mm.pcx

#
# Magellan file format
#
gpsbabel -i magellan -f ${REFERENCE}/magfile -o magellan -F ${TMPDIR}/magfile
compare ${TMPDIR}/magfile ${REFERENCE}/magfile

#
# Magellanx is just like, but with longer names. (which this admittedly
# doesn't actually exercise...)
#
gpsbabel -i magellan -f ${REFERENCE}/magfile -o magellanx -F ${TMPDIR}/magfile2
compare ${TMPDIR}/magfile2 ${REFERENCE}/magfile

# Magellanx routes, however, have an extra 'name' field in them.
gpsbabel -r -i magellanx -f ${REFERENCE}/route/magexplorist.rte -o magellanx -F ${TMPDIR}/magxfile.rte
gpsbabel -r -i magellanx -f ${TMPDIR}/magxfile.rte -o magellanx -F ${TMPDIR}/magxfile2.rte
compare ${TMPDIR}/magxfile2.rte ${REFERENCE}/route/magexplorist.rte


# Navitrak DNA marker format
gpsbabel -i dna -f ${REFERENCE}/dnatest.txt -o dna -F ${TMPDIR}/dnatest.txt
compare ${TMPDIR}/dnatest.txt ${REFERENCE}/dnatest.txt

# PSP (PocketStreets 2002 Pushpin (.PSP)) file format. Use mxf as an 
# intermediate format to avoid binary FP anomalies on compareerent platforms.
rm -f ${TMPDIR}/psp.mxf ${TMPDIR}/mxf.psp
gpsbabel -i psp -f ${REFERENCE}/ps.psp -o mxf -F ${TMPDIR}/psp.mxf
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o mxf -F ${TMPDIR}/mxf.psp
compare ${TMPDIR}/psp.mxf ${TMPDIR}/mxf.psp
gpsbabel -i psp -f ${REFERENCE}/ps.psp -o gpx -F ${TMPDIR}/psp1.gpx
gpsbabel -i psp -f ${REFERENCE}/ps.psp -o psp -F ${TMPDIR}/xxx.psp
gpsbabel -i psp -f ${TMPDIR}/xxx.psp -o gpx -F ${TMPDIR}/psp2.gpx
compare ${TMPDIR}/psp1.gpx ${TMPDIR}/psp2.gpx

# MXF (Maptech Exchange Format) file format
rm -f ${TMPDIR}/mx.mxf ${TMPDIR}/mxf.mxf
gpsbabel -i mxf -f ${REFERENCE}/mxf.mxf -o mxf -F ${TMPDIR}/mx.mxf
gpsbabel -i mxf -f ${TMPDIR}/mx.mxf -o mxf -F ${TMPDIR}/mxf.mxf
compare ${TMPDIR}/mxf.mxf ${REFERENCE}

# tmpro (TopoMapPro Places) file format
rm -f ${TMPDIR}/topomappro.txt ${TMPDIR}/mxf.mxf
gpsbabel -i tmpro -f ${REFERENCE}/topomappro.txt -o tmpro -F ${TMPDIR}/tmp.txt
gpsbabel -i tmpro -f ${TMPDIR}/tmp.txt -o tmpro -F ${TMPDIR}/topomappro.txt
compare ${TMPDIR}/topomappro.txt ${REFERENCE}

# TPG (NG Topo!) file format
# This is hard to test as the datum conversions create minute
# inconsistencies in the coordinates.  So..  we test our i/o 
# against a format that rounds higher than we care to compare
# for binary data. 
rm -f ${TMPDIR}/topo.mxf ${TMPDIR}/tpg.mxf ${TMPDIR}/geo.tpg
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o tpg -F ${TMPDIR}/geo.tpg
gpsbabel -i tpg -f ${TMPDIR}/geo.tpg -o mxf -F ${TMPDIR}/tpg.mxf
gpsbabel -i tpg -f ${REFERENCE}/tpg.tpg -o mxf -F ${TMPDIR}/topo.mxf
compare ${TMPDIR}/tpg.mxf ${TMPDIR}/topo.mxf

# TPO (NG Topo!) file format
# This is hard to test as the datum conversions create minute
# inconsistencies in the coordinates. We have four reference files:
# tpo-sample1.tpo, tpo-sample1.gpx, tpo-sample2.gpx, and
# tpo-sample2.tpo.  These are used to check the conversion to/from
# TPO format.
#
# Version 2.x tests
rm -f ${TMPDIR}/tpo-sample1.gpx ${TMPDIR}/tpo-sample2.tpo
gpsbabel -t -i tpo2 -f ${REFERENCE}/track/tpo-sample1.tpo -o gpx -F ${TMPDIR}/tpo-sample1.gpx
compare ${TMPDIR}/tpo-sample1.gpx ${REFERENCE}/track/tpo-sample1.gpx
#gpsbabel -t -i gpx -f reference/track/tpo-sample2.gpx -o tpo2 -F ${TMPDIR}/tpo-sample2.tpo
#bincompare ${TMPDIR}/tpo-sample2.tpo reference/track/tpo-sample2.tpo
#
# Version 3.x tests.  Remove the timestamp from the generated file
# so that the compare will succeed.
rm -f ${TMPDIR}/tpo-sample3.gpx ${TMPDIR}/tpo-sample3.gpx2
gpsbabel -t -r -w -i tpo3 -f ${REFERENCE}/tpo-sample3.tpo -o gpx -F ${TMPDIR}/tpo-sample3.gpx
# Remove the timestamp
grep -v time <${TMPDIR}/tpo-sample3.gpx >${TMPDIR}/tpo-sample3.gpx2
compare ${TMPDIR}/tpo-sample3.gpx2 ${REFERENCE}/tpo-sample3.gpx

# OZI (OziExplorer 1.1) file format
rm -f ${TMPDIR}/oz.wpt ${TMPDIR}/ozi.wpt
gpsbabel -i ozi -f ${REFERENCE}/ozi.wpt -o ozi -F ${TMPDIR}/oz.wpt
gpsbabel -i ozi -f ${TMPDIR}/oz.wpt -o ozi -F ${TMPDIR}/ozi.wpt
compare ${TMPDIR}/ozi.wpt ${REFERENCE}

# Holux support is a little funky to test.  Becuase it loses precision,
# if we convert it to another format, we lose accuracy (rounding) in the
# coords, so converting it so something else and comparing it never works.
# So we verify that we can read the reference and write it and get an
# identical reference.
gpsbabel -i holux -f ${REFERENCE}/paris.wpo -o holux -F ${TMPDIR}/paris.wpo
# compare ${REFERENCE}/paris.wpo ${TMPDIR}/paris.wpo

# Magellan NAV Companion for PalmOS
# This format is hard to test, because each record and the database itself
# contains the time of creation, so two otherwise identical files won't
# compare accurately.  In any case, the files are binary so compare wouldn't
# like them.  So, we convert the reference file to gpsutil and the converted
# file to gpsutil and make sure they're the same, and that they're the same
# as one converted on a known-working installation.  Unfortunately, this does
# not verify that the appinfo block was written correctly.  However, it does
# successfully test for some endianness errors that might otherwise go 
# unnoticed.
rm -f ${TMPDIR}/magnav.pdb ${TMPDIR}/magnav.gpu ${TMPDIR}/magnavt.gpu
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o magnav -F ${TMPDIR}/magnav.pdb
gpsbabel -i magnav -f ${TMPDIR}/magnav.pdb -o gpsutil -F ${TMPDIR}/magnav.gpu
gpsbabel -i magnav -f ${REFERENCE}/magnav.pdb -o gpsutil -F ${TMPDIR}/magnavt.gpu
compare ${TMPDIR}/magnavt.gpu ${TMPDIR}/magnav.gpu
compare ${REFERENCE}/gu.wpt ${TMPDIR}/magnav.gpu

rm -f ${TMPDIR}/magnav.pdb
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o magnav -F ${TMPDIR}/magnav.pdb
bincompare ${TMPDIR}/magnav.pdb ${REFERENCE}/magnav.pdb



# GPSPilot Tracker for PalmOS
# This test is eerily similar to the NAV Companion test.  In fact, the 
# converted reference file (magnavr.gpu) is identical.
rm -f ${TMPDIR}/gpspilot.pdb ${TMPDIR}/gpspilot.gpu ${TMPDIR}/gpspil_t.gpu
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o gpspilot -F ${TMPDIR}/gpspilot.pdb
gpsbabel -i gpspilot -f ${TMPDIR}/gpspilot.pdb -o gpsutil -F ${TMPDIR}/gpspilot.gpu
gpsbabel -i gpspilot -f ${REFERENCE}/gpspilot.pdb -o gpsutil -F ${TMPDIR}/gpspil_t.gpu
compare ${TMPDIR}/gpspil_t.gpu ${TMPDIR}/gpspilot.gpu
compare ${REFERENCE}/gu.wpt ${TMPDIR}/gpspilot.gpu

# Cetus GPS for PalmOS
# This test is also similar to the NAV Companion test.
rm -f ${TMPDIR}/cetus.pdb ${TMPDIR}/cetus.gpu ${TMPDIR}/cetust.gpu
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o cetus -F ${TMPDIR}/cetus.pdb
gpsbabel -i cetus -f ${TMPDIR}/cetus.pdb -o gpsutil -F ${TMPDIR}/cetus.gpu
gpsbabel -i cetus -f ${REFERENCE}/cetus.pdb -o gpsutil -F ${TMPDIR}/cetust.gpu
compare ${TMPDIR}/cetust.gpu ${TMPDIR}/cetus.gpu
compare ${REFERENCE}/cetus.gpu ${TMPDIR}/cetus.gpu

# QuoVadis GPS for PalmOS
# This test is derived from the Cetus test above.
rm -f ${TMPDIR}/quovadis.pdb ${TMPDIR}/quovadis.gpu ${TMPDIR}/quovadist.gpu
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o quovadis -F ${TMPDIR}/quovadis.pdb
gpsbabel -i quovadis -f ${TMPDIR}/quovadis.pdb -o gpsutil -F ${TMPDIR}/quovadis.gpu
gpsbabel -i quovadis -f ${REFERENCE}/quovadis.pdb -o gpsutil -F ${TMPDIR}/quovadist.gpu
compare ${TMPDIR}/quovadist.gpu ${TMPDIR}/quovadis.gpu
compare ${REFERENCE}/quovadis.gpu ${TMPDIR}/quovadis.gpu

# GpsDrive
rm -f ${TMPDIR}/gpsdrive.txt
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o gpsdrive -F ${TMPDIR}/gpsdrive.txt
compare ${TMPDIR}/gpsdrive.txt ${REFERENCE}
gpsbabel -i gpsdrive -f ${REFERENCE}/gpsdrive.txt -o gpsdrive -F ${TMPDIR}/gpsdrive2.txt
compare ${TMPDIR}/gpsdrive2.txt ${REFERENCE}/gpsdrive.txt

# XMapHH Street Atlas USA file format
rm -f ${TMPDIR}/xmapwpt.wpt ${TMPDIR}/xmapwpt.xmapwpt
gpsbabel -i xmapwpt -f ${REFERENCE}/xmapwpt.wpt -o xmapwpt -F ${TMPDIR}/xmapwpt.xmapwpt
gpsbabel -i xmapwpt -f ${TMPDIR}/xmapwpt.xmapwpt -o xmapwpt -F ${TMPDIR}/xmapwpt.wpt
compare ${TMPDIR}/xmapwpt.wpt ${REFERENCE}

# XCSV
# Test that we can parse a style file, and read and write data in the 
# same xcsv format (a complete test is virtually impossible).
echo "RECORD_DELIMITER NEWLINE" > ${TMPDIR}/testo.style
echo "FIELD_DELIMITER COMMA" >> ${TMPDIR}/testo.style
echo "BADCHARS COMMA" >> ${TMPDIR}/testo.style
echo "PROLOGUE Header" >> ${TMPDIR}/testo.style
echo "EPILOGUE Footer" >> ${TMPDIR}/testo.style
echo "IFIELD SHORTNAME,,%s" >> ${TMPDIR}/testo.style
echo "IFIELD LAT_DIRDECIMAL,,%c%lf" >> ${TMPDIR}/testo.style
echo "IFIELD LON_DECIMALDIR,,%lf%c" >> ${TMPDIR}/testo.style
rm -f ${TMPDIR}/xcsv.geo ${TMPDIR}/xcsv.xcsv
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o xcsv,style=${TMPDIR}/testo.style -F ${TMPDIR}/xcsv.geo
gpsbabel -i xcsv,style=${TMPDIR}/testo.style -f ${TMPDIR}/xcsv.geo -o xcsv,style=${TMPDIR}/testo.style -F ${TMPDIR}/xcsv.xcsv
compare ${TMPDIR}/xcsv.geo ${TMPDIR}/xcsv.xcsv

# Garmin Mapsource This is a binary format with some undocumented
# fields.  This test is therefore intentionally vague.  We read a file,
# convert it to GPX, then write a file as MPS, then read it back and
# write it as GPX and compare them.  Since we're writing both GPX files
# ourselves from the same version, we're immune to changes in our own
# GPX output.

rm -fr ${TMPDIR}/ms.gpx ${TMPDIR}/ms[12].gpx
gpsbabel -i mapsource -f ${REFERENCE}/mapsource.mps  -o gpx -F ${TMPDIR}/ms1.gpx
gpsbabel -i mapsource -f ${REFERENCE}/mapsource.mps  -o mapsource -F ${TMPDIR}/ms.mps
gpsbabel -i mapsource -f ${TMPDIR}/ms.mps -o gpx -F ${TMPDIR}/ms2.gpx
compare ${TMPDIR}/ms1.gpx ${TMPDIR}/ms2.gpx

#
# MRCB mapsource track test
#
rm -f ${TMPDIR}/mps-track.mps
gpsbabel -t -i mapsource -f ${REFERENCE}/track/mps-track.mps -o mapsource,mpsverout=3 \
         -F ${TMPDIR}/mps-track.mps
compare ${TMPDIR}/mps-track.mps ${REFERENCE}/track

# Now do a test of reading waypoints from a track-only file - should have an empty result
rm -f ${TMPDIR}/mps-track.mps
gpsbabel -i mapsource -f ${REFERENCE}/track/mps-track.mps -o mapsource,mpsverout=3 \
         -F ${TMPDIR}/mps-track.mps
compare ${TMPDIR}/mps-track.mps ${REFERENCE}/mps-empty.mps

#
# MRCB mapsource route test
#
rm -f ${TMPDIR}/mps-route.mps
gpsbabel -r -i mapsource -f ${REFERENCE}/route/route.mps -o mapsource,mpsverout=4 \
         -F ${TMPDIR}/mps-route.mps
compare ${TMPDIR}/mps-route.mps ${REFERENCE}/route/route.mps

# Now do a test of reading tracks from a route-only file - should have an empty result
rm -f ${TMPDIR}/mps-route.mps
gpsbabel -t -i mapsource -f ${REFERENCE}/route/route.mps -o mapsource,mpsverout=3 \
         -F ${TMPDIR}/mps-route.mps
compare ${TMPDIR}/mps-route.mps ${REFERENCE}/mps-empty.mps

#
# Geocaching Database is a binary Palm format that, like the GPX variants
# has a zillion "equivalent" encodings of any given record set.  So we
# read the reference file, spin it to GPX and back to GCDB and then spin
# that one to GPX.
#

gpsbabel -i gcdb -f ${REFERENCE}/GeocachingDB.PDB -o gpx -F ${TMPDIR}/gcdb1.gpx \
		-o gcdb -F ${TMPDIR}/gcdb1.pdb
gpsbabel -i gpx -f ${TMPDIR}/gcdb1.gpx -o gpx -F ${TMPDIR}/gcdb2.gpx
compare ${TMPDIR}/gcdb1.gpx ${TMPDIR}/gcdb1.gpx

#
# Duplicate filter - Since filters have no format of their own, we use csv
# as an intermediate format for testing the filter.
#
rm -f ${TMPDIR}/filterdupe.csv1 ${TMPDIR}/filterdupe.csv2
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o csv -F ${TMPDIR}/filterdupe.csv1
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -f ${REFERENCE}/../geocaching.loc -x duplicate,shortname \
		-o csv -F ${TMPDIR}/filterdupe.csv2
sort_and_compare ${TMPDIR}/filterdupe.csv1 ${TMPDIR}/filterdupe.csv2

#
# Position filter -  Since very small distances are essentialy a duplicate 
# position filter, we can test very similarly to the duplicate filter.
#
rm -f ${TMPDIR}/filterpos.csv1 ${TMPDIR}/filterpos.csv2
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -o csv -F ${TMPDIR}/filterpos.csv1
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc -f ${REFERENCE}/../geocaching.loc -x position,distance=5f \
		-o csv -F ${TMPDIR}/filterpos.csv2
sort_and_compare ${TMPDIR}/filterpos.csv1 ${TMPDIR}/filterpos.csv2

#
# Radius filter
#
rm -f ${TMPDIR}/radius.csv
gpsbabel -i geo -f ${REFERENCE}/../geocaching.loc \
		-x radius,lat=35.9720,lon=-87.1347,distance=14.7 \
		-o csv -F ${TMPDIR}/radius.csv
compare ${TMPDIR}/radius.csv ${REFERENCE}

#
# magellan SD card waypoint / route format
#
rm -f ${TMPDIR}/magellan.rte
gpsbabel -r -i magellan -f ${REFERENCE}/route/magellan.rte -o magellan \
         -F ${TMPDIR}/magellan.rte
compare ${TMPDIR}/magellan.rte ${REFERENCE}/route/magellan.rte


#
# GPX routes -- since GPX contains a date stamp, tests will always
# fail, so we use magellan as an interim format...
#
rm -f ${TMPDIR}/gpxroute.gpx ${TMPDIR}/maggpx.rte
gpsbabel -r -i gpx -f ${REFERENCE}/route/route.gpx -o gpx \
         -F ${TMPDIR}/gpxroute.gpx
gpsbabel -r -i gpx -f ${TMPDIR}/gpxroute.gpx -o magellan \
         -F ${TMPDIR}/maggpx.rte
compare ${TMPDIR}/maggpx.rte ${REFERENCE}/route/magellan.rte

#
# GPX tracks -- since GPX contains a date stamp, tests will always
# fail, so we use magellan as an interim format...
#
rm -f ${TMPDIR}/gpxtrack.gpx ${TMPDIR}/maggpx.trk
gpsbabel -t -i gpx -f ${REFERENCE}/track/tracks.gpx -o gpx \
         -F ${TMPDIR}/gpxtrack.gpx
gpsbabel -t -i magellan -f ${REFERENCE}/track/meridian.trk -o gpx \
         -F ${TMPDIR}/maggpx.trk
compare ${TMPDIR}/maggpx.trk ${TMPDIR}/gpxtrack.gpx

#
# MAPSEND waypoint / route format
#
rm -f ${TMPDIR}/route.mapsend
gpsbabel -r -i mapsend -f ${REFERENCE}/route/route.mapsend -o mapsend \
         -F ${TMPDIR}/route.mapsend
bincompare ${TMPDIR}/route.mapsend ${REFERENCE}/route/route.mapsend

#
# MAPSEND track format 
#
rm -f ${TMPDIR}/mapsend.trk
gpsbabel -t -i mapsend -f ${REFERENCE}/track/mapsend.trk -o mapsend,trkver=3 \
         -F ${TMPDIR}/mapsend.trk
compare ${TMPDIR}/mapsend.trk ${REFERENCE}/track/

#
# copilot
#
rm -f ${TMPDIR}/copilot.pdb
gpsbabel -i copilot -f ${REFERENCE}/UKultralight.pdb -o copilot -F ${TMPDIR}/cop.pdb
gpsbabel -i copilot -f ${REFERENCE}/UKultralight.pdb -o gpx -F ${TMPDIR}/cop1.gpx
gpsbabel -i copilot -f ${TMPDIR}/cop.pdb -o gpx -F ${TMPDIR}/cop2.gpx
compare ${TMPDIR}/cop1.gpx ${TMPDIR}/cop2.gpx

#
# EasyGPS.   Another binary format.
#
rm -f ${TMPDIR}/easy.loc
gpsbabel -i easygps -f ${REFERENCE}/easygps.loc -o easygps -F ${TMPDIR}/ez.loc
gpsbabel -i easygps -f ${REFERENCE}/easygps.loc -o gpx -F ${TMPDIR}/ez1.gpx
gpsbabel -i easygps -f ${TMPDIR}/ez.loc -o gpx -F ${TMPDIR}/ez2.gpx
compare ${TMPDIR}/ez1.gpx ${TMPDIR}/ez2.gpx

