GPSBABEL_FREEZE_TIME=y
export GPSBABEL_FREEZE_TIME

PNAME=${PNAME:-./gpsbabel}
DIFF=${DIFF:-diff}
OD=${OD:-od -Ax -txC -v}

TMPDIR=/tmp/gpsbabel.$$
mkdir -p $TMPDIR
trap "rm -fr $TMPDIR" 0 1 2 3 15

bincompare()
{
	rm -f ${TMPDIR}/bc1
        rm -f ${TMPDIR}/bc2
        ${OD} $1 >${TMPDIR}/bc1 
        ${OD} $2 >${TMPDIR}/bc2
        ${DIFF} -u ${TMPDIR}/bc1 ${TMPDIR}/bc2 || {
		echo ERROR binary comparing $*
                exit 1
	}
}

compare()
{
	${DIFF} $* ||  {
		echo ERROR comparing $*
		exit 1
	} 
}

sort_and_compare()
{
	sort $1 > $TMPDIR/s1
	sort $2 > $TMPDIR/s2
	compare $TMPDIR/s1 $TMPDIR/s2
}


# Geocaching .loc
rm -f ${TMPDIR}/gl.loc
${PNAME} -i geo -f 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
${PNAME} -i geo -f geocaching.loc -o gpsutil -F ${TMPDIR}/gu.wpt
compare ${TMPDIR}/gu.wpt reference
${PNAME} -i gpsutil -f ${TMPDIR}/gu.wpt -o gpx -F ${TMPDIR}/1.gpx
${PNAME} -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+
${PNAME} -i geo -f geocaching.loc -o gpsman -F ${TMPDIR}/gm.gm
${PNAME} -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
${PNAME} -i geo -f geocaching.loc -o gpx -F ${TMPDIR}/gl.gpx
${PNAME} -i gpx -f ${TMPDIR}/gl.gpx -o gpsutil -F ${TMPDIR}/gpx.gpx
compare ${TMPDIR}/gpx.gpx ${TMPDIR}/gu.wpt

# Magellan Mapsend
rm -f ${TMPDIR}/mm.mapsend ${TMPDIR}/mm.gps
${PNAME} -i geo -f geocaching.loc -o mapsend -F ${TMPDIR}/mm.mapsend
${PNAME} -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
${PNAME} -i geo -f geocaching.loc -o tiger -F ${TMPDIR}/tiger
${PNAME} -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
${PNAME} -i geo -f geocaching.loc -o lowranceusr -F ${TMPDIR}/lowrance1.usr
bincompare ${TMPDIR}/lowrance1.usr reference/lowrance.usr
${PNAME} -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
${PNAME} -i lowranceusr -f reference/all.usr -o gpx -F ${TMPDIR}/enchilada.gpx
${PNAME} -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
${PNAME} -i lowranceusr,ignoreicons -f reference/all.usr -o gpx -F ${TMPDIR}/enchilada.gpx
${PNAME} -i gpx -f ${TMPDIR}/enchilada.gpx -o lowranceusr -F ${TMPDIR}/enchilada1.usr
bincompare ${TMPDIR}/enchilada1.usr reference/ignoreicons.usr

# CSV (Comma separated value) data.

${PNAME}  -i geo -f geocaching.loc -o csv -F ${TMPDIR}/csv.csv
${PNAME}  -i csv -f ${TMPDIR}/csv.csv -o csv -F ${TMPDIR}/csv2.csv
compare ${TMPDIR}/csv2.csv ${TMPDIR}/csv.csv 

#
# Delorme TopoUSA 4 is a CSV strain.  
#
rm -f ${TMPDIR}/xmap-1.gpx ${TMPDIR}/xmap-2.gpx ${TMPDIR}/xmap
${PNAME} -i xmap -f reference/xmap -o xmap -F ${TMPDIR}/xmap
${PNAME} -i xmap -f reference/xmap -o gpx -F ${TMPDIR}/xmap-1.gpx
${PNAME} -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
${PNAME} -i geo -f geocaching.loc -o pcx -F ${TMPDIR}/mm.pcx
${PNAME} -i pcx -f ${TMPDIR}/mm.pcx -o gpsutil -F ${TMPDIR}/pcx.gps
compare ${TMPDIR}/mm.gps ${TMPDIR}/gu.wpt

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

