#! /bin/sh

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

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

CFG="$TMPDIR"/test.cf

OPTS="-y 0"

cat <<EOF > "$CFG"
robx=0.415
min_dev=0.0
ham_cutoff=0.10
spam_cutoff=0.95
spam_header_name=X-Bogosity
stats_in_header=Y
EOF

map_rc()
{
    (
	set +e
	"$@"
	a=$?
	[ $a -eq 0 ] && exit 0
	[ $a -eq 1 ] && exit 0
	[ $a -eq 2 ] && exit 0
	exit $a
    )
}

# Expected spamicity values (11/17/02 13:13):
#
#	1 g v		X-Bogosity: Ham,  spamicity=0.000000
#	1 r v		X-Bogosity: Ham,  spamicity=0.402296
#	2a g vv		X-Bogosity: Spam, spamicity=1.000000
#	2a r vv		X-Bogosity: Ham,  spamicity=0.527095
#	2b g vv		X-Bogosity: Spam, spamicity=1.000000
#	2b g vvv	X-Bogosity: Spam, spamicity=1.000000
#	2b r vv		X-Bogosity: Spam, spamicity=0.540124
#	2b r vvv	X-Bogosity: Spam, spamicity=0.540124
#	3 g vvv		X-Bogosity: Spam, spamicity=1.000000
#	3 r vvv		X-Bogosity: Spam, spamicity=0.540316

BOGOFILTER_DIR="$TMPDIR"
export BOGOFILTER_DIR
$BOGOFILTER $OPTS -c "$CFG" -s < "$SYSTEST"/inputs/spam.mbx
$BOGOFILTER $OPTS -c "$CFG" -n < "$SYSTEST"/inputs/good.mbx
for f in "$BOGOFILTER_DIR"/*.$DB_EXT ; do
    n="$(basename "$f" ".$DB_EXT")"
    $BOGOUTIL -C -d "$BOGOFILTER_DIR/$n.$DB_EXT" > "$BOGOFILTER_DIR/$n.txt"
done
if  [ $verbose -ne 0 ]; then
    ls -l "$BOGOFILTER_DIR"/*list.txt
fi
#
# run tests for msg.1.txt, msg.2.txt, and msg.3.txt
#
for tst in 1.1.v.yes 2.2a.vv.yes 2.2a.vvv.no 3.3.vvv.no ; do
    in=`echo $tst | tr "." " " | $AWK '{print $1}'`
    out=`echo $tst | tr "." " " | $AWK '{print $2}'`
    ver=`echo $tst | tr "." " " | $AWK '{print $3}'`
    sih=`echo $tst | tr "." " " | $AWK '{print $4}'`
    map_rc $BOGOFILTER $OPTS -c "$CFG" --stats-in-header=$sih -$ver 2> /dev/null \
    < "$SYSTEST"/inputs/msg.$in.txt > "$TMPDIR"/out || :
    sed s/,.version=.*// < "$TMPDIR"/out > "$TMPDIR"/msg.$out.$ver
done
#
# classify msg.2.txt a second time and rerun tests
#    (expect different results from first run)
#
$BOGOFILTER $OPTS -c "$CFG" -s < "$SYSTEST"/inputs/msg.3.txt
for tst in 2.2b.vv.yes 2.2b.vvv.no ; do
    in=`echo $tst | tr "." " " | $AWK '{print $1}'`
    out=`echo $tst | tr "." " " | $AWK '{print $2}'`
    ver=`echo $tst | tr "." " " | $AWK '{print $3}'`
    sih=`echo $tst | tr "." " " | $AWK '{print $4}'`
    map_rc $BOGOFILTER $OPTS -c "$CFG" --stats-in-header=$sih -$ver 2> /dev/null \
    < "$SYSTEST"/inputs/msg.$in.txt > "$TMPDIR"/out
    sed s/,.version=.*// < "$TMPDIR"/out > "$TMPDIR"/msg.$out.$ver
done
if  [ $verbose -eq 0 ]; then
    rm -f "$BOGOFILTER_DIR"/*.$DB_EXT
fi

rm -f "$CFG"

if [ "$BF_SAVEDIR" ] ; then . "$srcdir/t.save" ; fi

for i in "$OUTPUTS"/msg.*.v* ; do
    j="$(basename "$i")"
    if  [ $verbose -eq 0 ]; then
	cmp "$i" "$TMPDIR/$j"
    else
	set +e
	diff $DIFF_BRIEF "$i" "$TMPDIR"/$j
    fi
done
