#!/bin/sh
######################################################
#
# Test long header line handling (998 chars)
#
######################################################

set -e

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

genlongsubject() {
	len="${1:-998}"
	awk -v len="$len" 'BEGIN {
		prefix = "Subject: " len " "
		while (i++<len) {
			s = s "x"
		}
		print prefix substr(s, length(prefix) + 1)
	}'
}

addcr() {
	awk '{printf($0 "\r\n")}'
}


# Ensure lines won't get folded
echo "mhl: -width 2000" >> "$MMH/profile"
trap 'printf "/^mhl:/d\nw\nq\n" | ed - "$MMH/profile"' 0 1 2 15


len=997

# Write message with long header line
msgfile=$(mhpath b)
msgnum=$(basename $msgfile)
cat > $msgfile <<-EOF
From: foo@example.edu
To: bar@example.edu
`genlongsubject $len`
Date: Sun, 15 Jul 2018 12:26:59 +0200

foo
EOF

# check it
cat > $expected <<-EOF
Date:    Sun, 15 Jul 2018 12:26:59 +0200
From:    foo@example.edu
To:      bar@example.edu
`genlongsubject $len`

part       text/plain                   4
foo
EOF

COLUMNS=2000 show $msgnum > $actual 2>&1
diff -u $expected $actual


len=998

# Write message with long header line
msgfile=$(mhpath b)
msgnum=$(basename $msgfile)
cat > $msgfile <<-EOF
From: foo@example.edu
To: bar@example.edu
`genlongsubject $len`
Date: Sun, 15 Jul 2018 12:26:59 +0200

foo
EOF

# check it
cat > $expected <<-EOF
Date:    Sun, 15 Jul 2018 12:26:59 +0200
From:    foo@example.edu
To:      bar@example.edu
`genlongsubject $len`

part       text/plain                   4
foo
EOF

COLUMNS=2000 show $msgnum > $actual 2>&1
diff -u $expected $actual


len=999

# Write message with long header line
msgfile=$(mhpath b)
msgnum=$(basename $msgfile)
cat > $msgfile <<-EOF
From: foo@example.edu
To: bar@example.edu
`genlongsubject $len`
Date: Sun, 15 Jul 2018 12:26:59 +0200

foo
EOF

# check it
cat > $expected <<-EOF
mhl: format error in message 13
part       text/plain                   4
foo
EOF

COLUMNS=2000 show $msgnum > $actual 2>&1
diff -u $expected $actual



# now with CRLF


len=998

# Write message with long header line
msgfile=$(mhpath b)
msgnum=$(basename $msgfile)
cat > $msgfile <<-EOF
From: foo@example.edu
To: bar@example.edu
`genlongsubject $len | addcr`
Date: Sun, 15 Jul 2018 12:26:59 +0200

foo
EOF

# check it
cat > $expected <<-EOF
Date:    Sun, 15 Jul 2018 12:26:59 +0200
From:    foo@example.edu
To:      bar@example.edu
`genlongsubject $len`

part       text/plain                   4
foo
EOF

COLUMNS=2000 show $msgnum > $actual 2>&1
diff -u $expected $actual


len=999

# Write message with long header line
msgfile=$(mhpath b)
msgnum=$(basename $msgfile)
cat > $msgfile <<-EOF
From: foo@example.edu
To: bar@example.edu
`genlongsubject $len | addcr`
Date: Sun, 15 Jul 2018 12:26:59 +0200

foo
EOF

# check it
cat > $expected <<-EOF
mhl: format error in message 15
part       text/plain                   4
foo
EOF

COLUMNS=2000 show $msgnum > $actual 2>&1
diff -u $expected $actual