# Navitrak DNA marker format
${PNAME} -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
${PNAME} -i psp -f reference/ps.psp -o mxf -F ${TMPDIR}/psp.mxf
${PNAME} -i geo -f geocaching.loc -o mxf -F ${TMPDIR}/mxf.psp
compare ${TMPDIR}/psp.mxf ${TMPDIR}/mxf.psp
${PNAME} -i psp -f reference/ps.psp -o gpx -F ${TMPDIR}/psp1.gpx
${PNAME} -i psp -f reference/ps.psp -o psp -F ${TMPDIR}/xxx.psp
${PNAME} -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
${PNAME} -i mxf -f reference/mxf.mxf -o mxf -F ${TMPDIR}/mx.mxf
${PNAME} -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
${PNAME} -i tmpro -f reference/topomappro.txt -o tmpro -F ${TMPDIR}/tmp.txt
${PNAME} -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
${PNAME} -i geo -f geocaching.loc -o tpg -F ${TMPDIR}/geo.tpg
${PNAME} -i tpg -f ${TMPDIR}/geo.tpg -o mxf -F ${TMPDIR}/tpg.mxf
${PNAME} -i tpg -f reference/tpg.tpg -o mxf -F ${TMPDIR}/topo.mxf
compare ${TMPDIR}/tpg.mxf ${TMPDIR}/topo.mxf

# OZI (OziExplorer 1.1) file format
rm -f ${TMPDIR}/oz.wpt ${TMPDIR}/ozi.wpt
${PNAME} -i ozi -f reference/ozi.wpt -o ozi -F ${TMPDIR}/oz.wpt
${PNAME} -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.
${PNAME} -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
${PNAME} -i geo -f geocaching.loc -o magnav -F ${TMPDIR}/magnav.pdb
${PNAME} -i magnav -f ${TMPDIR}/magnav.pdb -o gpsutil -F ${TMPDIR}/magnav.gpu
${PNAME} -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
${PNAME} -i geo -f 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
${PNAME} -i geo -f geocaching.loc -o gpspilot -F ${TMPDIR}/gpspilot.pdb
${PNAME} -i gpspilot -f ${TMPDIR}/gpspilot.pdb -o gpsutil -F ${TMPDIR}/gpspilot.gpu
${PNAME} -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
${PNAME} -i geo -f geocaching.loc -o cetus -F ${TMPDIR}/cetus.pdb
${PNAME} -i cetus -f ${TMPDIR}/cetus.pdb -o gpsutil -F ${TMPDIR}/cetus.gpu
${PNAME} -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
${PNAME} -i geo -f geocaching.loc -o quovadis -F ${TMPDIR}/quovadis.pdb
${PNAME} -i quovadis -f ${TMPDIR}/quovadis.pdb -o gpsutil -F ${TMPDIR}/quovadis.gpu
${PNAME} -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
${PNAME} -i geo -f geocaching.loc -o gpsdrive -F ${TMPDIR}/gpsdrive.txt
compare ${TMPDIR}/gpsdrive.txt reference
${PNAME} -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
${PNAME} -i xmapwpt -f reference/xmapwpt.wpt -o xmapwpt -F ${TMPDIR}/xmapwpt.xmapwpt
${PNAME} -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
${PNAME} -i geo -f geocaching.loc -o xcsv,style=${TMPDIR}/testo.style -F ${TMPDIR}/xcsv.geo
${PNAME} -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
${PNAME} -i mapsource -f reference/mapsource.mps  -o gpx -F ${TMPDIR}/ms1.gpx
${PNAME} -i mapsource -f reference/mapsource.mps  -o mapsource -F ${TMPDIR}/ms.mps
${PNAME} -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
${PNAME} -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
${PNAME} -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
${PNAME} -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
${PNAME} -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.
#

${PNAME} -i gcdb -f reference/GeocachingDB.PDB -o gpx -F ${TMPDIR}/gcdb1.gpx \
		-o gcdb -F ${TMPDIR}/gcdb1.pdb
${PNAME} -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
${PNAME} -i geo -f geocaching.loc -o csv -F ${TMPDIR}/filterdupe.csv1
${PNAME} -i geo -f geocaching.loc -f 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
${PNAME} -i geo -f geocaching.loc -o csv -F ${TMPDIR}/filterpos.csv1
${PNAME} -i geo -f geocaching.loc -f 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
${PNAME} -i geo -f 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
${PNAME} -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
${PNAME} -r -i gpx -f reference/route/route.gpx -o gpx \
         -F ${TMPDIR}/gpxroute.gpx
