#! /bin/sh

set -e

# Note:  When run via "make check", test output files are automatically deleted.
#	 When run from the command line, outputs files are left in directory
#	 lexer.mbx.YYYYMMDD.  This is useful when something is different.
#
#	 ./inputs  - test inputs
#	 ./outputs - known correct outputs
#
#	 lexer.mbx.YYYYMMDD:
#		tests   - directory containing individual output files

: ${srcdir=.}
relpath="`pwd`/../.."

NODB=1 . ${srcdir}/../t.frame

OUT="lexer.mbx.out"

verbose=0
if [ "$1" = "-v" ]; then
    verbose=1
    v="-v"
fi

if [ "$1" = "-vv" ]; then
    verbose=1
    v="-v -x l"
fi

BOGOLEXER="$VAL ${relpath}/bogolexer"
for f in good spam ; do
    $BOGOLEXER -C -D -p $v < ${srcdir}/inputs/$f.mbx > $TMPDIR/$f.1
    sort < $TMPDIR/$f.1 | uniq > $TMPDIR/$f.2
    if  [ $verbose -ne 0 ]; then
	$BOGOLEXER -C -D -x ml -vvv -p < ${srcdir}/inputs/$f.mbx > $TMPDIR/$f.v
    fi
done

RESULT=`cat $TMPDIR/spam.2 | wc -l`.`cat $TMPDIR/good.2 | wc -l`
RESULT=`echo $RESULT | sed s@\ @@g`

#strict checking
WANT="1102.3073"
#loose checking
WANT="1103.3073"
# v0.12.4 - parsing adds '!' and money amounts
WANT="1126.3080"
# v0.13.0 - new parsing rules - case sensitive, header_line_markup, tokenize_html_tags
WANT="1690.3918"
# v0.13.5 - new parsing rules - allow terminal exclamation points
WANT="1724.3937"
# v0.13.6.2 - lexer_v3.l changes to exclude brackets
WANT="1723.3931"
# v0.13.7.3 - lexer_v3.l changes to allow space in mime boundary
WANT="1722.3929"
# v0.15 - removed html.c - process_html_comments()
WANT="1720.3925"
# v0.15.1 - unfolding header lines ...
WANT="1720.3927"
# v0.15.1 - discarding message separators
WANT="1710.3914"
# v0.15.1 - discarding additional message ids
WANT="1708.3909"

if [ "$RESULT" != "$WANT" -o $verbose -ne 0 ] ; then
    echo want: $WANT, have: $RESULT | tee $TMPDIR/$OUT
fi

[ ! -z "$BF_SAVEDIR" ] && . ${srcdir}/../t.save

test "$RESULT" = "$WANT"
