#! /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
#	 bogodir.YYYYMMDD.  This is useful when something is different.

. ${srcdir=.}/t.frame

HOME="$TMPDIR/home"
mkdir "$HOME"
export HOME

LOG="$TMPDIR/bogodir.log"
cat /dev/null >"$LOG"

map_rc()
{
    (
	set +e
	echo >> "$LOG" "$@"
	echo >> "$LOG" "### expect: $expect"
	result=`"$@" 2>&1 | tee -a "$LOG" | grep open | head -1`
	ok=`echo "$result" | grep "$expect"`
	if [ -n "$ok" ] ; then
	    echo >>"$LOG" "### ok: $ok"
	    echo >>"$LOG" "PASS"
	else
	    echo >>"$LOG" "### got '$result'"
	    echo >>"$LOG" "FAIL"
	fi
	echo >> "$LOG" ""

	[ -z "$ok" ] && rc=1
	[ -n "$ok" ] && rc=0
	exit $rc
    )
}

#standard test (bogodir.log=3225)
OPTS=" -x d -vv -D"

#a bit more info (bogodir.log=5071)
#OPTS=" -x dcw -vvv -D"

#everything (bogodir.log=5416)
#OPTS=" -x cdfghlmrstw -vvvv -D"

# just $HOME should display /home/userid
unset BOGOFILTER_DIR
expect="$HOME/.bogofilter"
map_rc $BOGOFILTER $OPTS -C < /dev/null

cat <<EOF >"$TMPDIR/bogodir.cf"
bogofilter_dir=$TMPDIR/bogoconf.d
EOF

# config file should display "command"
expect="$TMPDIR/bogoconf.d"
map_rc $BOGOFILTER $OPTS -c "$TMPDIR/bogodir.cf" < /dev/null

# command line and config file should display "command.d"
expect="$TMPDIR/command.d"
map_rc $BOGOFILTER $OPTS -c "$TMPDIR/bogodir.cf" -d "$TMPDIR/command.d" < /dev/null

# $HOME and $BOGOFILTER_DIR should display $TMPDIR

expect="$BOGOFILTER_DIR"
map_rc $BOGOFILTER $OPTS -C < /dev/null
map_rc $BOGOFILTER $OPTS -c "$TMPDIR/bogodir.cf" < /dev/null

# command line should display "command.d"
expect="$TMPDIR/command.d"
map_rc $BOGOFILTER $OPTS -C -d "$TMPDIR/command.d" < /dev/null
map_rc $BOGOFILTER $OPTS -c "$TMPDIR/bogodir.cf" -d "$TMPDIR/command.d" < /dev/null

# test whether command line supercedes bogus config file entry
cat <<EOF >"$TMPDIR/bogodir.cf"
bogofilter_dir=$TMPDIR/bogus
wordlist r,word,wordlist.$DB_EXT,1
EOF

map_rc $BOGOFILTER $OPTS -c "$TMPDIR/bogodir.cf" -d "$TMPDIR/command.d" -v < /dev/null
test ! -d "$TMPDIR/bogus"
