#!/bin/sh
######################################################
#
# Test mhlist
#
######################################################

set -e

if test -z "${MH_OBJ_DIR}"; then
    srcdir=`dirname $0`/../..
    MH_OBJ_DIR=`cd $srcdir && pwd`; export MH_OBJ_DIR
fi

. "$MH_OBJ_DIR/test/common.sh"

setup_test

check_exit '-eq 1' mhlist -

expected=$MH_TEST_DIR/$$.expected
actual=$MH_TEST_DIR/$$.actual


start_test 'with no options and no current message'
run_test 'mhlist' 'mhlist: no cur message'

start_test 'with no options and current message'
folder +inbox 5 > /dev/null
cat > $expected <<EOF
 msg part  type/subtype              size description
   5       text/plain                  25
EOF
run_prog mhlist > $actual 2>&1
check $expected $actual 'keep first'

start_test '-noheaders'
run_test 'mhlist 5 -noheaders' '   5       text/plain                  25'

start_test '-headers'
run_prog mhlist -noheaders -headers > $actual 2>&1
check $expected $actual 'keep first'

start_test 'with folder and current message'
run_prog mhlist +inbox > $actual 2>&1
check $expected $actual 'keep first'

start_test 'with specified message'
run_prog mhlist 5 > $actual 2>&1
check $expected $actual 'keep first'

start_test '-file'
run_prog mhlist -file "$MH_TEST_DIR/Mail/inbox/5" > $actual 2>&1
check $expected $actual

start_test '-file -'
cat > $expected <<EOF
 msg part  type/subtype              size description
   0       text/plain                  25
EOF
run_prog mhlist -file - < "$MH_TEST_DIR/Mail/inbox/5" > $actual 2>&1
check $expected $actual

start_test 'message with no blank line separating the headers from the body.'
# Shouldn't make a difference to the size of the body part.
# Bug meant no-blank-line case was one bigger by including the `\n'
# ending the last header.
f=$MH_TEST_DIR/$$.blank
cat >$f-yes <<\E
foo: bar

body1
body2
E
sed '/^$/d' $f-yes >$f-no
run_prog mhlist -file $f-yes >$actual-yes
run_prog mhlist -file $f-no >$actual-no
check $actual-yes $actual-no
rm $f-yes $f-no

start_test 'message number greater than highest'
run_test 'mhlist 11' "mhlist: message 11 doesn't exist"

start_test 'multiple messages'
cat > $expected <<EOF
 msg part  type/subtype              size description
   2       text/plain                  25
   8       text/plain                  25
EOF
run_prog mhlist 2 8 > $actual 2>&1
check $expected $actual

# Write message with a text/plain subpart.
msgfile=`mhpath new`
cat > $msgfile <<EOF
To: recipient@example.com
From: sender@example.com
Subject: mhlist test
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0"

------- =_aaaaaaaaaa0
Content-Type: multipart/related;
    type="multipart/alternative";
    boundary="subpart__1.1"

--subpart__1.1
Content-Type: text/plain; charset="iso-8859-1"
Content-Disposition: attachment; filename="test1"

This is the first text/plain part, in a subpart.

--subpart__1.1--

------- =_aaaaaaaaaa0
Content-Type: text/plain; charset="iso-8859-1"
Content-Disposition: attachment; filename="test2"
Content-MD5: kq+Hnc2SD/eKwAnkFBDuEA==
Content-Transfer-Encoding: quoted-printable

This is the second text/plain part.

------- =_aaaaaaaaaa0
Content-Type: text/plain; charset="iso-8859-1"; name="test3"
Content-Disposition: attachment; filename="test3"

This is the third text/plain part.

------- =_aaaaaaaaaa0
Content-Type: text/plain; charset="iso-8859-1"
Content-Disposition: attachment; filename="test4"
Content-Transfer-Encoding: base64

VGhpcyBpcyB0aGUgZm91cnRoIHRleHQvcGxhaW4gcGFydC4K

------- =_aaaaaaaaaa0--
EOF

start_test '-part'
run_test 'mhlist last -part 3 -noheader' \
'  11       multipart/mixed            936
     1     multipart/related          180
     3     text/plain                  35'

start_test '-part of a subpart'
run_test 'mhlist last -part 1.1 -noheader' \
'  11       multipart/mixed            936
     1     multipart/related          180
     1.1   text/plain                  49'

start_test '-type'
run_test 'mhlist last -type text/plain -noheader' \
'  11       multipart/mixed            936
     1     multipart/related          180
     1.1   text/plain                  49
     2     text/plain                  36
     3     text/plain                  35
     4     text/plain                  36'

start_test '-norealsize'
run_test 'mhlist last -part 4 -noheader -norealsize' \
'  11       multipart/mixed            936
     1     multipart/related          180
     4     text/plain                  49'

start_test '-realsize, the default'
run_test 'mhlist last -part 4 -noheader -norealsize -realsize' \
'  11       multipart/mixed            936
     1     multipart/related          180
     4     text/plain                  36'

#
# Set the cur sequence to 1 to test default behavior of changecur after
# this test.
#

mark 1 -sequence cur -add -zero

start_test '-verbose'
run_test 'mhlist 11 -verbose -noheader' \
'  11       multipart/mixed            936
	     boundary="----- =_aaaaaaaaaa0"
     1     multipart/related          180
	     type="multipart/alternative"
	     boundary="subpart__1.1"
     1.1   text/plain                  49
	     charset="iso-8859-1"
     2     text/plain                  36
	     charset="iso-8859-1"
     3     text/plain                  35
	     charset="iso-8859-1"
	     name="test3"
     4     text/plain                  36
	     charset="iso-8859-1"'

#
start_test 'default changecur behavior'
#

run_test 'mark -sequence cur -list' 'cur: 11'

#
# Now test -nochangecur
#

mhlist -nochangecur 1 >/dev/null

run_test 'mark -sequence cur -list' 'cur: 11'

start_test 'MIME parser and m_getfld'
cat > $expected <<EOF
 msg part  type/subtype              size description
  12       multipart/related          204
     1     multipart/alternative       74
     1.1   text/plain                  17
     2     text/plain                  17
EOF

msgfile=`mhpath new`
msgnum=`basename $msgfile`
cat > $msgfile <<EOF
To: recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    recipient@example.com, recipient@example.com, recipient@example.com,
    re@example.com
From: sender@example.com
Subject: very long header
Date: Wed, 4 Dec 2013 21:12:00 -0500
MIME-Version: 1.0
Content-Type: multipart/related; boundary="1"

--1
Content-Type: multipart/alternative; boundary="2"

--2
Content-Type: text/plain; charset="us-ascii"

This is part 1.


--2--

--1
Content-Type: text/plain; charset="us-ascii"

This is part 2.


--1--
EOF

run_prog mhlist last > $actual 2>&1
check $expected $actual

##  now check mhlist output

msgfile=`mhpath new`
msgnum=`basename $msgfile`
cat > $msgfile <<EOF
To: example@example.org
From: someone <someone@example.com>
Subject: mhlist test
Date: Thu, 29 Jan 2015 18:12:21 +0000 (GMT)
Content-Type: multipart/mixed; boundary="BoundaryMixed"

--BoundaryMixed
Content-type: multipart/alternative;
 boundary="BoundaryAlternative1"

--BoundaryAlternative1
Content-type: text/enriched; CHARSET=US-ASCII
Content-transfer-encoding: 7bit

This is supposedly enriched.

--BoundaryAlternative1
Content-type: text/plain; charset=ISO-8859-1; format=flowed
Content-transfer-encoding: 8bit

This is the body text/plain part.

--BoundaryAlternative1
Content-type: text/html; CHARSET=US-ASCII
Content-transfer-encoding: 7bit

<html><body><div><span style="line-height: 1.5;">
This is the text/html body part.
</span></div></body></html>

--BoundaryAlternative1--

--BoundaryMixed
Content-type: multipart/alternative;
 boundary="BoundaryAlternative2"

--BoundaryAlternative2
Content-type: audio/wav
Content-transfer-encoding: 8bit

pretend wav audio

--BoundaryAlternative2
Content-type: audio/mp3
Content-transfer-encoding: 8bit

pretend mp3 audio

--BoundaryAlternative2
Content-type: audio/basic
Content-transfer-encoding: 8bit

pretend basic audio

--BoundaryAlternative2--

--BoundaryMixed--
EOF

# Write the expected output.
cat > $expected <<EOF
 msg part  type/subtype              size description
  13       multipart/mixed           1038
     1     multipart/alternative      518
     1.1   text/html                  111
     1.2   text/plain                  34
     1.3   text/enriched               29
     2     multipart/alternative      326
     2.1   audio/basic                 20
     2.2   audio/mp3                   18
     2.3   audio/wav                   18
EOF

start_test 'vanilla output'
run_prog mhlist $msgnum > $actual 2>&1
check "$expected" "$actual"

# Write the expected output.
cat > $expected <<EOF
 msg part  type/subtype              size description
  13       multipart/mixed           1038
     1     multipart/alternative      518
     1.1   text/html                  111
     1.2   text/plain                  34
     1.3   text/enriched               29
     2     multipart/alternative      326
