#!/bin/sh
######################################################
#
# Test charset conversion functionality
#
######################################################

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' mhshow -

require_locale en_US.UTF-8 en_US.UTF8 en_US.utf-8 en_US.utf8

iconv_elides_question_marks=0
if test "$ICONV_ENABLED" -eq 0; then
    text_size=10
else
    text_size=11
    #### The GNU iconv library normalises charset names by eliding '?', along
    #### with some other characters.  The iconv libraries used on FreeBSD/NetBSD
    #### and Mac OS X don't.
    printf x | iconv -f '?UTF-8' -t UTF-8 >/dev/null 2>&1  &&
        iconv_elides_question_marks=1
fi

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

echo 'mhshow-charset-windows-1252: echo "This is a test"' >> "$MH"

# Write message with bogus quoted-printable data.
start_test 'Write message with bogus quoted-printable data'
msgfile=`mhpath new`
msgnum=`basename $msgfile`
cat > $msgfile <<EOF
From: foo@example.edu
To: bar@example.edu
Subject: test
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: quoted-printable
Content-Description: bogus quoted-printable data
Date: Sun, 18 Dec 2005 00:52:39 +0100

4 =F7 2 =3D 2
EOF

cat > $expected <<EOF
[ Message inbox:11 ]
Date:    Sun, 18 Dec 2005 00:52:39 +0100
To:      bar@example.edu
From:    foo@example.edu
Subject: test

MIME-Version: 1.0
Content-Description: bogus quoted-printable data

[ part  - text/plain - bogus quoted-printable data  ${text_size}B  ]
This is a test
EOF

run_prog mhshow $msgnum > $actual 2>&1
check "$expected" "$actual" 'keep first' : bogus quoted-printable data


start_test 'locale profile component'
#### Used LC_ALL above, now use locale profile component instead.
printf "locale: $LC_ALL\n" >> "${MH}"
unset LC_ALL

run_prog mhshow $msgnum > $actual 2>&1
check "$expected" "$actual" : locale profile component


#### All of the remaining tests rely on iconv.a
if test "$ICONV_ENABLED" -eq 0; then
  finish_test
  exit $failed
fi


#### RFC 2231 defines the extended value syntax:
#### extended-initial-value := [charset] "'" [language] "'"
####                           extended-other-values
#### used below, where language is blank.

start_test 'Encoded parameter value'
msgfile=`mhpath new`
msgnum=`basename $msgfile`
#### Use a charset to exercise the iconv code, if it's available,
#### in get_param_value().  The Content-Type charset is UTF-8, encoded
#### to exercise iconv.
cat > $msgfile <<EOF
From: foo@example.edu
To: bar@example.edu
Subject: test
MIME-Version: 1.0
Content-Type: text/plain; charset*=ISO-2022-KR''%1b%24%29%43%55%54%46%2d%38
Content-Transfer-Encoding: quoted-printable
Content-Description: bogus quoted-printable data
Date: Sun, 18 Dec 2005 00:52:39 +0100

4 =C3=B7 2 =3D 2
EOF

cat > $expected <<EOF
[ Message inbox:12 ]
Date:    Sun, 18 Dec 2005 00:52:39 +0100
To:      bar@example.edu
From:    foo@example.edu
Subject: test

MIME-Version: 1.0
Content-Description: bogus quoted-printable data

[ part  - text/plain - bogus quoted-printable data  11B  ]
4 ÷ 2 = 2
EOF

run_prog mhshow $msgnum > $actual 2>&1
check "$expected" "$actual" : encoded parameter value


#### mhshow replaces the invalid character in the charset parameter value with
#### a '?'.  So, a charset name of "?us-ascii" is fed to iconv.  If
#### $iconv_elides_question_marks, iconv will normalize that to "us-ascii".
#### Otherwise, we expect iconv to fail.
start_test 'replacement character in parameter value'
cat > $msgfile <<'EOF'
Subject: invalid parameter value charset
MIME-Version: 1.0
Content-Type: text/plain; charset*=invalid''%0Dus-ascii
EOF

cat > $expected <<EOF
[ Message inbox:12 ]
Subject: invalid parameter value charset

MIME-Version: 1.0

[ part  - text/plain -   0B  ]
EOF

run_prog mhshow $msgnum > $actual 2>&1
if [ $iconv_elides_question_marks -eq 1 ]; then
    check "$expected" "$actual"
else
    if grep "Can't convert .*us-ascii to UTF-8" "$actual" >/dev/null; then
        rm -f "$expected" "$actual"
    else
        failed=1
    fi
fi


finish_test
exit $failed
