#!/bin/sh
#
# Check to see if the scan output is correct if we have a message
# right on the stdio buffer boundary
#

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

thisdir="$srcdir/test/inc"
expected="$MH_TEST_DIR/$$.expected"
actual="$MH_TEST_DIR/$$.actual"

cat > "${MH_TEST_DIR}/test.mbox" <<EOF
From nobody@nowhereville Jan 1 1970
Received: I am the very model of a modern Major-General,
Received: I've information vegetable, animal, and mineral,
Received: I know the kings of England, and I quote the fights historical
Received: From Marathon to Waterloo, in order categorical;
Received: I'm very well acquainted, too, with matters mathematical,
Received: I understand equations, both the simple and quadratical,
Received: About binomial theorem I'm teeming with a lot o' news,
Received: With many cheerful facts about the square of the hypotenuse.
Received: I'm very good at integral and differential calculus;
Received: I know the scientific names of beings animalculous:
Received: In short, in matters vegetable, animal, and mineral,
Received: I am the very model of a modern Major-General.
Received: I know our mythic history, King Arthur's and Sir Caradoc's;
Received: I answer hard acrostics, I've a pretty taste for paradox,
Received: I quote in elegiacs all the crimes of Heliogabalus,
Received: In conics I can floor peculiarities parabolous;
Received: I can tell undoubted Raphaels from Gerard Dows and Zoffanies,
Received: I know the croaking chorus from The Frogs of Aristophanes!
Received: Then I can hum a fugue of which I've heard the music's din afore,c
Received: And whistle all the airs from that infernal nonsense Pinafore.
Received: Then I can write a washing bill in Babylonic cuneiform,
Received: And tell you ev'ry detail of Caractacus's uniform:d
Received: In short, in matters vegetable, animal, and mineral,
Received: I am the very model of a modern Major-General.
Received: In fact, when I know what is meant by "mamelon" and "ravelin",
Received: When I can tell at sight a Mauser rifle from a Javelin,
Received: When such affairs as sorties and surprises I'm more wary at,
Received: And when I know precisely what is meant by "commissariat",
Received: When I have learnt what progress has been made in modern gunnery,
Received: When I know more of tactics than a novice in a nunnery—
Received: In short, when I've a smattering of elemental strategy—
Received: You'll say a better Major-General has never sat a gee.
Received: For my military knowledge, though I'm plucky and adventury,
Received: Has only been brought down to the beginning of the century;
Received: But still, in matters vegetable, animal, and mineral,
Received: I am the very model of a modern Major-General. 
Received: For the Angel of Death spread his wings on the blast,
Received: And breathed in the face of the foe as he passed:
Received: the eyes of the sleepers waxed deadly and chill,
Received: And their hearts but once heaved, and for ever grew still!
Received: Abbott: Strange as it may seem, they give ball players nowadays very peculiar names.
Received: Costello: Funny names?
Received: Abbott: Nicknames, nicknames. Now, on the St. Louis team we have Who's on first, What's on second, I Don't Know is on third--
Received: Costello: That's what I want to find out. I want you to tell me the names of the fellows on the St. Louis team.
Received: Abbott: I'm telling you. Who's on first, What's on second, I Don't Know is on third--
Received: Costello: You know the fellows' names?
Received: Abbott: Yes.
Received: Costello: Well, then who's playing first?
Received: Abbott: Yes.
Received: Costello: I mean the fellow's name on first base.
Received: Abbott: Who.
Received: Costello: The fellow playin' first base.
Received: Abbott: Who.
Received: Costello: The guy on first base.
Received: Abbott: Who is on first.
Received: Costello: Well, what are you askin' me for?
Received: Abbott: I'm not asking you--I'm telling you. Who is on first.
Received: Costello: I'm asking you--who's on first?
Received: Abbott: That's the man's name.
Received: Costello: That's who's name?
Received: Abbott: Yes.
Received: Costello: When you pay off the first baseman every month, who gets the money?
X-Spam-Source: Definitely spam!  Seriously!
From: Mr Nobody <nobody1@example.com>
To: Another Nobody <nobody2@example.com>
Date: Thu, 1 Mar 2012 01:02:03 -0500
Subject: Who is on first?

