#!/bin/sh

. ${srcdir=.}/t.frame

OUT="$TMPDIR"/test.out
CORRECT="$OUTPUTS"/multiple.wordlists.ref

CF_DEFAULT=default.cf
CF_IGN_REG=ign_reg.cf
CF_IGN_USR_SYS=ign_usr_sys.cf
CF_MRG_USR_SYS=mrg_usr_sys.cf

# single wordlist
cat <<EOF > "$TMPDIR"/$CF_DEFAULT
bogofilter_dir=$TMPDIR
EOF

# ignore list, single wordlist
cat <<EOF > "$TMPDIR"/$CF_IGN_REG
bogofilter_dir=$TMPDIR
wordlist i,ign,ignore.$DB_EXT,1
wordlist r,wrd,wordlist.$DB_EXT,2
EOF

# ignore list, user & system wordlists
cat <<EOF > "$TMPDIR"/$CF_IGN_USR_SYS
bogofilter_dir=$TMPDIR
wordlist r,sys,system.$DB_EXT,6
wordlist i,ign,ignore.$DB_EXT,4
wordlist r,usr,user.$DB_EXT,5
EOF

# merged user & system wordlists
cat <<EOF > "$TMPDIR"/$CF_MRG_USR_SYS
bogofilter_dir=$TMPDIR
wordlist r,usr,user.$DB_EXT,7
wordlist r,sys,system.$DB_EXT,7
EOF

# files for building databases
cat <<EOF > "$TMPDIR"/ignore.txt
ignore
user_low
system_hi
EOF

cat <<EOF > "$TMPDIR"/wordlist.txt
.MSG_COUNT 1 1
.WORDLIST_VERSION 20040500 0
ignore 2 7
common 2 7
word_low 2 7
word_hi 7 2
EOF

cat <<EOF > "$TMPDIR"/user.txt
.MSG_COUNT 1 1
.WORDLIST_VERSION 20040500 0
ignore 2 8
common 2 8
user_low 2 8
user_hi 8 2
EOF

cat <<EOF > "$TMPDIR"/system.txt
.MSG_COUNT 10 10
.WORDLIST_VERSION 20040500 0
ignore 2 9
common 2 9
system_low 2 9
system_hi 9 2
EOF

cat <<EOF > "$TMPDIR"/test.ref
.MSG_COUNT 1 1
.WORDLIST_VERSION 20040500 0
system
EOF

# test message
cat <<EOF > "$TMPDIR"/message
ignore
common
user_low
system_hi
word_low
word_hi
user_low
user_hi
system_low
system_hi
message
EOF

# suppress version info in output
BFOPTS="-D -H -e -m0.1 -y 0 --stats-in-header=no"
BFHEAD='--header-format=%h: %c, tests=bogofilter, spamicity=%p'

# build wordlists
$BOGOUTIL -l "$TMPDIR"/ignore.$DB_EXT   < "$TMPDIR"/ignore.txt
$BOGOUTIL -l "$TMPDIR"/wordlist.$DB_EXT < "$TMPDIR"/wordlist.txt
$BOGOUTIL -l "$TMPDIR"/user.$DB_EXT     < "$TMPDIR"/user.txt
$BOGOUTIL -l "$TMPDIR"/system.$DB_EXT   < "$TMPDIR"/system.txt

(echo "#### 1: score message using each wordlist ####"; echo) >> "$OUT"

for DB in "$TMPDIR"/*.$DB_EXT ; do
    N=`basename "${DB}" .$DB_EXT`
    printf "%-12s" $N: >> "$OUT"
    $BOGOFILTER $BFOPTS "$BFHEAD" -v -C --wordlist=r,wordlist,$N.$DB_EXT,1 -I "$TMPDIR"/message >> "$OUT"
done
echo >> "$OUT"

(echo "#### 1: score message using each config file ####"; echo) >> "$OUT"

for CFG in "$TMPDIR"/*.cf ; do
    N=`basename "$CFG"`
    echo Using $N: >> "$OUT"
    $BOGOFILTER $BFOPTS "$BFHEAD" -vvv -c "$CFG" -I "$TMPDIR"/message >> "$OUT"
    echo >> "$OUT"
done

(echo "#### Update user using $CF_IGN_USR_SYS ####"; echo) >> "$OUT"

# Add message to user.$DB_EXT (first regular wordlist in CF_IGN_USR_SYS)

echo >> "$OUT"
echo common message ignore | $BOGOFILTER $BFOPTS "$BFHEAD" -n -c "$TMPDIR"/$CF_IGN_USR_SYS

(echo "#### 2: score message using each wordlist ####"; echo) >> "$OUT"

for DB in "$TMPDIR"/*.$DB_EXT ; do
    N=`basename "$DB" .$DB_EXT`
    printf "%-12s" $N: >> "$OUT"
    $BOGOFILTER $BFOPTS "$BFHEAD" -v -C --wordlist=r,wordlist,$N.$DB_EXT,1 -I "$TMPDIR"/message >> "$OUT"
done
echo >> "$OUT"

(echo "#### 2: score message using each config file ####"; echo) >> "$OUT"

for CFG in "$TMPDIR"/*.cf ; do
    N=`basename "$CFG"`
    echo Using $N: >> "$OUT"
    $BOGOFILTER $BFOPTS "$BFHEAD" -vvv -c "$CFG" -I "$TMPDIR"/message >> "$OUT"
    echo >> "$OUT"
done

# compare test output to reference output

if  [ $verbose -eq 0 ]; then
    diff "$CORRECT" "$OUT"
    cmp "$CORRECT" "$OUT"
else
    diff $DIFF_BRIEF "$CORRECT" "$OUT"
fi
