#! /bin/sh

if test ! -d Mail -o ! -d Maildir; then
    echo "please prepare mailboxes"
    exit 1
fi

if test ! -d test.Mail -o ! -d test.Maildir; then
    echo "Please run scmail.scm test first."
    exit 1
fi

SCMAIL_REFILE="gosh -I. -I.. -l dummy-socket.scm ../scmail-refile"

check_nfiles() {
    if test `find $1  -type f -print |wc -l` != \
            `find $2 -type f -print |wc -l`; then
        echo "Number of files differs: $1 and $2"
        exit 1
    fi
}

scbayes_common() {
    task=$1
    config=$2
    shift
    shift

    echo -n "scbayes ${task}ing... "
    gosh -I.. ../scbayes -d $PWD/test.scmail -q -c $config $* --$task-spam spam
    gosh -I.. ../scbayes -d $PWD/test.scmail -q -c $config $* --$task-nonspam from.foo ml.test-ml-example-org
    echo "done."
}

scbayes_learn() {
    scbayes_common learn $*
}

scbayes_unlearn() {
    scbayes_common unlearn $*
}

scbayes_learn3() {
    for i in 1 2 3; do
        scbayes_learn $*
    done
}

scbayes_check() {
    if gosh -I.. ../scbayes -c $1  --check-spam spam; then
        echo "spam filtering succeeded"
    else
        echo "ERROR: spam filtering failed"
        exit 1
    fi
    gosh -I.. ../scbayes -c test.config --check-mail 5 || exit 1

    if gosh -I.. ../scbayes -c $1  --check-nonspam from.foo; then
        echo "nonspam filtering succeeded"
    else
        echo "ERROR: nonspam filtering failed"
        exit 1
    fi
    gosh -I.. ../scbayes -c test.config --check-mail 1 || exit 1
}

rm -f test.scmail-deliver
rm -rf test.Mail2 test.Maildir2
cp -rp Mail test.Mail2
cp -rp Maildir test.Maildir2
inode_before=`ls -i test.Maildir2/cur/1000000001.localhost:abc | awk '{print $1}'`
rm -rf test.scmail
mkdir test.scmail

rm -rf test.scmail
mkdir test.scmail

cat <<EOF > test.config
(
 :smtp-host "localhost"
 :log-file  "$PWD/test.scmail/log"
 :umask     #o007

 :token-table   "$PWD/test.scmail/token-table.dbm"
 :digest       "$PWD/test.scmail/digest.dbm"

 :size-limit 1024

 ;; for MH
 :mailbox   "$PWD/test.Mail2"
 :inbox     "inbox"
 :spam      "spam"   ; a folder for spam mails
 :mailbox-type  mh
 )
EOF

cat <<EOF > test.config2
(
 :smtp-host "localhost"
 :log-file  "$PWD/test.scmail/log"
 :umask     #o007

 :token-table   "$PWD/test.scmail/token-table.dbm"
 :digest       "$PWD/test.scmail/digest.dbm"

 :size-limit 1024

 ;; for Maildir
 :mailbox   "$PWD/test.Maildir2"
 :inbox     ""
 :spam      "spam"   ; a folder for spam mails
 :mailbox-type  maildir
 )
EOF

gosh -ugauche.test -e '(test-start "scmail-refile --dry-run")' -Eexit
$SCMAIL_REFILE -d $PWD/test.scmail -c test.config  -r test-rules -n |tee test.log.dryrun
diff -r Mail test.Mail2 || exit 1

$SCMAIL_REFILE -d $PWD/test.scmail -c test.config2  -r test-rules -n |tee test.log.dryrun2
diff -r Maildir test.Maildir2 || exit 1

gosh -ugauche.test -e '(test-end)' -Eexit

gosh -ugauche.test -e '(test-start "scmail-refile/scmail-deliver for MH")' -Eexit
$SCMAIL_REFILE -d $PWD/test.scmail -c test.config  -r test-rules |tee test.log.normal
diff -r test.Mail test.Mail2 || exit 1

# log files to stdout must be same
diff test.log.dryrun test.log.normal

# dry run
cat 5 | gosh -I.. ../scmail-deliver -d $PWD/test.scmail -c test.config -r test-rules -n >> test.scmail-deliver

# must be stored in test.Mail2/spam/3

cat 5 | gosh -I.. ../scmail-deliver -d $PWD/test.scmail -c test.config  -r test-rules

diff test.Mail2/spam/2 test.Mail2/spam/3 || exit 1

gosh -ugauche.test -e '(test-end)' -Eexit

gosh -ugauche.test -e '(test-start "scbayes for MH")' -Eexit

scbayes_learn3 test.config --force
scbayes_check test.config


# must be stored in test.Mail2/spam/4
echo '(add-bayesian-filter-rule!)' > test.test-rules
cat 4 | gosh -I .. ../scmail-deliver -d $PWD/test.scmail -c test.config  -r test.test-rules >> test.scmail-deliver
diff test.Mail2/spam/1 test.Mail2/spam/4 || exit 1


gosh -ugauche.test -e '(test-end)' -Eexit