Abbott: Every dollar of it. And why not, the man's entitled to it.
Costello: Who is?
Abbott: Yes.
Costello: So who gets it?
Abbott: Why shouldn't he? Sometimes his wife comes down and collects it.
Costello: Who's wife?
Abbott: Yes. After all, the man earns it.
Costello: Who does?
Abbott: Absolutely.
Costello: Well, all I'm trying to find out is what's the guy's name on first base?
Abbott: Oh, no, no. What is on second base.
Costello: I'm not asking you who's on second.
Abbott: Who's on first!
EOF

cat > "${MH_TEST_DIR}/test.mbox.2" <<EOF
Received: I am the very model of a modern Major-General,
Received: I've information vegetable, animal, and mineral,
Received: I know the kings of England, and I quote the fights historical
Received: From Marathon to Waterloo, in order categorical;
Received: I'm very well acquainted, too, with matters mathematical,
Received: I understand equations, both the simple and quadratical,
Received: About binomial theorem I'm teeming with a lot o' news,
Received: With many cheerful facts about the square of the hypotenuse.
Received: I'm very good at integral and differential calculus;
Received: I know the scientific names of beings animalculous:
Received: In short, in matters vegetable, animal, and mineral,
Received: I am the very model of a modern Major-General.
Received: I know our mythic history, King Arthur's and Sir Caradoc's;
Received: I answer hard acrostics, I've a pretty taste for paradox,
Received: I quote in elegiacs all the crimes of Heliogabalus,
Received: In conics I can floor peculiarities parabolous;
Received: I can tell undoubted Raphaels from Gerard Dows and Zoffanies,
Received: I know the croaking chorus from The Frogs of Aristophanes!
Received: Then I can hum a fugue of which I've heard the music's din afore,c
Received: And whistle all the airs from that infernal nonsense Pinafore.
Received: Then I can write a washing bill in Babylonic cuneiform,
Received: And tell you ev'ry detail of Caractacus's uniform:d
Received: In short, in matters vegetable, animal, and mineral,
Received: I am the very model of a modern Major-General.
Received: In fact, when I know what is meant by "mamelon" and "ravelin",
Received: When I can tell at sight a Mauser rifle from a Javelin,
Received: When such affairs as sorties and surprises I'm more wary at,
Received: And when I know precisely what is meant by "commissariat",
Received: When I have learnt what progress has been made in modern gunnery,
Received: When I know more of tactics than a novice in a nunnery—
Received: In short, when I've a smattering of elemental strategy—
Received: You'll say a better Major-General has never sat a gee.
Received: For my military knowledge, though I'm plucky and adventury,
Received: Has only been brought down to the beginning of the century;
Received: But still, in matters vegetable, animal, and mineral,
Received: I am the very model of a modern Major-General. 
Received: For the Angel of Death spread his wings on the blast,
Received: And breathed in the face of the foe as he passed:
Received: the eyes of the sleepers waxed deadly and chill,
Received: And their hearts but once heaved, and for ever grew still!
Received: Abbott: Strange as it may seem, they give ball players nowadays very peculiar names.
Received: Costello: Funny names?
Received: Abbott: Nicknames, nicknames. Now, on the St. Louis team we have Who's on first, What's on second, I Don't Know is on third--
Received: Costello: That's what I want to find out. I want you to tell me the names of the fellows on the St. Louis team.
Received: Abbott: I'm telling you. Who's on first, What's on second, I Don't Know is on third--
Received: Costello: You know the fellows' names?
Received: Abbott: Yes.
Received: Costello: Well, then who's playing first?
Received: Abbott: Yes.
Received: Costello: I mean the fellow's name on first base.
Received: Abbott: Who.
Received: Costello: The fellow playin' first base.
Received: Abbott: Who.
Received: Costello: The guy on first base.
Received: Abbott: Who is on first.
Received: Costello: Well, what are you askin' me for?
Received: Abbott: I'm not asking you--I'm telling you. Who is on first.
Received: Costello: I'm asking you--who's on first?
Received: Abbott: That's the man's name.
Received: Costello: That's who's name?
Received: Abbott: Yes.
Received: Costello: When you pay off the first baseman every month, who gets the money?
X-Spam-Source: Definitely spam!  Seriously!
From: Mr Nobody <nobody1@example.com>
To: Another Nobody <nobody2@example.com>
Date: Thu, 1 Mar 2012 01:02:03 -0500
Subject: Who is on first?

