if test "$VALGRIND" != ""
then
    VALGRIND="$VALGRIND --tool=memcheck --leak-check=full --error-exitcode=1"
fi

cat >testsuite.msg <<EOF
From: =?iso-8859-1?q?h=E9ader?= <nobody@example.com>
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=zzz

--zzz
Subject: =?iso-8859-1?q?t=E9st?=
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable

b=F6dy

test
EOF

cat >testsuite1.filter <<EOF
if (/héader/)
{
   EXITCODE=0
   exit
}
EXITCODE=1
exit
EOF

cat >testsuite2.filter <<EOF
if (/héader/:h)
{
   EXITCODE=0
   exit
}
EXITCODE=1
exit
EOF
cat >testsuite3.filter <<EOF
if (/héader/:b)
{
   EXITCODE=0
   exit
}
EXITCODE=1
exit
EOF

cat >testsuite4.filter <<EOF
if (/tést/:h)
{
   EXITCODE=0
   exit
}
EXITCODE=1
exit
EOF

cat >testsuite5.filter <<EOF
if (/bödy/:b)
{
   EXITCODE=0
   exit
}
EXITCODE=1
exit
EOF

chmod 600 ./testsuite1.filter ./testsuite2.filter ./testsuite3.filter ./testsuite4.filter ./testsuite5.filter

run_maildrop() {
   $VALGRIND ./maildrop "$@"
}

runtest() {
    type="small message $1"

    # Small file
    if run_maildrop ./testsuite1.filter <testsuite.msg
    then
	:
    else
	echo "Test 1 failed: $type"
	exit 1
    fi

    if run_maildrop ./testsuite2.filter <testsuite.msg
    then
	:
    else
	echo "Test 2 failed: $type"
	exit 1
    fi

    if run_maildrop ./testsuite3.filter <testsuite.msg
    then
	echo "Test 3 failed: $type"
	exit 1
    fi

    if run_maildrop ./testsuite4.filter <testsuite.msg
    then
	:
    else
	echo "Test 4 failed: $type"
	exit 1
    fi

    if run_maildrop ./testsuite5.filter <testsuite.msg
    then
	:
    else
	echo "Test 5 failed: $type"
	exit 1
    fi

    type="small pipe"
    if cat testsuite.msg | run_maildrop ./testsuite1.filter
    then
	:
    else
	echo "Test 1 failed: $type"
	exit 1
    fi

    if cat testsuite.msg | run_maildrop ./testsuite2.filter
    then
	:
    else
	echo "Test 2 failed: $type"
	exit 1
    fi

    if cat testsuite.msg | run_maildrop ./testsuite3.filter
    then
	echo "Test 3 failed: $type"
	exit 1
    fi

    if cat testsuite.msg | run_maildrop ./testsuite4.filter
    then
	:
    else
	echo "Test 4 failed: $type"
	exit 1
    fi

    if cat testsuite.msg | run_maildrop ./testsuite5.filter
    then
	:
    else
	echo "Test 5 failed: $type"
	exit 1
    fi

    dd if="/dev/zero" bs=8192 count=2 2>/dev/null | tr '\0' '\012' >>testsuite.msg

    type="large message $1"

    # Large file
    if run_maildrop ./testsuite1.filter <testsuite.msg
    then
	:
    else
	echo "Test 1 failed: $type"
	exit 1
    fi

    if run_maildrop ./testsuite2.filter <testsuite.msg
    then
	:
    else
	echo "Test 2 failed: $type"
	exit 1
    fi

    if run_maildrop ./testsuite3.filter <testsuite.msg
    then
	echo "Test 3 failed: $type"
	exit 1
    fi

    if run_maildrop ./testsuite4.filter <testsuite.msg
    then
	:
    else
	echo "Test 4 failed: $type"
	exit 1
    fi

    if run_maildrop ./testsuite5.filter <testsuite.msg
    then
	:
    else
	echo "Test 5 failed: $type"
	exit 1
    fi

    type="large pipe"
    if cat testsuite.msg | run_maildrop ./testsuite1.filter
    then
	:
    else
	echo "Test 1 failed: $type"
	exit 1
    fi

    if cat testsuite.msg | run_maildrop ./testsuite2.filter
    then
	:
    else
	echo "Test 2 failed: $type"
	exit 1
    fi

    if cat testsuite.msg | run_maildrop ./testsuite3.filter
    then
	echo "Test 3 failed: $type"
	exit 1
    fi

    if cat testsuite.msg | run_maildrop ./testsuite4.filter
    then
	:
    else
	echo "Test 4 failed: $type"
	exit 1
    fi

    if cat testsuite.msg | run_maildrop ./testsuite5.filter
    then
	:
    else
	echo "Test 5 failed: $type"
	exit 1
    fi
}

runtest ""

for f in testsuite?.filter
do
    echo "SHELL=/bin/sh" > t
    echo 'xfilter "cat"' >>t
    cat $f >>t
    chmod 600 t
    mv -f t $f
done

runtest " with xfilter"

cat >testsuite.msg <<EOF
X-Spam-Flag: yes
EOF
cat >testsuite1.filter <<EOF
EXITCODE=0
if (/^X-Spam-Flag/)
{
    EXITCODE=1
}
exit
EOF

chmod 600 testsuite1.filter

if run_maildrop ./testsuite1.filter <testsuite.msg
then
    echo "bodyless message test failed"
    exit 1
fi

cat >testsuite.msg <<EOF
Subject: mainpart
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="zz"

--zzz
Subject: attachmentpart

body

--zzz--
EOF

cat >testsuite1.filter <<EOF
if (/body/)
{
     echo "Should not have found the body"
     EXITCODE=1
}

if (!/body/:b)
{
     echo "Should have found the body"
     EXITCODE=1
}

if (!(/^Subject: mainpart/ && /^Subject: mainpart/))
{
     echo "all header search 1 failed"
     EXITCODE=1
}

if (!(/^Subject: mainpart/:h && /^Subject: mainpart/:h))
{
     echo "all header search 2 failed"
     EXITCODE=1
}

if (!/^Subject: mainpart/:H)
{
     echo "mainpart search failed"
     EXITCODE=1
}

if (/^Subject: attachmentpart/:H)
{
     echo "attachment search should have failed"
     EXITCODE=1
}
exit
EOF

if run_maildrop ./testsuite1.filter <testsuite.msg
then
	:
else
	echo "header/body test failed"
	exit 1
fi

rm -f testsuite?.filter testsuite.msg

rm -f testsuite.dotlock
cat >testmd <<EOF
dotlock "testsuite.dotlock" {
	EXITCODE=5
	exit
}
EOF
chmod 600 testmd
run_maildrop ./testmd </dev/null
if test $? != 5
then
	echo "dotlock test did not exit with the correct exit code"
	exit 1
fi
if test -f testsuite.dotlock
then
	echo "dotlock test did not clean up after itself"
	exit 1
fi
rm -f testmd
