#!/bin/sh
#
# Copyright 1998 - 2023 S. Varshavchik.  See COPYING for
# distribution information.

PWD=`pwd`

unset LC_CTYPE
unset LC_NUMERIC
unset LC_TIME
unset LC_COLLATE
unset LC_MONETARY
unset LC_MESSAGES
unset LC_PAPER
unset LC_NAME
unset LC_ADDRESS
unset LC_TELEPHONE
unset LC_MEASUREMENT
unset LC_IDENTIFICATION
unset LC_ALL
unset VERBOSE

exec 3>&2
if test "$VALGRIND" != ""
then
    VALGRIND="$VALGRIND --tool=memcheck --leak-check=full --error-exitcode=1 --log-fd=3"
fi

set -e

$VALGRIND ./testtimer

if test "@USE_GDBM@@USE_DB@" != "00" -a -x ../makedat/makedatprog
then

cat <<EOF >testsuite.chk
foo	bar
foo.bar.com	foobar
foobar@foo.bar.com	barfoo
.
EOF
$VALGRIND ../makedat/makedatprog testsuite.chk testsuite.tmp testsuite.dat

cat <<EOF >testsuite.recipe
if ( gdbmopen("$PWD/testsuite.dat", "W") != 0 )
{
	exit
}

FOO=gdbmfetch("foo")
echo "FOO: \$FOO"

FOO=gdbmfetch("foobar@foo.bar.com", "D")
echo "FOO: \$FOO"
FOO=gdbmfetch("foobaz@foo.bar.com", "D")
echo "FOO: \$FOO"
gdbmstore("foobaz@foo.bar.com", "BAR");
FOO=gdbmfetch("foobaz@foo.bar.com", "D")
echo "FOO: \$FOO"
EOF

chmod go-rw testsuite.recipe
cat <<EOF >testsuite.chk
FOO: bar
FOO: barfoo
FOO: foobar
FOO: BAR
EOF
$VALGRIND ./maildrop ./testsuite.recipe </dev/null >teststdin
diff teststdin testsuite.chk

fi


rm -rf testsuite.maildir
../maildir/maildirmake ./testsuite.maildir

cat <<EOF >testsuite.recipe

FLAGS="S"
to "$PWD/testsuite.maildir"
EOF

chmod go-rw testsuite.recipe

echo "To: testsuite" >teststdin
$VALGRIND ./maildrop ./testsuite.recipe <teststdin

echo "2,S" >testsuite.chk

ls testsuite.maildir/cur | sed 's/.*://' | diff -U 3 - testsuite.chk

if test "@maildirquota@" = 1
then
	rm -rf testsuite.maildir
	../maildir/maildirmake ./testsuite.maildir

	cat <<EOF >testsuite.recipe

	MAILDIRQUOTA="1C"
	exception {
		cc "$PWD/testsuite.maildir"
		echo "Delivered message one"
		cc "$PWD/testsuite.maildir"
		echo "Delivered message two"
	}
	echo "Done."
	exit
EOF

	chmod go-rw testsuite.recipe
	cat <<EOF >testsuite.chk
Delivered message one
Done.
EOF
	echo "To: testsuite" >testsuite.stdin
	$VALGRIND ./maildrop ./testsuite.recipe <testsuite.stdin >testsuite.chk2
	diff testsuite.chk2 testsuite.chk
fi


cat >testsuite.chk <<EOF
#! /bin/bash
echo "Parameters: \$*"
cat
EOF

chmod +x testsuite.chk
cat >testsuite.recipe <<EOF
SENDMAIL='./testsuite.chk'
to "! nobody@example.com"
EOF
echo "Subject: test-forward" | $VALGRIND ./maildrop testsuite.recipe