Abbott: Every dollar of it. And why not, the man's entitled to it.
Costello: Who is?
Abbott: Yes.
Costello: So who gets it?
Abbott: Why shouldn't he? Sometimes his wife comes down and collects it.
Costello: Who's wife?
Abbott: Yes. After all, the man earns it.
Costello: Who does?
Abbott: Absolutely.
Costello: Well, all I'm trying to find out is what's the guy's name on first base?
Abbott: Oh, no, no. What is on second base.
Costello: I'm not asking you who's on second.
Abbott: Who's on first!
EOF

run_test "inc -file ${MH_TEST_DIR}/test.mbox -width 120 -truncate" \
	"Incorporating new mail into inbox...

  11+ 03/01 Mr Nobody          Who is on first?<<Abbott: Every dollar of it. And why not, the man's entitled to it. Cos"

check `mhpath +inbox 11` "${MH_TEST_DIR}/test.mbox.2" "keep first"
rm -f "${MH_TEST_DIR}/test.mbox" "${MH_TEST_DIR}/test.mbox.2"

run_test "scan -width 120 +inbox 11" \
	"  11+ 03/01 Mr Nobody          Who is on first?<<Abbott: Every dollar of it. And why not, the man's entitled to it. Cos"

# check header field name with ':' character than lands on m_getfld() buffer
cat >"$MH_TEST_DIR/mess" <<EOF
Comments: The important characteristic of this message is that the 4097'th
Comments: byte is the ':' at the end of header field name.  That tests some
Comments: logic in m_getfld (), assuming its MSG_INPUT_SIZE is 4096.
Comments: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Comments: BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
Comments: CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
Comments: DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
Comments: EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
Comments: FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
Comments: GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
Comments: HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
Comments: IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
Comments: JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
Comments: KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK
Comments: LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
Comments: MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
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Comments: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
Comments: cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
Comments: dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
Comments: eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
Comments: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
Comments: gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
Comments: hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
Comments: iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
Comments: jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
Comments: kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Comments: llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll
Comments: mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
Comments: nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
Comments: oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
Comments: pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
Comments: qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
Comments: rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Comments: ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
Comments: tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
Comments: uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
Comments: vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Comments: wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
Comments: This is it, the line with the 4097'th character of ':'.
Comments: From somewhere
From: No Such User <nosuch@example.com>
To: Some Other User <someother@example.com>
Subject: all that and nothing to say?
Date: Thu, 10 Jan 2013 19:54:01 -0500

EOF

run_test 'scan -width 60 -file '"$MH_TEST_DIR/mess" \
         '   1  01/10 No Such User       all that and nothing to say?'
rm -f "$MH_TEST_DIR/mess"

# check m_getfld() handling of fields with trailing whitespace
# Even though header field names aren't supposed to have spaces (RFC
# 28220, m_getfld () trims trailing whitespace from them.
cat >`mhpath new` <<EOF
Date : Sun, 18 Dec 2005 00:52:39 +0100
From	: foo@example.edu
To: bar@example.edu
Subject: test
EOF
run_test 'scan last' '  12  12/18 foo@example.edu    test'

# check header field body with more than 511 characters
cat >"$MH_TEST_DIR/mess" <<EOF
Comments: The important characteristic of this message is that this header
          field body is more than 511 bytes long.  That tests the logic in
          m_getfld () for transitioning to the FLDPLUS state.  This is the
          only use for the FLDPLUS state, handling header field bodies that
          are longer than the buffer used to retrieve them.  The trailing
          newline after the last character is the 512th character.  The 511th
          character, counting all spaces, even the leading one, is*
From: No Such User <nosuch@example.com>
To: Some Other User <someother@example.com>
Subject: FLDPLUS test
Date: Wed, 16 Jan 2013 20:33:58 -0600

EOF

run_test 'scan -width 80 -file '"$MH_TEST_DIR"'/mess' \
         '   1  01/16 No Such User       FLDPLUS test'
rm -f "$MH_TEST_DIR/mess"

