#!/bin/sh

# Maintainer's sanity check

TZ=EST5EDT
export TZ

echo '****************************' >&2
echo '* Sanity check in progress *' >&2
echo '****************************' >&2
rm -rf confmdtest
mkdir confmdtest || exit 1
mkdir confmdtest/tmp || exit 1
mkdir confmdtest/cur || exit 1
mkdir confmdtest/new || exit 1

cat >confmdtest/cur/msg1:2,S <<EOF || exit 1
From: John <john@example.com>
To: Steve <steve@example.com>, Tom <tom@example.com>
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="b1"
Subject: This is the message

foobar
--b1
Content-Type: text/plain

This is section 1

--b1
Content-Type: text/plain

This is section 2

--b1
Content-Type: message/rfc822

Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="b2"
Subject: This is message part 3

foobar
--b2
Content-Type: text/plain

This is section 3.1

--b2
Content-Type: text/plain

This is section 3.2

--b2--
--b1
Content-Type: multipart/mixed; boundary="b3"

foobar
--b3
Content-Type: text/plain

This is section 4.1

--b3
Content-Type: message/rfc822

Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="b4"
Subject: This is message part 4.2

foobar
--b4
Content-Type: text/plain

This is section 4.2.1

--b4
Content-Type: multipart/alternative; boundary="b5"

foobar
--b5
Content-Type: text/plain

This is section 4.2.2.1

--b5
Content-Type: text/plain

This is section 4.2.2.2

--b5--

--b4--

--b3--

--b1--
EOF

env MAILDIR=confmdtest ./imapd <<EOF | sed 's/UIDVALIDITY [0-9]*/UIDVALIDITY/;s/INTERNALDATE "[^"]*"/INTERNALDATE -DATE-/g'
a001 select inbox
a002 fetch 1 (bodystructure)
a003 fetch 1 (body[])
a004 fetch 1 (body[]<500.100>)
a005 fetch 1 (body[1])
a006 fetch 1 (body[1.text])
a007 fetch 1 (body[2])
a008 fetch 1 (body[2.text])
a009 fetch 1 (body[3.header])
a010 fetch 1 (body[3.mime])
a011 fetch 1 (body[3.mime]<10,50>)
a012 fetch 1 (body[3.1])
a013 fetch 1 (body[3.2])
a014 fetch 1 (body[4.1])
a015 fetch 1 (body[4.2.header])
a016 fetch 1 (body[4.2.1])
a017 fetch 1 (body[4.2.2.1])
a018 fetch 1 (body[4.2.2.2])
a019 fetch 1 (envelope)
a020 fetch 1 (body)
a021 fetch 1 (envelope body)
a022 fetch 1 (bodystructure)
a023 fetch 1 (rfc822.size)
a024 fetch 1 (all)
a025 fetch 1 (fast)
a026 fetch 1 (full)
a027 fetch 1 (rfc822.text)
a028 fetch 1 (body[header.fields(content-type)])
adone logout
EOF

rm -f confmdtest/cur/msg1:2,S

cat >confmdtest/new/msg2 <<EOF || exit 1
From: John <john@example.com>
To: Steve <steve@example.com>,
    Tom <tom@example.com>
Mime-Version: 1.0
Date: Wed, 22 Sep 1999 15:41:09 -0200
Content-Type: multipart/mixed; boundary="b1"
Subject: This is the message

foobar
--b1
Content-Type: text/plain

This is section 1

--b1
Content-Type: message/rfc822

Subject: This is message part 4.2
From: dave@example.org
To: tom@example.org
Cc: steve@example.org
Mime-Version: 1.0
content-type: text/plain
content-transfer-encoding: quoted-printable


M=41ry had a little lamb, it's fleece was white as snow.  And everywhere
Mary went, the lamb was sure to go.

--b1--
EOF
cat >confmdtest/new/msg3 <<EOF || exit 1
From: todd@example.org
To: kevin@example.org
Subject: today's meeting

Today's meeting has been cancelled
EOF
cat >confmdtest/new/msg4 <<EOF || exit 1
Subject: New MIME headers test
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="c1"
Content-Language: en


--c1
Content-Type: text/plain
Content-ID: <foo@bar>
Content-Description:  MIME test message
Content-MD5: aaaabbbb

test

--c1--
EOF
touch -m 0101000099 confmdtest/new/msg3
env MAILDIR=confmdtest ./imapd <<EOF | sed 's/UIDVALIDITY [0-9]*/UIDVALIDITY/;s/INTERNALDATE "[^"]*"/INTERNALDATE -DATE-/g'
b000 status inbox ( MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN)
b001 select inbox
b001a status inbox ( MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN)
b002 fetch 1:2 (flags uid)
b003 search header "Content-Type" "multipart"
b004 search 2 header "Content-Type" "multipart"
b005 search to tom@example
b006 search subject "message part 4.2"
b007 search before "1-Feb-1999"
b008 search on "1-Jan-1999"
b009 search senton "22-Sep-1999"
b010 search recent
b011 fetch 1 (rfc822.header)
b012 fetch 1:2 (flags)
b013 search seen
b014 search body "mary had a little lamb"
b015 uid search body "mary had a little lamb"
b016 store 1:2 +flags(\flagged)
b017 store 1 flags(\seen \deleted)
b018 expunge
b019 fetch 1 (flags uid)
b020 create inbox.bozo
b021 uid copy 3 inbox.bozo
b022 select inbox.bozo
b023 status inbox.bozo (uidnext)
b024 fetch 1 (flags)
b025 append inbox.bozo \Seen {11}
test

test

b026 append inbox.bozo (\Seen \Flagged) {11}
test

test