${PNAME} -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
${PNAME} -t -i gpx -f reference/track/tracks.gpx -o gpx \
         -F ${TMPDIR}/gpxtrack.gpx
${PNAME} -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
${PNAME} -r -i mapsend -f reference/route/route.mapsend -o mapsend \
         -F ${TMPDIR}/route.mapsend
compare ${TMPDIR}/route.mapsend reference/route/

#
# MAPSEND track format 
#
rm -f ${TMPDIR}/mapsend.trk
${PNAME} -t -i mapsend -f reference/track/mapsend.trk -o mapsend \
         -F ${TMPDIR}/mapsend.trk
compare ${TMPDIR}/mapsend.trk reference/track/

#
# copilot
#
rm -f ${TMPDIR}/copilot.pdb
${PNAME} -i copilot -f reference/UKultralight.pdb -o copilot -F ${TMPDIR}/cop.pdb
${PNAME} -i copilot -f reference/UKultralight.pdb -o gpx -F ${TMPDIR}/cop1.gpx
${PNAME} -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
${PNAME} -i easygps -f reference/easygps.loc -o easygps -F ${TMPDIR}/ez.loc
${PNAME} -i easygps -f reference/easygps.loc -o gpx -F ${TMPDIR}/ez1.gpx
${PNAME} -i easygps -f ${TMPDIR}/ez.loc -o gpx -F ${TMPDIR}/ez2.gpx
compare ${TMPDIR}/ez1.gpx ${TMPDIR}/ez2.gpx

#
# GPilotS.   A Palm format.  Another binary format that 
#
# rm -f ${TMPDIR/gpilots.l
#${PNAME} -i easygps -f reference/gpilots.pdb -o gpx -F ${TMPDIR}/gp.gpx
${PNAME} -i geo -f geocaching.loc -o gpilots -F ${TMPDIR}/blah.pdb
${PNAME} -i gpilots -f ${TMPDIR}/blah.pdb -o gpx -F ${TMPDIR}/1.gpx
${PNAME} -i gpilots -f reference/gpilots.pdb -o gpx -F ${TMPDIR}/2.gpx
compare ${TMPDIR}/1.gpx ${TMPDIR}/2.gpx
#${PNAME} -i easygps -f reference/gpilots.pdb -o gpx -F ${TMPDIR}/gp.gpx

#
# Navicache.
${PNAME} -i navicache -f reference/navicache.xml -o gpsutil -F ${TMPDIR}/navi.wpt
compare ${TMPDIR}/navi.wpt reference/navicache.ref
#

#
# CoastalExplorer..
${PNAME} -r -i coastexp -f reference/coastexp.nob -o gpx -F ${TMPDIR}/coastexp.gpx
compare ${TMPDIR}/coastexp.gpx reference/coastexp.ref
${PNAME} -r -i gpx -f ${TMPDIR}/coastexp.gpx -o coastexp -F ${TMPDIR}/coastexp.nob
compare ${TMPDIR}/coastexp.nob reference/coastexp.ref2
${PNAME} -w -i coastexp -f reference/coastexp.nob -o gpx -F ${TMPDIR}/coastexp.gpx
compare ${TMPDIR}/coastexp.gpx reference/coastexp.ref3
${PNAME} -w -i gpx -f ${TMPDIR}/coastexp.gpx -o coastexp -F ${TMPDIR}/coastexp.nob
compare ${TMPDIR}/coastexp.nob reference/coastexp.ref4
#

# PsiTrex.  A text format that can't be handled by XCSV due to context of
# data based on other data values in the file
# Waypoints first
rm -f ${TMPDIR}/psit-ww.txt ${TMPDIR}/psit-ww.mps
${PNAME} -i psitrex -f reference/psitwpts.txt -o mapsource -F ${TMPDIR}/psit-ww.mps
${PNAME} -i mapsource -f ${TMPDIR}/psit-ww.mps -o psitrex -F ${TMPDIR}/psit-ww.txt
compare reference/psitwpts.txt ${TMPDIR}/psit-ww.txt

# Now test correct "empty" handling - ask for routes when there aren't any
# Uses mapsource as the empty handling for this has already happened above
rm -f ${TMPDIR}/psit-wr.mps
${PNAME} -r -i psitrex -f reference/psitwpts.txt -o mapsource,mpsverout=3 -F ${TMPDIR}/psit-wr.mps
compare reference/mps-empty.mps ${TMPDIR}/psit-wr.mps

# Routes next
rm -f ${TMPDIR}/psit-rr.txt ${TMPDIR}/psit-rr.mps
${PNAME} -r -i psitrex -f reference/route/psitrtes.txt -o mapsource -F ${TMPDIR}/psit-rr.mps
${PNAME} -r -i mapsource -f ${TMPDIR}/psit-rr.mps -o psitrex -F ${TMPDIR}/psit-rr.txt
compare reference/route/psitrtes.txt ${TMPDIR}/psit-rr.txt

# Now test correct "empty" handling - ask for tracks when there aren't any
# Uses mapsource as the empty handling for this has already happened above
rm -f ${TMPDIR}/psit-rt.mps
${PNAME} -t -i psitrex -f reference/route/psitrtes.txt -o mapsource,mpsverout=3 -F ${TMPDIR}/psit-rt.mps
compare reference/mps-empty.mps ${TMPDIR}/psit-rt.mps

# Tracks last
rm -f ${TMPDIR}/psit-tt.txt ${TMPDIR}/psit-tt.mps
${PNAME} -t -i psitrex -f reference/track/psittrks.txt -o mapsource -F ${TMPDIR}/psit-tt.mps
${PNAME} -t -i mapsource -f ${TMPDIR}/psit-tt.mps -o psitrex -F ${TMPDIR}/psit-tt.txt
compare reference/track/psittrks.txt ${TMPDIR}/psit-tt.txt

# Now test correct "empty" handling - ask for waypoints when there aren't any
# Uses mapsource as the empty handling for this has already happened above
rm -f ${TMPDIR}/psit-tw.mps
${PNAME} -i psitrex -f reference/track/psittrks.txt -o mapsource,mpsverout=3 -F ${TMPDIR}/psit-tw.mps
compare reference/mps-empty.mps ${TMPDIR}/psit-tw.mps

#
# Arc Distance filter
#
rm -f ${TMPDIR}/arcdist.txt
${PNAME} -i xmap -f reference/arcdist_input.txt \
         -x arc,file=reference/arcdist_arc.txt,distance=1 \
         -o xmap -F ${TMPDIR}/arcdist.txt
compare ${TMPDIR}/arcdist.txt reference/arcdist_output.txt

#
# Polygon filter
#
rm -f ${TMPDIR}/polygon.txt
${PNAME} -i xmap -f reference/arcdist_input.txt \
         -x polygon,file=reference/polygon_allencty.txt \
         -o xmap -F ${TMPDIR}/polygon.txt
compare ${TMPDIR}/polygon.txt reference/polygon_output.txt

#
# Simplify filter
#
rm -f ${TMPDIR}/simplify.txt
${PNAME} -r -i gpx -f reference/route/route.gpx \
         -x simplify,count=10 \
         -o arc -F ${TMPDIR}/simplify.txt
compare ${TMPDIR}/simplify.txt reference/simplify_output.txt

#
# Route reversal filter.   Do it twice and be sure we get what we
# started with.
#
rm -f ${TMPDIR}/reverse1.arc ${TMPDIR}/reverse2.arc ${TMPDIR}/reference.arc
${PNAME} -r -i gpx -f reference/route/route.gpx \
	  -o arc -F ${TMPDIR}/reference.arc
${PNAME} -r -i gpx -f reference/route/route.gpx \
         -x reverse \
         -o arc -F ${TMPDIR}/reverse1.arc
${PNAME} -r -i gpx -f reference/route/route.gpx \
         -x reverse \
         -x reverse \
         -o arc -F ${TMPDIR}/reverse2.arc
# Verify the first and last are the same
compare ${TMPDIR}/reference.arc  ${TMPDIR}/reverse2.arc
# Verify the first and second are different.
#${DIFF}  ${TMPDIR}/reverse1.arc  ${TMPDIR}/reverse2.arc > /dev/null && {
#		echo ERROR Failed reversal test.
#		exit 1
#}

# parkrrrr: This isn't a straightforward compare; we *want* it to fail.
# Obviously this test should just be rewritten with a new reference.
#compare  ${TMPDIR}/reverse1.arc  ${TMPDIR}/reverse2.arc

#
# Geoniche: No reference file was available, so we created one and just
# test it against itself.
#
rm -f ${TMPDIR}/gn.pdb ${TMPDIR}/1.gpx ${TMPDIR}/2.gpx
${PNAME} -i geoniche -f reference/geoniche.pdb -o geoniche -F ${TMPDIR}/gn.pdb
${PNAME} -i geoniche -f reference/geoniche.pdb -o gpx -F ${TMPDIR}/1.gpx
${PNAME} -i geoniche -f ${TMPDIR}/gn.pdb -o gpx -F ${TMPDIR}/2.gpx
compare ${TMPDIR}/1.gpx ${TMPDIR}/2.gpx

#
# saroute covers *.anr, *.rte, and *.rtd, but I only have an .anr for testing.
# Unfortunately for us, this is a read-only format for now.
#
${PNAME} -t -i saroute -f reference/track/i65.anr -o gpx -F ${TMPDIR}/gpl1.gpx
${PNAME} -t -i gpx -f reference/track/i65.anr.gpx -o gpx -F ${TMPDIR}/gpl2.gpx
compare ${TMPDIR}/gpl1.gpx ${TMPDIR}/gpl2.gpx

#
# Delorme GPL file.   This is sort of a track format.
#
rm -f ${TMPDIR}/gpl1.gpx ${TMPDIR}/gpl2.gpx ${TMPDIR}/gpl1.gpl
${PNAME} -t -i gpl -f reference/track/webpark1.gpl -o gpx -F ${TMPDIR}/gpl1.gpx
${PNAME} -t -i gpl -f reference/track/webpark1.gpl -o gpl -F ${TMPDIR}/gpl1.gpl
${PNAME} -t -i gpl -f ${TMPDIR}/gpl1.gpl -o gpx -F ${TMPDIR}/gpl2.gpx
compare ${TMPDIR}/gpl1.gpx ${TMPDIR}/gpl2.gpx

#
# NetStumbler Summary File (read-only)
#
rm -f ${TMPDIR}/netstumbler.mps
${PNAME} -i netstumbler -f reference/netstumbler.txt -o mapsource -F ${TMPDIR}/netstumbler.mps
bincompare ${TMPDIR}/netstumbler.mps reference/netstumbler.mps

#
# IGC tests
#
rm -f ${TMPDIR}/igc*out
${PNAME} -i gpx -f reference/igc1.gpx -o igc -F ${TMPDIR}/igc.out
sed '/^LXXXGenerated by GPSBabel Version/d' ${TMPDIR}/igc.out > ${TMPDIR}/igc_sed.out
compare ${TMPDIR}/igc_sed.out reference/igc1_igc.out

${PNAME} -i igc -f ${TMPDIR}/igc.out -o gpx -F ${TMPDIR}/igc.gpx
compare ${TMPDIR}/igc.gpx reference/igc1_gpx.out

${PNAME} -i gpx -f ${TMPDIR}/igc.gpx -o igc -F ${TMPDIR}/igc.out
sed '/^LXXXGenerated by GPSBabel Version/d' ${TMPDIR}/igc.out > ${TMPDIR}/igc_sed.out
compare ${TMPDIR}/igc_sed.out reference/igc1_igc.out

${PNAME} -i gpx -f reference/igc1_baro.gpx -i igc -f reference/igc1_igc.out -o igc,timeadj=auto -F ${TMPDIR}/igc.out
sed '/^LXXXGenerated by GPSBabel Version/d' ${TMPDIR}/igc.out > ${TMPDIR}/igc_sed.out
compare ${TMPDIR}/igc_sed.out reference/igc1_3d.out


${PNAME} -i igc -f reference/igc2.igc -o gpx -F ${TMPDIR}/igc.gpx
compare ${TMPDIR}/igc.gpx reference/igc2_gpx.out

${PNAME} -i gpx -f ${TMPDIR}/igc.gpx -o igc -F ${TMPDIR}/igc.out
sed '/^LXXXGenerated by GPSBabel Version/d' ${TMPDIR}/igc.out > ${TMPDIR}/igc_sed.out
compare ${TMPDIR}/igc_sed.out reference/igc2_igc.out

${PNAME} -i igc -f ${TMPDIR}/igc.out -o gpx -F ${TMPDIR}/igc.gpx
compare ${TMPDIR}/igc.gpx reference/igc2_gpx.out

#
# Google Maps XML test
#
rm -f ${TMPDIR}/google.out
${PNAME} -i google -f reference/google.xml -o arc -F ${TMPDIR}/google.out
compare ${TMPDIR}/google.out reference/google.arc

rm -f ${TMPDIR}/google.out
${PNAME} -i google -f reference/google.js -o arc -F ${TMPDIR}/google.out
compare ${TMPDIR}/google.out reference/google.arc