# check inc of message with header field body with more than 511 characters
cat >"$expected" <<EOF
Comments: The important characteristic of this message is that this header
          field body is more than 511 bytes long.  That tests the logic in
          m_getfld () for transitioning to the FLDPLUS state.  This is the
          only use for the FLDPLUS state, handling header field bodies that
          are longer than the buffer used to retrieve them.  The trailing
          newline after the last character is the 512th character.  The 511th
          character, counting all spaces, even the leading one, is*
          And, it continues onto this line.
From: No Such User <nosuch@example.com>
To: Some Other User <someother@example.com>
Subject: FLDPLUS test
Date: Wed, 16 Jan 2013 20:33:58 -0600
EOF

cat >"$MH_TEST_DIR/mess" <<EOF
From nosuch@example.com  Wed 16 Jan 2013 20:33:58 -0600
Comments: The important characteristic of this message is that this header
          field body is more than 511 bytes long.  That tests the logic in
          m_getfld () for transitioning to the FLDPLUS state.  This is the
          only use for the FLDPLUS state, handling header field bodies that
          are longer than the buffer used to retrieve them.  The trailing
          newline after the last character is the 512th character.  The 511th
          character, counting all spaces, even the leading one, is*
          And, it continues onto this line.
From: No Such User <nosuch@example.com>
To: Some Other User <someother@example.com>
Subject: FLDPLUS test
Date: Wed, 16 Jan 2013 20:33:58 -0600

EOF
run_prog inc -file "$MH_TEST_DIR/mess" >/dev/null
check "$expected" `mhpath last`  ||  rmm last
rm -f "$MH_TEST_DIR/mess"

# check scan and inc of mbox with multiple messages
echo y | packf -file "$MH_TEST_DIR/msgbox"
run_test "scan -width 80 -file $MH_TEST_DIR/msgbox" \
'   1  09/29 Test1              Testing message 1<<This is message number 1 >>
   2  09/29 Test2              Testing message 2<<This is message number 2 >>
   3  09/29 Test3              Testing message 3<<This is message number 3 >>
   4  09/29 Test4              Testing message 4<<This is message number 4 >>
   5  09/29 Test5              Testing message 5<<This is message number 5 >>
   6  09/29 Test6              Testing message 6<<This is message number 6 >>
   7  09/29 Test7              Testing message 7<<This is message number 7 >>
   8  09/29 Test8              Testing message 8<<This is message number 8 >>
   9  09/29 Test9              Testing message 9<<This is message number 9 >>
  10  09/29 Test10             Testing message 10<<This is message number 10 >>
  11  03/01 Mr Nobody          Who is on first?<<Abbott: Every dollar of it. An
  12  12/18 foo@example.edu    test'
run_test "inc -width 80 -file $MH_TEST_DIR/msgbox -truncate" \
'Incorporating new mail into inbox...

  13+ 09/29 Test1              Testing message 1<<This is message number 1 >>
  14  09/29 Test2              Testing message 2<<This is message number 2 >>
  15  09/29 Test3              Testing message 3<<This is message number 3 >>
  16  09/29 Test4              Testing message 4<<This is message number 4 >>
  17  09/29 Test5              Testing message 5<<This is message number 5 >>
  18  09/29 Test6              Testing message 6<<This is message number 6 >>
  19  09/29 Test7              Testing message 7<<This is message number 7 >>
  20  09/29 Test8              Testing message 8<<This is message number 8 >>
  21  09/29 Test9              Testing message 9<<This is message number 9 >>
  22  09/29 Test10             Testing message 10<<This is message number 10 >>
  23  03/01 Mr Nobody          Who is on first?<<Abbott: Every dollar of it. An
  24  12/18 foo@example.edu    test'
rm -f  "$MH_TEST_DIR/msgbox"

# check mbox with two blank lines between header and body
cat >>"$MH_TEST_DIR/msgbox" <<EOF
From 
Date: Mon, 15 Apr 2013 21:02:12 -0500
From: me@example.com


test
EOF

run_test "inc -width 60 -file $MH_TEST_DIR/msgbox -truncate" \
'Incorporating new mail into inbox...

  25+ 04/15 me@example.com     <<test >>'
rm -f  "$MH_TEST_DIR/msgbox"


exit ${failed:-0}