rm -rf testsuite.maildir
cat >testsuite.recipe <<EOF
to "./testsuite.maildir"
EOF
echo "Subject: msg1" | $VALGRIND ./maildrop testsuite.recipe
echo "Subject: msg2" | $VALGRIND ./maildrop testsuite.recipe
sed '/^From /d' <testsuite.maildir
ls -al testsuite.maildir | cut -c1-10
cat >testsuite.recipe <<EOF
UMASK='022'
to "./testsuite.maildir"
EOF
rm testsuite.maildir
echo "Subject: msg1" | $VALGRIND ./maildrop testsuite.recipe
ls -al testsuite.maildir | cut -c1-10
cat >testsuite.recipe <<EOF
UMASK='022'
to "| cat >testsuite.maildir2"
EOF
echo "Subject: testsuite.maildir2" | $VALGRIND ./maildrop testsuite.recipe
cat testsuite.maildir2
rm -f testsuite.maildir2

cat <<EOF >testsuite.recipe
if ( 2 && 3 )
{
     echo "Yes1"
}

if ( 2 && 0 )
{
     echo "No1"
}

if ( 2 & 1 )
{
     echo "No2"
}

if ( 2 & 3 )
{
     echo "Yes2"
}

a=(4 || 2)

echo "Four: \$a"

a=(4 | 2)

echo "Six: \$a"

a=(5 && 3)

echo "Three: \$a"

a=(5 & 3)

echo "One: \$a"

a=! 5

echo "Zero: \$a"

a= ~5

echo "Minus six: \$a"

EOF

chmod go-rw testsuite.recipe
$VALGRIND ./maildrop ./testsuite.recipe </dev/null


./reformail -s /bin/bash -c 'cat >testmsg.$FILENO' <<EOF

From message1
From: nobody@example.com
Subject: message1

message1

From message2
From: nobody@example.com
Subject: message2

message2
EOF

./reformail -x subject: <testmsg.000
./reformail -x subject: <testmsg.001
rm testmsg.00[01]

rm -f testcache.dat
set +e
./reformail -D 2000 testcache.dat <<EOF
Subject: dummy
Message-ID: <1>

EOF
echo "Not dupe: $?"
./reformail -D 2000 testcache.dat <<EOF
Subject: dummy
Message-ID: <1>

EOF
echo "Not dupe: $?"
rm -f testcache.dat
set -e

./reformail -x from: -x subject: <<EOF
From: extract1@example.com
Subject: subject1
Date: today

test
EOF
./reformail -X from: -X subject: <<EOF
From: extract2@example.com
Subject: subject2
Date: today

test
EOF

./reformail -a 'From: me@example.com' -a 'Subject: replacementsubject' <<EOF
From: nobody@example.com

-a test
EOF

./reformail -A 'From: me@example.com' -A 'Subject: replacementsubject' <<EOF
From: nobody@example.com

-A test
EOF

./reformail -c <<EOF
From: nobody@example.com
Subject: catenate
	header test
To: nobody@example.com

test
EOF

./reformail -f0 <<EOF
From testing
Return-Path: old
Subject: remove From_ test

test
EOF
./reformail -f0 <<EOF
Subject: remove From_ test

test
EOF
./reformail -f1 <<EOF
From testing
From: nobody@example.com
Subject: add From_ test

test
EOF
(./reformail -f1 | sed 's/example.com.*/example.com/') <<EOF
From: nobody@example.com
Subject: add From_ test

test
EOF
./reformail -i 'Header1: value1' -i 'Header2: value2' <<EOF
From: nobody@example.com
Subject: -i test
Header1: value1

test
EOF
./reformail -I 'Header1:' -I 'Header2: value2' <<EOF
From: nobody@example.com
Subject: -I test
Header1: value1
Header2: old value2

test
EOF
./reformail -R subject: old-subject: <<EOF
Subject: -R option test
From: nobody@example.com

-R option test
EOF
./reformail -u 'subject:' <<EOF
Subject: -u test, line1
Subject: -u test, line2

Testing -u option
EOF
./reformail -U 'subject:' <<EOF
Subject: -u test, line1
Subject: -u test, line2

Testing -u option
EOF

cat >testsuite.recipe <<EOF
if (hasaddr("nobody@example.com"))
{
  echo "Found"
}
else
{
  echo "Not found"
}
exit
EOF

cat >testsuite.msg <<EOF
To: "nobody@host2.example.com" <nobody@example.com>

test
EOF
$VALGRIND ./maildrop testsuite.recipe <testsuite.msg

cat >testsuite.msg <<EOF
To: "nobody@example.com" <nobody@host2.example.com>

