#!/usr/bin/env sh

GPSBABEL_FREEZE_TIME=y
export GPSBABEL_FREEZE_TIME

# Turn on GNU libc instrumentation.
MALLOC_CHECK_=2
export MALLOC_CHECK_

# Testcases are written for English/US locale (by default)
LC_ALL=en_US.UTF-8
export LC_ALL

while :
do
  case "$1" in
  -p) PNAME=$2; shift 2;;
  -v) VALGRIND=$2; shift 2;;
  -d) DEBUG=1; shift 1;;
  *) break;;
  esac
done

BASEPATH=`dirname $0`
PNAME=${PNAME:-${BASEPATH}/gpsbabel}
DIFF=${DIFF:-diff}
REFERENCE=${BASEPATH}/reference
# OD=${OD:-od -Ax -txC -v}
if [ -x /usr/bin/hexdump ] ; then
	OD=${OD:-hexdump -v -C}
else
	OD=${OD:-od -Ax -txC -v}
fi
XMLWF=xmlwf

TMPDIR=${GBTEMP:-/tmp}/gpsbabel.$$
mkdir -p $TMPDIR
if test -z  "$DEBUG"; then
  trap "rm -fr $TMPDIR" 0 1 2 3 15
else
	echo "Preserving tempdir TMPDIR because DEBUG is set"
fi

bincompare()
{
	rm -f ${TMPDIR}/bc1
		rm -f ${TMPDIR}/bc2
		${OD} $1 >${TMPDIR}/bc1
		${OD} $2 >${TMPDIR}/bc2
		${DIFF} ${TMPDIR}/bc1 ${TMPDIR}/bc2 || {
		echo ERROR binary comparing $*
		errorcount=`expr $errorcount + 1`
		#exit 1
	}
}

compare()
{
	${DIFF} -u -b -w $* ||  {
		echo ERROR comparing $*
		errorcount=`expr $errorcount + 1`
		#exit 1
	}
}

compare_nole()
{
	${DIFF} -u --strip-trailing-cr $* ||  {
		echo ERROR comparing $*
		errorcount=`expr $errorcount + 1`
		#exit 1
	}
}

compare_with_alternate()
{
	${DIFF} -u -b -w $1 $3 1>${TMPDIR}/compare.log ||  {
		${DIFF} -u -b -w $2 $3 || {
			echo ERROR comparing with alternate $2 to $3
			cat ${TMPDIR}/compare.log
			rm -f ${TMPDIR}/compare.log
			echo ERROR comparing with alternate $1 to $3
			errorcount=`expr $errorcount + 1`
			#exit 1
		}
	}
}

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

gpsbabel()
{
	${VALGRIND} "${PNAME}" "$@" || {
 	echo "$PNAME returned error $?"
 	echo "($PNAME $@)"
 	errorcount=`expr $errorcount + 1`
 	#exit 1
 }
}

utf8bomcheck()
{
  if [ -z "${VALGRIND}" ]; then
    if [ "$(dd if=$1 bs=1 count=3 2>/dev/null)" = $'\xef\xbb\xbf' ]; then
      echo "ERROR: UTF-8 BOM found in $1"
      errorcount=`expr $errorcount + 1`
    fi
  fi
}

xmlwfcheck()
{
  if [ -z "${VALGRIND}" ]; then
    if command -v ${XMLWF} >/dev/null 2>&1; then
      # xmlwf is a bit lame, exit status is always 0
      rm -f ${TMPDIR}/xmlwf.out
      ${XMLWF} $1 2>&1 | tee ${TMPDIR}/xmlwf.out
      if [ -s ${TMPDIR}/xmlwf.out ]; then
        echo "ERROR: xml is not well-formed in $1"
        errorcount=`expr $errorcount + 1`
      fi
    fi
  fi
}

# Some formats are just too boring to test.   The ones that
# are xcsv include
# garmin301
# garmin_poi
# gpsdrivetrack
# nima
# mapconverter
# geonet
# saplus
# s_and_t
# xmap2006
# cambridge
# cup

errorcount=0

if [ $# -ge 1 ]; then
  while [ $# -ge 1 ];
  do
    t=${BASEPATH}/testo.d/$1.test
    echo Running `basename $t`
    . $t
    shift;
  done
else
  for i in ${BASEPATH}/testo.d/*.test
  do
    echo Running `basename $i`
    . $i
  done
fi

# Multiple tests produce these files in addition to gpx.test and kml.test,
# e.g. tpo.test has produced problematic gpx and kml files.
# For this reason this check isn't incorporated into each test.

# reference/basecamp.gpx has a UTF-8 BOM which is useful for verifying this test.
#cp reference/basecamp.gpx ${TMPDIR}
#cp LineStyles.gpx.badchar ${TMPDIR}/bad.gpx
XMLS=$(find ${TMPDIR} -name "*.gpx" -o -name "*.kml")

if [ -z "${VALGRIND}" ]; then
  echo "Running UTF-8 BOM test"
  for i in ${XMLS}
  do
    utf8bomcheck $i
  done
fi

if [ -z "${VALGRIND}" ]; then
  if command -v ${XMLWF} >/dev/null 2>&1; then
    echo "Running well-formed XML test"
    for i in ${XMLS}
    do
      xmlwfcheck $i
    done
  else
    echo "Skipping well-formed XML test"
  fi
fi

exit $errorcount