gosh -ugauche.test -e '(test-start "scmail-refile/scmail-deliver for Maildir")' -Eexit

rm -rf test.scmail
mkdir test.scmail

$SCMAIL_REFILE -d $PWD/test.scmail -c test.config2  -r test-rules | tee test.log.normal2
# log files to stdout must be same
diff test.log.dryrun2 test.log.normal2

check_nfiles test.Maildir test.Maildir2

# must be stored in spam folder.
cat 5 | gosh -I.. ../scmail-deliver -d $PWD/test.scmail -c test.config2  -r test-rules >> test.scmail-deliver

gosh -ugauche.test -e '(test-end)' -Eexit

gosh -ugauche.test -e '(test-start "scbayes for Maildir")' -Eexit

scbayes_learn3 test.config2 --force
scbayes_check test.config2

# must be stored in spam folder.
echo '(add-bayesian-filter-rule!)' > test.test-rules
cat 4 | gosh -I .. ../scmail-deliver -d $PWD/test.scmail -c test.config2  -r test.test-rules >> test.scmail-deliver

check_nfiles test.Mail2/spam test.Maildir2/spam

gosh -I.. ../scbayes -c test.config2 --table-stat > test.stat1

gosh -ugauche.test -e '(test-end)' -Eexit


gosh -ugauche.test -e '(test-start "scbayes --force")' -Eexit

# without --force, the token table must not be changed.
scbayes_learn test.config2
gosh -I.. ../scbayes -c test.config2 --table-stat > test.stat2
cmp test.stat1 test.stat2 || exit 1

# with --force, the token table must be changed.
scbayes_learn test.config2 --force
gosh -I.. ../scbayes -c test.config2 --table-stat > test.stat3
cmp test.stat1 test.stat3 > /dev/null && exit 1

gosh -ugauche.test -e '(test-end)' -Eexit

gosh -ugauche.test -e '(test-start "scbayes --unlearn-spam/nonspam")' -Eexit

rm -rf test.scmail
mkdir test.scmail

scbayes_learn test.config2
scbayes_unlearn test.config2
gosh -I.. ../scbayes -c test.config2 --table-stat > test.stat4

tail +2 test.stat4 > test.stat4.1
grep ' 0w/ .* 0m .* 0w/ .* 0m' test.stat4 > test.stat4.2
cmp test.stat4.1 test.stat4.2 || exit 1

# they must be empty
gosh -I.. ../scbayes -c test.config2 --dump-table > test.table
gosh -I.. ../scbayes -c test.config2 --dump-digest > test.digest
cmp test.table /dev/null || exit 1
cmp test.digest /dev/null || exit 1

gosh -ugauche.test -e '(test-end)' -Eexit

gosh -ugauche.test -e '(test-start "scmail-deliver: output must be empty")' -Eexit

# scmail-deliver must not output anything.
cmp test.scmail-deliver /dev/null || exit 1

gosh -ugauche.test -e '(test-end)' -Eexit

gosh -ugauche.test -e '(test-start "scbayes --flush-interval")' -Eexit

scbayes_learn test.config2 --table test.table-1.dbm --digest test.digest-1.dbm
scbayes_learn test.config2 --flush-interval 1 --table test.table-2.dbm --digest test.digest-2.dbm

# they must have the same content.
gosh -I.. ../scbayes -c test.config2 --digest test.digest-1.dbm --dump-digest | sort | awk '{ print $ 1}' > test.digest-1
gosh -I.. ../scbayes -c test.config2 --digest test.digest-2.dbm --dump-digest | sort | awk '{ print $ 1}' > test.digest-2

gosh -I.. ../scbayes -c test.config2 --table test.table-1.dbm --dump-table | sort > test.table-1
gosh -I.. ../scbayes -c test.config2 --table test.table-2.dbm --dump-table | sort > test.table-2

cmp test.table-1 test.table-2 || exit 1
cmp test.digest-1 test.digest-2 || exit 1

gosh -ugauche.test -e '(test-end)' -Eexit

gosh -ugauche.test -e '(test-start "umask")' -Eexit
test `ls -ld test.Mail2/backup |awk '{print $1}'` = drwxr-x--- || exit 1
test `ls -l test.Mail2/backup/1 |awk '{print $1}'` = -rw-rw---- || exit 1
gosh -ugauche.test -e '(test-end)' -Eexit

gosh -ugauche.test -e '(test-start "maildir refile: sys-rename is used?")' -Eexit

inode_after=`ls -i test.Maildir2/from.foo/cur/1000000001.localhost:abc | awk '{print $1}'`

test "$inode_before" -a "$inode_before" = "$inode_after"
gosh -ugauche.test -e '(test-end)' -Eexit


gosh -ugauche.test -e '(test-start "scmail-deliver to Maildir/new")' -Eexit
cat 7 | gosh -I .. ../scmail-deliver -d $PWD/test.scmail -c test.config2  -r test.test-rules
file=`tail -1 test.scmail/log |perl -nle 'print $1 if /refile: \(stdin\) -> (new\/.*)$/'`
diff 7 test.Maildir2/$file || exit 1
gosh -ugauche.test -e '(test-end)' -Eexit


exit 0
