#! /bin/sh

# t.regtest
#
#	test the registration options, i.e. '-s', '-n', '-S', and '-N'

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

relpath="`pwd`/.."

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

INPUT="$srcdir/inputs"

#opts="-x rw -vvvv"
OPTS="-C -y 0"

check_counts()
{
    if  [ $verbose -ne 0 ]; then
	echo "test #$T"
    fi

    if [ -f  "$BOGOFILTER_DIR"/goodlist.$DB_EXT ] ; then
	g=$("$BOGOUTIL" -C -d "$BOGOFILTER_DIR"/goodlist.$DB_EXT | grep -v " 0 " | tee "$TMPDIR"/good.$T.out | wc -l)
	s=$("$BOGOUTIL" -C -d "$BOGOFILTER_DIR"/spamlist.$DB_EXT | grep -v " 0 " | tee "$TMPDIR"/spam.$T.out | wc -l)
    else
	"$BOGOUTIL" -C -d "$BOGOFILTER_DIR"/wordlist.$DB_EXT > "$TMPDIR"/word.$T.out
	g=`grep -v " 0$" < "$TMPDIR"/word.$T.out | tee "$TMPDIR"/good.$T.out | wc -l`
	s=`grep -v " 0 " < "$TMPDIR"/word.$T.out | tee "$TMPDIR"/spam.$T.out | wc -l`
    fi
    WANT=`printf "%d.%d" $S $G`
    HAVE=`printf "%d.%d" $s $g`

    if  [ $verbose -ne 0 ]; then
	echo want: $WANT, have: $HAVE
    fi
    if [ ! "$WANT" = "$HAVE" ] ; then
	if [ $verbose -eq 0 ] ; then
	    exit 1
	fi
    fi
}

create_empty_wordlist

# populate goodlist and spamlist

T=1	# test #1
S=32	# expected tokens from spam message
G=36	# expected tokens from good message

"$BOGOFILTER" $OPTS -s -I "${INPUT}"/msg.regtest.s
"$BOGOFILTER" $OPTS -n -I "${INPUT}"/msg.regtest.n

check_counts

# move msg.regtest.n from goodlist to spamlist in two separate steps

T=2.1	# test #2.1
S=32	# expected tokens from spam message
G=0	# expected tokens from good message

"$BOGOFILTER" $OPTS -N -I "${INPUT}"/msg.regtest.n

check_counts

T=2.2	# test #2.2
S=60	# expected tokens from spam message
G=0	# expected tokens from good message

"$BOGOFILTER" $OPTS -s -I "${INPUT}"/msg.regtest.n

check_counts

# move msg.regtest.n from goodlist to spamlist in one big step

T=3	# test #3
S=60	# expected tokens from spam message
G=0	# expected tokens from good message

create_empty_wordlist

"$BOGOFILTER" $OPTS -s -I "${INPUT}"/msg.regtest.s
"$BOGOFILTER" $OPTS -n -I "${INPUT}"/msg.regtest.n
"$BOGOFILTER" $OPTS -Ns -I "${INPUT}"/msg.regtest.n

check_counts

# move msg.regtest.s from spamlist to goodlist in one big step

T=4	# test #4
S=0	# expected tokens from spam message
G=60	# expected tokens from good message

create_empty_wordlist

"$BOGOFILTER" $OPTS -s -I "${INPUT}"/msg.regtest.s
"$BOGOFILTER" $OPTS -n -I "${INPUT}"/msg.regtest.n
"$BOGOFILTER" $OPTS -Sn -I "${INPUT}"/msg.regtest.s

check_counts