test
EOF
$VALGRIND ./maildrop testsuite.recipe <testsuite.msg

cat >testsuite.recipe <<EOF
echo getaddr('To: list: "user 1" <user1@example.com>, user2@example.com;')
exit
EOF
$VALGRIND ./maildrop testsuite.recipe </dev/null

cat >testsuite.msg <<EOF
logfile "./nonexistent/logfile"
EOF
rm -f testsuite.lock

cat >testsuite.recipe <<EOF
echo "VERBOSE=\$VERBOSE"; f="x"
f=\`echo ' '; echo Yes; echo ' '\`
g="y\${f}z\$f"
g=escape('+')
echo \$g
if (1)
{
  g="1"
}
exception {
  include "./testsuite.msg"
}
import PATH
dotlock "testsuite.lock" {
   logfile "/dev/null"
   log "Logging"
}
flock "testsuite.lock" {
}
exit
EOF
chmod 600 testsuite.recipe
$VALGRIND  ./maildrop -V 9 ./testsuite.recipe 2>&1 </dev/null

cat >testsuite.msg <<EOF
Header: one
Header: two

ok
EOF
cat >testsuite.chk <<EOF
one
two
EOF
cat >testsuite.recipe <<EOF

foreach /Header: (.*)/
{
  echo "Matched: \$MATCH (\$MATCH1)"
}

/^(Header:) (.*)/
echo "Matched: \$MATCH (\$MATCH1) (\$MATCH2)"

if (lookup("one", "testsuite.chk", ""))
{
  echo "One found"
}

if (lookup("two", "testsuite.chk", ""))
{
  echo "Two found"
}

if (lookup("three", "testsuite.chk", ""))
{
  echo "Two found"
}
echo substr("abc", 1)
echo substr("abc", 1, 2)

echo "a" =~ /a/;
echo "a" =~ /b/;

i=0
while (\$i < 5)
{
    echo \$i
    i=\$i+1
}
system "exit 0"
echo tolower("Alpha")
echo toupper("BETA");
unset i
exit
EOF
$VALGRIND ./maildrop -V 9 ./testsuite.recipe 2>&1 <testsuite.msg

rm -rf testsuite.maildir
../maildir/maildirmake ./testsuite.maildir

cat >testsuite.recipe <<EOF
KEYWORDS="alpha,beta"
to "./testsuite.maildir"
EOF
cat >testsuite.msg <<EOF
From: mrsam
EOF
$VALGRIND ./maildrop -V 9 ./testsuite.recipe 2>&1 <testsuite.msg
../maildir/maildirkw -L ./testsuite.maildir | tr ' ' '\012' | sed 1d | sort

cat >testsuite.recipe <<EOF
if (0 < 1 || 1 < 2)
{
}

if ( 1 > 2 && 2 > 3)
{
}

if ( 1 <= 2 && 2 >= 1 && 1 == 1 && 1 != 2 && "a" lt "b" && "a" le "a" && \
     "b" gt "a" && b ge "b" && "a" eq "a" && "a" ne "b")
{
}

1 | 2;
3 & 1;
1 + 2*3 - 4 / 4
length("abc")
exit
EOF
$VALGRIND ./maildrop -V 9 ./testsuite.recipe 2>&1 <testsuite.msg

rm -rf testsuite.maildir
../maildir/maildirmake testsuite.maildir

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

--aaa
Content-Type: text/plain

--aaa--

Junk following the closing MIME delimiter.
EOF

cat >testsuite.recipe <<EOF
to "./testsuite.maildir"
EOF

$VALGRIND ./maildrop -A 'Subject: interesting' testsuite.recipe <testsuite.msg
echo testsuite.maildir/new/* | cut -d, -f2-

rm -f testsuite.maildir/new/*

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

$VALGRIND ./maildrop -A 'Subject: interesting' testsuite.recipe <testsuite.msg
echo testsuite.maildir/new/* | cut -d, -f2-

rm -f testsuite.recipe
rm -f testsuite.msg
rm -f testsuite.lock
rm -f testsuite.chk*
rm -f testsuite.dat
rm -rf testsuite.maildir
rm -f teststdin
