#! /bin/sh -x

. ${srcdir:=.}/t.frame

#
# test common bogoutil functions
#

# initialize
rm -r "$TMPDIR"/* # reset TXN detection
if [ $DB_TXN = true ] ; then addarg=--db-transaction=yes ; else addarg= ; fi
echo bacon on toast | $BOGOFILTER -C -d "$TMPDIR" -n $addarg
echo foul eggs      | $BOGOFILTER -C -d "$TMPDIR" -s $addarg

# see that these do not crash
$BOGOUTIL -C -v -V > /dev/null
$BOGOUTIL -C -h    > /dev/null

# check if bogoutil can count ;-)
cat <<_EOF > "$TMPDIR"/ref
7 tokens dumped
_EOF
$BOGOUTIL -C -v -d "$WORDLIST" -D | tail -1 >  "$TMPDIR"/out
cmp -s "$TMPDIR"/out "$TMPDIR"/ref

# ROBX calculation cannot work, insufficient data
if $BOGOUTIL -C -r "$WORDLIST" ; then false ; fi
if $BOGOUTIL -C -R "$WORDLIST" ; then false ; fi

# load more data as ROBX fodder
$BOGOFILTER -xd -C -n -d "$TMPDIR" -M -B "$srcdir"/inputs/good.mbx
$BOGOFILTER -xd -C -s -d "$TMPDIR" -M -B "$srcdir"/inputs/spam.mbx

# printing robx must not store the token
$BOGOUTIL -C -r "$WORDLIST" | egrep '^ ?0?.[0-9]+$' > /dev/null
if $BOGOUTIL -C -w "$WORDLIST" .ROBX | egrep '^[[:alnum:]]' ; then
    false
fi

# storing robx must store the token, quite obviously :-)
# also use -p so we test both -p and -w
$BOGOUTIL -C -R "$WORDLIST"
$BOGOUTIL -C -p "$WORDLIST" .ROBX | egrep '^\.ROBX' > /dev/null

# check if it can dump tokens from stdin
echo "head:toast" | $BOGOUTIL -C -p "$WORDLIST" \
| egrep '^head:toast' > /dev/null

if [ $DB_TYPE = db ] || [ $DB_TYPE = sqlite ] ; then
    ps=`$BOGOUTIL -C --db-print-pagesize "$WORDLIST"`
    test -n "$ps"
    test "$ps" != UNKNOWN
fi

if [ $DB_TYPE = db ] ; then
    $BOGOUTIL -C --db-verify "$WORDLIST"
    pc=`$BOGOUTIL -C --db-print-leafpage-count "$WORDLIST"`
    test "$pc" != ""
    test "$pc" != "UNKNOWN"
    test "$pc" != "0"

    if [ $DB_TXN = true ] ; then
	$BOGOUTIL -C --db-recover "$TMPDIR"/
	$BOGOUTIL -C --db-checkpoint "$TMPDIR"/
	if ls "$TMPDIR/log."* > /dev/null 2>&1 ; then
	    $BOGOUTIL -C --db-prune "$TMPDIR"/
	    ls "$TMPDIR"/log.* > "$TMPDIR"/logs.1
	    $BOGOUTIL -C --db-list-logfiles "$TMPDIR" all > "$TMPDIR/logs.2"
	    cmp "$TMPDIR"/logs.1 "$TMPDIR"/logs.2
	fi
	if ls "$TMPDIR/__db."* > /dev/null 2>&1 ; then
	    $BOGOUTIL -C --db-remove-environment "$TMPDIR"/
	    if ls "$TMPDIR/__db."* > /dev/null 2>&1 ; then
		false
	    fi
	fi
    fi
fi

#test with wordlist in current directory
BOGOUTIL="$VAL`pwd`/${relpath}/bogoutil$EXE_EXT"
WORDLIST="wordlist.${DB_EXT}"
OPC=$PRINTCORE
case $srcdir in
    /*)	PRINTCORE="$srcdir"/$INSERT/printcore ;;
    *)	PRINTCORE=`pwd`/"$srcdir"/$INSERT/printcore ;;
esac

unset BOGOFILTER_DIR

cd "$TMPDIR"
"$BOGOUTIL" -C -w "$WORDLIST" .MSG_COUNT > /dev/null
"$BOGOUTIL" -C -p "$WORDLIST" .MSG_COUNT > /dev/null
PRINTCORE=$OPC
cd - > /dev/null
