#!/bin/sh
######################################################
#
# Test UTF-8 body along with an attachment
#
######################################################

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
testname="${MH_TEST_DIR}/$$"
expected="$MH_TEST_DIR/$$.expected"
actual="$MH_TEST_DIR/$$.actual"

require_locale $en_locales

#### Rely on sendmail/pipe below to override default mts.
mts_fakesendmail="${MHMTSCONF}-fakesendmail"
cp "${MHMTSCONF}" "$mts_fakesendmail"
printf 'sendmail: %s/test/fakesendmail\n' "$srcdir" >>"$mts_fakesendmail"
MHMTSCONF="$mts_fakesendmail"

# argument: expected output
test_attachment ()
{
  run_prog send -draft -mts sendmail/pipe

  # fakesendmail drops the message and any cc's into this mbox.
  mbox="${MH_TEST_DIR}"/Mail/fakesendmail.mbox
  inc -silent -file "$mbox"
  rm -f "$mbox"

  #
  # It's hard to calculate the exact Date: header post is going to
  # use, so we'll just use sed to remove the actual date so we can easily
  # compare it against our "correct" output.  And same for charset,
  # because mime_type() can rely on file(1) --mime-encoding.
  #
  sed -e 's/^Date:.*/Date:/' -e 's/; charset="[^;]*//' \
      "`mhpath cur`" > "${testname}.actual"

  check "${testname}.actual" "$1"
}


# check that 7-bit body isn't encoded
start_test "7-bit body isn't encoded"
cat > "${MH_TEST_DIR}/attachment.txt" <<EOF
this is an attachment
EOF

cat > "${MH_TEST_DIR}/Mail/draft" <<EOF
From: Mr Nobody <nobody@example.com>
To: Somebody <somebody@example.com>
Subject: Test
Nmh-Attach: $MH_TEST_DIR/attachment.txt

This is a test.
EOF

cat > "${testname}.expected" <<EOF
From: Mr Nobody <nobody@example.com>
To: Somebody <somebody@example.com>
Subject: Test
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0"
Date:

------- =_aaaaaaaaaa0
Content-Type: text/plain

This is a test.

------- =_aaaaaaaaaa0
Content-Type: text/plain; name="attachment.txt"
Content-Description: attachment.txt
Content-Disposition: attachment; filename="attachment.txt"

this is an attachment

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

test_attachment "${testname}.expected"


# check that 8-bit body is encoded as quoted-printable text/plain,
# even with an attachment
start_test '8-bit body is encoded as quoted-printable text/plain'
cat > "${MH_TEST_DIR}/Mail/draft" <<EOF
From: Mr Nobody <nobody@example.com>
To: Somebody <somebody@example.com>
Subject: Test
Nmh-Attach: $MH_TEST_DIR/attachment.txt

¡Ay, caramba!
EOF

cat > "${testname}.expected" <<EOF
From: Mr Nobody <nobody@example.com>
To: Somebody <somebody@example.com>
Subject: Test
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0"
Content-Transfer-Encoding: 8bit
Date:

------- =_aaaaaaaaaa0
Content-Type: text/plain
Content-Transfer-Encoding: 8bit

¡Ay, caramba!

------- =_aaaaaaaaaa0
Content-Type: text/plain; name="attachment.txt"
Content-Description: attachment.txt
Content-Disposition: attachment; filename="attachment.txt"

this is an attachment

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

test_attachment "${testname}.expected"


# check that 8-bit attachment gets C-T-E
start_test '8-bit attachment gets C-T-E'
rm -f "${MH_TEST_DIR}/attachment.txt"
cat > "${MH_TEST_DIR}/attachment.txt" <<EOF
8-bit attachment, ¡Ay, caramba!
EOF

cat > "${MH_TEST_DIR}/Mail/draft" <<EOF
From: Mr Nobody <nobody@example.com>
To: Somebody <somebody@example.com>
Subject: Test
Nmh-Attach: $MH_TEST_DIR/attachment.txt
EOF

cat > "${testname}.expected" <<EOF
From: Mr Nobody <nobody@example.com>
To: Somebody <somebody@example.com>
Subject: Test
MIME-Version: 1.0
Content-Type: text/plain; name="attachment.txt"
Content-Description: attachment.txt
Content-Disposition: attachment; filename="attachment.txt"
Content-Transfer-Encoding: 8bit
Date:

8-bit attachment, ¡Ay, caramba!
EOF

test_attachment "${testname}.expected"


# check 8-bit body but make sure that the locale is set to C, which
# should cause mhbuild to fail
start_test '8-bit body with C locale'
cat > "${MH_TEST_DIR}/Mail/draft" <<EOF
From: Mr Nobody <nobody@example.com>
To: Somebody <somebody@example.com>
Subject: Test
Nmh-Attach: $MH_TEST_DIR/attachment.txt

¡Ay, caramba!
EOF

found_locale=$LC_ALL   # Set by require_locale().
LC_ALL="C"; export LC_ALL

set +e
run_prog send -draft -mts sendmail/pipe > "$actual" 2>&1
set -e

cat > "$expected" <<EOF
mhbuild: Text content contains 8 bit characters, but character set is US-ASCII
${MH_INST_DIR}${bindir}/mhbuild: exited 1
EOF

check "$expected" "$actual"


# previous test, but with locale profile entry
start_test '8-bit body with C locale with locale profile entry'

printf 'locale: %s\n' "$found_locale" >>"${MH}"

cat > "${testname}.expected" <<EOF
From: Mr Nobody <nobody@example.com>
To: Somebody <somebody@example.com>
Subject: Test
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0"
Content-Transfer-Encoding: 8bit
Date:

------- =_aaaaaaaaaa0
Content-Type: text/plain
Content-Transfer-Encoding: 8bit

¡Ay, caramba!

------- =_aaaaaaaaaa0
Content-Type: text/plain; name="attachment.txt"
Content-Description: attachment.txt
Content-Disposition: attachment; filename="attachment.txt"
Content-Transfer-Encoding: 8bit

8-bit attachment, ¡Ay, caramba!

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

test_attachment "${testname}.expected"


rm -f ${MHMTSCONF} "${MH_TEST_DIR}/attachment.txt"

finish_test
exit ${failed:-0}

# emacs hackage to ensure that it writes the inverted exclamation
# point as UTF-8 multibyte character \xC2\xA1 instead of just \xA1.
# Local Variables:
# coding: utf-8
# End:
