#!/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
#	 bulkmode.YYYYMMDD.  This is useful when something is different.
#
#	 ./inputs  - test inputs
#	 ./outputs - known correct outputs
#		     esp. bulkmode.out
#
#	 ./bulkmode.YYYYMMDD:
#		directory containing individual output files

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

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

if [ -d "$BOGOFILTER_DIR" ] ; then rm -f "$BOGOFILTER_DIR"/* ; else : ; fi

mkdir -p "$BOGOFILTER_DIR"
$BOGOFILTER -C -s < "$SYSTEST"/inputs/spam.mbx
$BOGOFILTER -C -n < "$SYSTEST"/inputs/good.mbx

OPT="-t -v -D"
pattern="$SYSTEST/inputs/msg.?.txt"

CFG="$TMPDIR/test.cf"

# Fisher tristate

cat <<EOF > "$CFG"
robx=0.415
min_dev=0.1
ham_cutoff=0.1
spam_cutoff=0.95
terse_format = %1.1c %d
spamicity_tags = S,h,u
spamicity_formats = %6.2e %6.2e %0.6f
block_on_subnets=yes
replace_nonascii_characters=yes
EOF

#
# run tests for msg.[1-8].txt
#

BOGOFILTER="$BOGOFILTER $OPT"

# test scoring of individual files

NAME="bulk-single"
( for f in $pattern ; do echo $f `$BOGOFILTER -c "$CFG" -e -I $f` ; done ) | \
    sed s@.*inputs/@./inputs/@ > "$TMPDIR"/$NAME.out

# test scoring of files listed on stdin

NAME="bulk-stdin"
ls $pattern | $BOGOFILTER -c "$CFG" -b | \
    sed s@.*inputs/@./inputs/@ > "$TMPDIR"/$NAME.out

# test scoring of files listed on linend

NAME="bulk-linend"
$BOGOFILTER -c "$CFG" -B `ls $pattern` | \
    sed s@.*inputs/@./inputs/@ > "$TMPDIR"/$NAME.out

# test scoring each file twice (using linend)

NAME="bulk-double-1"
for f in $pattern ; do 
    map_rc "$BOGOFILTER -c \"$CFG\" -B $f $f" | \
	sed s@.*inputs/@./inputs/@ >> "$TMPDIR"/$NAME.tmp
done
sort -u < "$TMPDIR"/$NAME.tmp > "$TMPDIR"/$NAME.out

# test scoring each file twice (using linend), when files are without "From " lines

NAME="bulk-double-2"
for f in $pattern ; do
    t="$TMPDIR/$(basename "$f")"
    $GREP -v "^From " <"$f" > "$t"
    map_rc "$BOGOFILTER -c \"$CFG\" -B \"$t\" \"$t\"" | \
	sed 's@.*/@./inputs/@' >> "$TMPDIR"/$NAME.tmp
done
sort -u <"$TMPDIR"/$NAME.tmp > "$TMPDIR"/$NAME.out

# test scoring of individual files using msg-count format

NAME="bogolex-single"
for f in $pattern ; do 
    map_rc "$BOGOTUNE -c \"$CFG\" -M \"$f\" | $BOGOFILTER -c \"$CFG\" >>\"$TMPDIR/$NAME.out\""
done

# test scoring of mbox batch of msg-count messages

NAME="bogolex-mbox"
cat $pattern > "$TMPDIR"/test.fr

map_rc "$BOGOFILTER -c \"$CFG\" -M" < "$TMPDIR"/test.fr > "$TMPDIR"/$NAME.out

NAME="bogolex-batch"
cat /dev/null > "$TMPDIR"/test.bl
for f in $pattern ; do 
    map_rc "$BOGOTUNE -c \"$CFG\" -M $f" >> "$TMPDIR/test.bl"
done

map_rc "$BOGOFILTER -c \"$CFG\" -M" < "$TMPDIR"/test.bl > "$TMPDIR"/$NAME.out

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

CORRECT="$OUTPUTS/bulkmode.out"

for OUT in "$TMPDIR"/bulk-*.out ; do
    if  [ $verbose -eq 0 ]; then
	cmp "$CORRECT" "$OUT"
    else
	diff $DIFF_BRIEF "$CORRECT" "$OUT"
    fi
done

CORRECT="$OUTPUTS/bogolex.out"

for OUT in "$TMPDIR"/bogolex-*.out ; do
    if  [ $verbose -eq 0 ]; then
	cmp "$CORRECT" "$OUT"
    else
	diff $DIFF_BRIEF "$CORRECT" "$OUT"
    fi
done