EOF

start_test 'just part 1'
run_prog mhlist -part 1 $msgnum > $actual 2>&1
check "$expected" "$actual"

# Write the expected output.
cat > $expected <<EOF
 msg part  type/subtype              size description
  13       multipart/mixed           1038
     1     multipart/alternative      518
     2     multipart/alternative      326
EOF

start_test 'ask for audio/basic in part 1, should get nothing'
run_prog mhlist -part 1 -type audio/basic $msgnum > $actual 2>&1
check "$expected" "$actual"

# Write the expected output.
cat > $expected <<EOF
 msg part  type/subtype              size description
  13       multipart/mixed           1038
     1     multipart/alternative      518
     1.1   text/plain                  34
     1.2   text/html                  111
     1.3   text/enriched               29
     2     multipart/alternative      326
     2.1   audio/wav                   18
     2.2   audio/basic                 20
     2.3   audio/mp3                   18
EOF

start_test 'bump plain and wav to the top of their parts'
run_prog mhlist -prefer text/plain -prefer audio/wav $msgnum > $actual 2>&1
check "$expected" "$actual"

# Write the expected output.
cat > $expected <<EOF
 msg part  type/subtype              size description
  13       multipart/mixed           1038
     1     multipart/alternative      518
     1.1   text/enriched               29
     1.2   text/plain                  34
     1.3   text/html                  111
     2     multipart/alternative      326
     2.1   audio/basic                 20
     2.2   audio/mp3                   18
     2.3   audio/wav                   18
EOF

start_test 'bump plain and enriched to the top.  add a bogus type as well.'
run_prog mhlist -prefer foo/bar -prefer text/plain -prefer text/enriched $msgnum > $actual 2>&1
check "$expected" "$actual"

start_test 'binary message/rfc822'
cat >"$expected" <<EOF
 msg part  type/subtype              size description
  14       multipart/mixed            341
     1     message/rfc822             157
EOF
cat > `mhpath new` <<EOF
From: Test <test@example.com>
To: Some User <user@example.com>
Date: Fri, 29 Sep 2006 00:00:00
Subject: message with binary message/rfc822 attachment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0"

and some garbage before the attachment

------- =_aaaaaaaaaa0
Content-Type: message/rfc822; name="1552"; charset="us-ascii"
Content-Transfer-Encoding: binary

From: Test <test@example.com>
To: <another_user@example.com>
Date: Thu, 28 Sep 2006 00:00:00
Subject: message/rfc822 attachment

This is an RFC-822 message.

------- =_aaaaaaaaaa0--
EOF

run_prog mhlist last > $actual 2>&1
check "$expected" "$actual"


start_test 'quoted-printable message/rfc822'
cat >"$expected" <<EOF
mhlist: "message/rfc822" type in message 15 should be encoded in 7bit, 8bit, or binary continuing...
EOF
cat > `mhpath new` <<EOF
From: Test <test@example.com>
To: Some User <user@example.com>
Date: Fri, 29 Sep 2006 00:00:00
Subject: message with quoted-printable message/rfc822 attachment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0"

and some garbage before the attachment

------- =_aaaaaaaaaa0
Content-Type: message/rfc822; name="1552"; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

From: Test <test@example.com>
To: <another_user@example.com>
Date: Thu, 28 Sep 2006 00:00:00
Subject: message/rfc822 attachment

This is an RFC-822 message.

------- =_aaaaaaaaaa0--
EOF

! run_prog mhlist last > $actual 2>&1
check "$expected" "$actual"


start_test '8bit message/external-body'
cat >"$expected" <<EOF
mhlist: "message/external-body" type in message 16 should be encoded in 7bit continuing...
EOF
cat > `mhpath new` <<EOF
From: Test <test@example.com>
To: Some User <user@example.com>
Date: Fri, 29 Sep 2006 00:00:00
Subject: message with 8bit message/external-body attachment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0"

and some garbage before the attachment

------- =_aaaaaaaaaa0
Content-Type: message/external-body; name="1552"; charset="us-ascii"
Content-Id: ext-id
Content-Transfer-Encoding: 8bit

From: Test <test@example.com>
To: <another_user@example.com>
Date: Thu, 28 Sep 2006 00:00:00
Subject: message/rfc822 attachment

This is an RFC-822 message.

------- =_aaaaaaaaaa0--
EOF

! run_prog mhlist last > $actual 2>&1
check "$expected" "$actual"


finish_test
exit $failed