#
# DeLorme .an1 tests
#
rm -f ${TMPDIR}/an1.out
${PNAME} -i an1 -f reference/foo.an1 -o csv -F ${TMPDIR}/an1.out
compare ${TMPDIR}/an1.out reference/an1-in.ref

rm -f ${TMPDIR}/an1.out
${PNAME} -i an1 -f reference/foo.an1 -o an1 -F ${TMPDIR}/an1.out
compare ${TMPDIR}/an1.out reference/an1-an1.ref

rm -f ${TMPDIR}/an1.out
${PNAME} -i xmap -f reference/xmap -o an1 -F ${TMPDIR}/an1.out 
compare ${TMPDIR}/an1.out reference/an1-out.ref

rm -f ${TMPDIR}/an1.out
${PNAME} -i google -f reference/google.js -o an1 -F ${TMPDIR}/an1.out
compare ${TMPDIR}/an1.out reference/an1-line-out.ref

#
# TomTom .ov2 tests
#

rm -f ${TMPDIR}/ov2.out
${PNAME} -i arc -f reference/google.arc -o tomtom -F ${TMPDIR}/ov2.out
compare ${TMPDIR}/ov2.out reference/ov2-arc-out.ref

rm -f ${TMPDIR}/ov2.out
${PNAME} -i geo -f reference/gl.loc -o tomtom -F ${TMPDIR}/ov2.out
compare ${TMPDIR}/ov2.out reference/ov2-geo-out.ref

rm -f ${TMPDIR}/ov2.out
${PNAME} -i tomtom -f reference/ov2-geo-out.ref -o gpsutil -F ${TMPDIR}/ov2.out
compare ${TMPDIR}/ov2.out reference/ov2-in.ref

#
# XCSV "human readable" tests
#
rm -f ${TMPDIR}/humanread.out
${PNAME} -i xcsv,style=reference/humanread.style -f reference/human.in -o arc -F ${TMPDIR}/humanread.out
compare ${TMPDIR}/humanread.out reference/humanread.out

rm -f ${TMPDIR}/humanwrite.out
${PNAME} -i xcsv,style=reference/humanread.style -f reference/human.in -o xcsv,style=reference/humanwrite.style -F ${TMPDIR}/humanwrite.out
compare ${TMPDIR}/humanwrite.out reference/humanwrite.out

#
# XCSV "path distance" test
#
rm -f ${TMPDIR}/pathdist.out
${PNAME} -i magellan -f reference/dusky.trk -o xcsv,style=reference/gnuplot.style -F ${TMPDIR}/pathdist.out
compare ${TMPDIR}/pathdist.out reference/dusky.gnuplot

# hsandv
rm -f ${TMPDIR}/hsandv.exp ${TMPDIR}/1.exp ${TMPDIR}/1.exp ${TMPDIR}/Glad_5.exp
${PNAME} -i geo -f geocaching.loc -o hsandv -F ${TMPDIR}/hsandv.exp
compare ${TMPDIR}/hsandv.exp reference
#the hsandv format is too lossy to do this test :(
#${PNAME} -i hsandv -f ${TMPDIR}/hsandv.exp -o geo -F ${TMPDIR}/1.exp
#${PNAME} -i hsandv -f reference/hsandv.exp -o geo -F ${TMPDIR}/2.exp
#compare ${TMPDIR}/1.exp ${TMPDIR}/2.exp
#test conversion from v4 to v5 files
${PNAME} -i hsandv -f reference/Glad_4.exp -o hsandv -F ${TMPDIR}/Glad_5.exp
# FIXME: Can't compare directly because of potential FP rounding.
# FIXME: compare ${TMPDIR}/Glad_5.exp reference

#
# stack filter tests
# These don't actually test for proper behavior, for now, but they do 
# exercise all of the currently-extant filter code.
#

${PNAME} -i geo -f geocaching.loc -x stack,push,copy,nowarn -x stack,push,copy -x stack,push -x stack,pop,replace -x stack,pop,append -x stack,push,copy -x stack,pop,discard -x stack,swap,depth=1 -o arc -F ${TMPDIR}/stackfilt.txt

#
# 'tabsep' isn't really tested in any non-trivial way, but we do exercise
# it.
#

${PNAME} -i geo -f geocaching.loc  -o tabsep -F - | ${PNAME} -i tabsep -f - -o geo -F ${TMPDIR}/tabsep.out
${PNAME} -i geo -f geocaching.loc  -o geo -F ${TMPDIR}/geotabsep.out

# 
# Now do the same for custom - it has the same issues.
#

compare ${TMPDIR}/tabsep.out ${TMPDIR}/geotabsep.out
${PNAME} -i geo -f geocaching.loc  -o custom -F - | ${PNAME} -i custom -f - -o geo -F ${TMPDIR}/custom.out
${PNAME} -i geo -f geocaching.loc  -o geo -F ${TMPDIR}/geocustom.out

#
# Write something to the various output-only formats
#
${PNAME} -i geo -f geocaching.loc -o text -F ${TMPDIR}/text.out -o html -F ${TMPDIR}/html.out -o vcard -F ${TMPDIR}/vcard.out #-o palmdoc -F ${TMPDIR}/pd.out

#
# tef "TourExchangeFormat" read test
#
rm -f ${TMPDIR}/tef_xml*
${PNAME} -r -i tef -f reference/route/tef_xml.sample.xml -o gpx -F ${TMPDIR}/tef_xml.sample.gpx
compare reference/route/tef_xml.sample.gpx ${TMPDIR}/tef_xml.sample.gpx

#
# PathAway Palm Database .pdb tests
#
rm -f ${TMPDIR}/pathaway*
${PNAME} -i geo -f geocaching.loc -o pathaway,dbname="pathaway-geo" -F ${TMPDIR}/pathaway-geo.pdb
${PNAME} -i pathaway -f ${TMPDIR}/pathaway-geo.pdb -o geo -F ${TMPDIR}/pathaway-geo.loc
compare ${TMPDIR}/pathaway-geo.loc reference/pathaway-geo.loc
rm -f ${TMPDIR}/pathaway*
${PNAME} -t -i pathaway -f reference/track/pathaway.pdb -o gpx -F ${TMPDIR}/pathaway.gpx
compare ${TMPDIR}/pathaway.gpx reference/track/pathaway.gpx

#
# Garmin GPS Database .gdb tests
#
rm -f ${TMPDIR}/gdb-*
${PNAME} -w -r -t -i gdb -f reference/gdb-sample.gdb -o gpx -F ${TMPDIR}/gdb-sample.gpx
compare reference/gdb-sample.gpx ${TMPDIR}/gdb-sample.gpx
${PNAME} -w -r -t -i gpx -f reference/gdb-sample.gpx -o gdb,ver=1 -F ${TMPDIR}/gdb-sample.gdb
${PNAME} -w -r -t -i gdb -f ${TMPDIR}/gdb-sample.gdb -o gpx -F ${TMPDIR}/gdb-sample.gpx
#
# Because of Garmin coordinates storage gpx is not good for this test
# compare reference/gdb-sample.gpx ${TMPDIR}/gdb-sample.gpx
#

#
# Vito Navigator II .smt tests
#
rm -f ${TMPDIR}/vitosmt*
${PNAME}    -i vitosmt -f reference/vitosmt.smt -o gpx -F ${TMPDIR}/vitosmt.gpx
compare ${TMPDIR}/vitosmt.gpx reference/vitosmt.gpx
${PNAME} -t -i vitosmt -f reference/vitosmt.smt -o gpx -F ${TMPDIR}/vitosmt_t.gpx
compare ${TMPDIR}/vitosmt_t.gpx reference/track/vitosmt_t.gpx

#
# tracks filter tests
#

rm -f ${TMPDIR}/trackfilter*

${PNAME} -t -i gpx -f reference/track/trackfilter.gpx -x track,pack,split,title="LOG-%Y%m%d" -o gpx -F ${TMPDIR}/trackfilter-new.gpx
grep -v "<time>" reference/track/trackfilter-new.gpx > ${TMPDIR}/trackfilter.ref
grep -v "<time>" ${TMPDIR}/trackfilter-new.gpx > ${TMPDIR}/trackfilter.new
compare ${TMPDIR}/trackfilter.ref ${TMPDIR}/trackfilter.new

#
# Map&Guide Motorrad Routenplaner .bcr files test
#
rm -f ${TMPDIR}/bcr*
${PNAME} -r -i bcr -f reference/route/bcr-sample.bcr -o gpx -F ${TMPDIR}/bcr-sample.gpx
compare reference/route/bcr-sample.gpx ${TMPDIR}/bcr-sample.gpx
${PNAME} -r -i gpx -f reference/route/bcr-sample.gpx -o bcr -F ${TMPDIR}/bcr-sample2.bcr
compare reference/route/bcr-sample2.bcr ${TMPDIR}/bcr-sample2.bcr
${PNAME} -r -i bcr -f ${TMPDIR}/bcr-sample2.bcr -o gpx -F ${TMPDIR}/bcr-sample2.gpx
compare reference/route/bcr-sample.gpx ${TMPDIR}/bcr-sample2.gpx

