#!/bin/sh -x

# verify that --db-transaction option is given 
# precedence over probing for a database environment

# to create a wordlist without transaction files:
#
#	bogofilter -d dir -n < /dev/null
#
# or:
#
#	bogofilter -d dir -n --db-transaction=no < /dev/null
#
# to create a wordlist with transaction files, or 
# to convert to a wordlist with transaction files
#
#	bogofilter -d dir -n --db-transaction=yes < /dev/null

. ${srcdir=.}/t.frame

# Skip test if non-default configuration
if [ -n "$USE_TRANSACTIONS" ] ; then
    exit 77
fi

AUTO_XA=`$BOGOFILTER -C -V 2>&1 | sed -n '2p;' | grep AUTO-XA || :`
if [ -z "$AUTO_XA" ]; then
    exit 77
fi

unset BF_FORCE_NON_TRANSACTIONAL || :
unset BF_FORCE_TRANSACTIONAL || :

OUT="$TMPDIR/test.out"
MSG="$TMPDIR"/test.msg
LOG="$BOGOFILTER_DIR/log.0000000001"
# cksum is POSIX compliant ($HASHTOOL isn't)
HASHTOOL=cksum

NO="--db-transaction=no"
YES="--db-transaction=yes"

cat <<EOF > "$MSG"
this is a test message
EOF

# clean up - t.frame may have created everything for us
rm -f	"$BOGOFILTER_DIR"/log.?????????? \
	"$BOGOFILTER_DIR"/__db.??? \
	"$BOGOFILTER_DIR"/lockfile-?

# transactions=no;  no log file

$BOGOFILTER -C $NO -H -n -I "$MSG"

if [ -f "$LOG" ] ; then
    echo "Failed test 't=n'" >> "$OUT"
    exit 1
fi

# probe=yes, no environment;  no log file

$BOGOFILTER -C -H -n -I "$MSG"

if [ -f "$LOG" ] ; then
    echo "Failed test 'p=y, e=n'" >> "$OUT"
    exit 1
fi

# transactions=yes;  log file should appear

$BOGOFILTER -C $YES -H -s -I "$MSG"

if [ ! -f "$LOG" ] ; then
    echo "Failed test 't=y'" >> "$OUT"
    exit 1
fi

# probe=yes;  log file should change

BEF=`$HASHTOOL "$LOG"`
$BOGOFILTER -C -H -n -I "$MSG"
AFT=`$HASHTOOL "$LOG"`

if [ "$BEF" = "$AFT" ] ; then
    echo "Failed test 'p=y, e=y'" >> "$OUT"
    echo BEF: $BEF >> "$OUT"
    echo AFT: $AFT >> "$OUT"
    exit 1
fi

# probe=yes, transactions=no;  log file should not change

BEF=`$HASHTOOL "$LOG"`
$BOGOFILTER -C $NO -H -s -I "$MSG"
AFT=`$HASHTOOL "$LOG"`

if [ "$BEF" != "$AFT" ] ; then
    echo "Failed test 'p=y, t=n'" >> "$OUT"
    echo BEF: $BEF >> "$OUT"
    echo AFT: $AFT >> "$OUT"
    exit 1
fi
