#
# Quick tests for Google Earth/KML format
# Note: Reference files are from GPSBabel's own output.
#
gpsbabel -i gpx -f ${REFERENCE}/gc/GC7FA4.gpx -f ${REFERENCE}/gc/GCGCA8.gpx -o kml -F ${TMPDIR}/ge-gc.kml
compare  ${REFERENCE}/earth-gc.kml ${TMPDIR}/ge-gc.kml
gpsbabel -i gpx -f ${REFERENCE}/expertgps.gpx -o kml -F ${TMPDIR}/ge-eg.kml
compare ${REFERENCE}/earth-expertgps.kml ${TMPDIR}/ge-eg.kml 

# And again, but with tracks.
gpsbabel -i gpx -f ${REFERENCE}/expertgps.gpx -o kml,track=1 \
  -F ${TMPDIR}/ge-eg-track.kml
compare ${REFERENCE}/earth-expertgps-track.kml ${TMPDIR}/ge-eg-track.kml

# Segmented input, with and without <Track>.
gpsbabel -i gpx -f ${REFERENCE}/track/segmented_tracks.gpx -o kml,track=0 \
  -F ${TMPDIR}/ge-seg.kml
compare ${REFERENCE}/track/segmented_tracks.kml ${TMPDIR}/ge-seg.kml

gpsbabel -i gpx -f ${REFERENCE}/track/segmented_tracks.gpx  -o kml,track=1 \
  -F ${TMPDIR}/ge-seg-track.kml
compare ${REFERENCE}/track/segmented_tracks-track.kml ${TMPDIR}/ge-seg-track.kml

# Fitness data.
gpsbabel -i gpx -f ${REFERENCE}/track/gpx_garmin_extensions.gpx  \
  -o kml,track=1 -F ${TMPDIR}/ge-fitness-track.kml
compare ${REFERENCE}/track/gpx_garmin_extensions-kml_track.kml ${TMPDIR}/ge-fitness-track.kml
# Fitness data with more extended data, specifically power.
gpsbabel -i gtrnctr -f ${REFERENCE}/track/gtrnctr_power.tcx \
  -o kml,track=1,points=0,lines=0 -F ${TMPDIR}/ge-power.kml
compare ${REFERENCE}/track/gtrnctr_power-kml.kml ${TMPDIR}/ge-power.kml

# test searches for route and waypoint min and max altitudes
gpsbabel -i gpx -f ${REFERENCE}/bounds-test.gpx -o kml -F ${TMPDIR}/bnds.kml
compare ${REFERENCE}/bounds-test.kml ${TMPDIR}/bnds.kml
gpsbabel -i gpx -f ${REFERENCE}/track/bounds-test-track.gpx -o kml -F ${TMPDIR}/bnds-track.kml
compare ${REFERENCE}/track/bounds-test-track.kml ${TMPDIR}/bnds-track.kml


set -e
if which xmllint > /dev/null;
then
  KMLS=$(find ${REFERENCE} -name \*.kml)
  for f in $KMLS
  do
    case $f in 
      # Argue with the KML wizards about this.  It was the intent for that
      # to be legal in Earth GX 2.2.
      ./reference/track/segmented_tracks-track.kml) continue;;
    *)
      tmpf=${TMPDIR}/$(basename $f)
      gpsbabel -i kml -f $f -o kml -F $tmpf
      xmllint --schema tools/kml22-schema/kml22gx.xsd --noout $tmpf
    esac;
  done
else
  echo "SKipping KML validation phase."
fi