#
# Garmin logbook.   This format has an extra section (lap data with things
# like heartbeat and calories burned) that we don't know what to do with,
# so we convert it to gpx, convert it to itself, convert THAT to gpx, and
# compare those.
#
rm -f ${TMPDIR}/glogbook*
${PNAME} -i glogbook -f reference/track/garmin_logbook.xml -o gpx -F ${TMPDIR}/glog1.gpx
${PNAME} -i glogbook -f reference/track/garmin_logbook.xml -o glogbook -F ${TMPDIR}/glog.xml
${PNAME} -i glogbook -f ${TMPDIR}/glog.xml -o gpx -F ${TMPDIR}/glog2.gpx
compare ${TMPDIR}/glog1.gpx ${TMPDIR}/glog2.gpx

#
# Dop filter test
#
rm -f ${TMPDIR}/dop*
sed '/<hdop>50/d' reference/dop-test.gpx | ${PNAME} -i gpx -f - -o openoffice -F - | sed 's/RPT...//g' > ${TMPDIR}/dop-hdop.ref
${PNAME} -i gpx -f reference/dop-test.gpx -x discard,hdop=50 -o openoffice -F - | sed 's/RPT...//g' > ${TMPDIR}/dop-hdop.fil
compare ${TMPDIR}/dop-hdop.ref ${TMPDIR}/dop-hdop.fil
sed '/<vdop>50/d' reference/dop-test.gpx | ${PNAME} -i gpx -f - -o openoffice -F - | sed 's/RPT...//g' > ${TMPDIR}/dop-vdop.ref
${PNAME} -i gpx -f reference/dop-test.gpx -x discard,vdop=50 -o openoffice -F - | sed 's/RPT...//g' > ${TMPDIR}/dop-vdop.fil
compare ${TMPDIR}/dop-vdop.ref ${TMPDIR}/dop-vdop.fil

#
# cotoGPS test
#
# rm -f ${TMPDIR}/coto*
# Track reading
# ${PNAME} -i coto -f reference/cototesttrack.pdb -o xcsv,style=reference/cototest.style -F ${TMPDIR}/cototrack.csv
# compare reference/cototesttrack.csv ${TMPDIR}/cototrack.csv
# Marker read/write
# ${PNAME} -i coto -f reference/cototestmarker.pdb -o xcsv,style=reference/cototest.style -F ${TMPDIR}/cotomarker.csv
# ${PNAME} -i gpx -f reference/cototestmarker.gpx -o xcsv,style=reference/cototest.style -F ${TMPDIR}/cotomarkergpx.csv
# compare ${TMPDIR}/cotomarker.csv ${TMPDIR}/cotomarkergpx.csv
# ${PNAME} -i gpx -f reference/cototestmarker.gpx -o coto -F ${TMPDIR}/cotomarker.pdb
# compare reference/cototestmarker.pdb ${TMPDIR}/cotomarker.pdb

#
# Fugawi test cases
rm -f ${TMPDIR}/fugawi*
${PNAME}  -i fugawi -f reference/fugawi.notime.txt -o fugawi -F ${TMPDIR}/fugawi1.txt
compare reference/fugawi.ref.txt ${TMPDIR}/fugawi1.txt
${PNAME}  -i geo -f geocaching.loc -o fugawi -F ${TMPDIR}/fugawi2.txt
compare reference/fugawi.ref.txt ${TMPDIR}/fugawi2.txt
${PNAME}  -i fugawi -f ${TMPDIR}/fugawi2.txt -o fugawi -F ${TMPDIR}/fugawi3.txt
compare ${TMPDIR}/fugawi2.txt ${TMPDIR}/fugawi3.txt
${PNAME}  -i fugawi -f reference/fugawi.time.txt  -o fugawi -F ${TMPDIR}/fugawi4.txt
compare reference/fugawi.time.ref.txt ${TMPDIR}/fugawi4.txt
${PNAME} -i gpx -f reference/track/tracks.gpx  -o fugawi -F ${TMPDIR}/fugawi5.txt
compare reference/track/fugawi.txt ${TMPDIR}/fugawi5.txt

exit 0