b027 noop
b028 fetch 1 flags
b029 fetch 1 flags
b030 select inbox
b031 fetch 2 (bodystructure)
adone logout
EOF
env MAILDIR=confmdtest ./imapd <<EOF | sed 's/UIDVALIDITY [0-9]*/UIDVALIDITY/;s/INTERNALDATE "[^"]*"/INTERNALDATE -DATE-/g'
foo select inbox.bozo
foo store 1 +flags \Deleted
foo expunge
foo logout
EOF
rm -rf confmdtest
mkdir confmdtest || exit 1
mkdir confmdtest/tmp || exit 1
mkdir confmdtest/cur || exit 1
mkdir confmdtest/new || exit 1
echo "10000S,2C" >confmdtest/maildirsize
env MAILDIR=confmdtest ./imapd <<EOF | sed 's/UIDVALIDITY [0-9]*/UIDVALIDITY/;s/INTERNALDATE "[^"]*"/INTERNALDATE -DATE-/g'
c001 select inbox
c002 append inbox {5}
test

c003 append inbox {5}
test

c004 append inbox {5}
test

c005 noop
c006 store 1 +flags \Deleted
c007 append inbox {5}
test

c008 noop
c009 append inbox {5}
test

c010 store 1 -flags \Deleted
cdone logout
EOF
cat confmdtest/maildirsize
rm -rf confmdtest
mkdir confmdtest || exit 1
mkdir confmdtest/tmp || exit 1
mkdir confmdtest/cur || exit 1
mkdir confmdtest/new || exit 1
echo "10000S,5C" >confmdtest/maildirsize
env MAILDIR=confmdtest ./imapd <<EOF | sed 's/UIDVALIDITY [0-9]*/UIDVALIDITY/;s/INTERNALDATE "[^"]*"/INTERNALDATE -DATE-/g'
d001 select inbox
d002 append inbox {5}
test

d003 noop
d004 copy 1 inbox
d005 noop
ddone logout
EOF
cat confmdtest/maildirsize
env MAILDIR=confmdtest ./imapd <<EOF | sed 's/UIDVALIDITY [0-9]*/UIDVALIDITY/;s/INTERNALDATE "[^"]*"/INTERNALDATE -DATE-/g'
e001 select inbox
e002 copy 1:2 inbox
e003 noop
edone logout
EOF
cat confmdtest/maildirsize
env MAILDIR=confmdtest ./imapd <<EOF | sed 's/UIDVALIDITY [0-9]*/UIDVALIDITY/;s/INTERNALDATE "[^"]*"/INTERNALDATE -DATE-/g'
f001 select inbox
f002 copy 1:2 inbox
f002 copy 1:2 inbox.Trash
f003 noop
f004 select inbox.Trash
f005 copy 1:2 inbox
f006 copy 1:2 inbox.Trash
f007 noop
f008 copy 1 inbox
f009 select inbox
fdone logout
EOF
cat confmdtest/maildirsize
echo "Counts:" `ls confmdtest/cur | wc -l` `ls confmdtest/.Trash/cur | wc -l`
env MAILDIR=confmdtest ./imapd <<EOF | sed 's/UIDVALIDITY [0-9]*/UIDVALIDITY/;s/INTERNALDATE "[^"]*"/INTERNALDATE -DATE-/g'
g001 select inbox
g002 select Trash
g003 select INBOX.Trash
g004 delete INBOX.Trash
g005 close
g006 delete INBOX.Trash
g007 create INBOX.a
g008 delete INBOX.a*
g009 delete INBOX.a
g010 create inbox.a*
gdone logout
EOF

rm -rf confmdtest2
../maildir/maildirmake confmdtest2
../maildir/maildirmake -f a confmdtest2
../maildir/maildirmake -f b confmdtest2
chmod u-w confmdtest2/.b/tmp
chmod u-w confmdtest2/.b/new
chmod u-w confmdtest2/.b/cur

echo "test `pwd`/confmdtest2" >confmdtest/shared-maildirs

env MAILDIR=confmdtest ./imapd <<EOF | sed 's/UIDVALIDITY [0-9]*/UIDVALIDITY/;s/INTERNALDATE "[^"]*"/INTERNALDATE -DATE-/g'
h001 list "" "*"
h002 list "" "%"
h003 list "" "%.%"
h004 list "" "%.%.%"
h005 list "shared" "*"
h006 list "shared.test" "*"
h007 list "INBOX" ""
h008 list "shared" ""
h009 list "shared.test" ""
h010 subscribe inbox
h011 list "" "*"
h012 lsub "" "*"
h013 subscribe shared.test.a
h014 list "" "*"
h015 lsub "" "*"
h016 subscribe shared.test.b
h017 list "" "*"
h018 lsub "" "*"
h019 unsubscribe shared.test.a
h020 list "" "*"
h021 lsub "" "*"
h022 subscribe shared.test.a
h023 list "" "*"
h024 lsub "" "*"
hdone logout
EOF

cat >confmdtest2/.a/new/msg1 <<EOF
Subject: message 1

message 1
EOF

cat >confmdtest2/.a/new/msg2 <<EOF
Subject: message 2

message 2
EOF

env MAILDIR=confmdtest ./imapd <<EOF | sed 's/UIDVALIDITY [0-9]*/UIDVALIDITY/;s/INTERNALDATE "[^"]*"/INTERNALDATE -DATE-/g'
i001 select shared.test.b
i002 select shared.test.a
i003 close
i004 select shared.test.a
i005 append shared.test.a {5}
test

i006 noop
i007 fetch 1:* (flags)
i008 select inbox
i009 copy 1:2 shared.test.a
i010 select shared.test.a
i011 fetch 1:* (flags)
i012 append shared.test.b {5}
i013 copy 1 shared.test.b
i014 store 1:2 +flags (\Deleted)
i015 expunge
idone logout
EOF
