#!/usr/bin/env bash

# convert-mailman-to-utf8: convert mailman language packs to UTF-8 charset and
# apply some patches
# Copyright (C) 2014 Maciej Delmanowski <drybjed@gmail.com>
# Homepage: https://debops.org/


# This program is free software; you can redistribute
# it and/or modify it under the terms of the
# GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# This program is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General
# Public License along with this program; if not,
# write to the Free Software Foundation, Inc., 59
# Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# An on-line copy of the GNU General Public License can
# be downloaded from the FSF web page at:
# https://www.gnu.org/copyleft/gpl.html


# mailman package in Debian Wheezy (version 2.1.15-1) comes with many language
# packs written in charsets other than UTF-8. This has been already fixed in
# Debian Jessie (https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=668304), but
# not entirely, and at the moment backport to Wheezy is unlikely. In Debian
# Jessie there are now some mailman packages which contain misleading encoding
# information: files are encoded in UTF-8 but the contents say that the charset
# is different, which produces misencoded content in browsers that interpret
# them.
#
# This script will help you convert mailman language packs to UTF-8, and fix
# small issues along the way. Conversion of the language packs should be
# propagated to mailman upstream (I'm not sure if Mailman developers will
# accept changes in mailman 2.x) or at least to Debian.
#
# Script requires patch and gettext packages.
#
# Usage:
#
#    convert-mailman-to-utf8 <lang> [lang] ... [lang]
#
# lang - two letter language name to convert

convert_to_utf8 () {
    local from_encoding="${1}"
    shift

    for filename in "${@}" ; do
        if [ -e "${filename}" ] ; then
            local file_encoding
            file_encoding="$(file -b --mime-encoding "${filename}")"

            if [ "${file_encoding}" != "utf-8" ] && [ "${file_encoding}" != "us-ascii" ] ; then

                echo "Converting ${filename} from ${file_encoding} to UTF-8"
                iconv -f "${from_encoding}" -t utf-8 -o "${filename}.new" "${filename}"
                mv "${filename}.new" "${filename}"

            fi

            if [ "${from_encoding}" = "iso8859-2" ] ; then
                if grep -q -E 'charset=[Ii][Ss][Oo]-8859-2' "${filename}" ; then
                    echo "Updating the MIME charset string in ${filename}"
                    sed -i "s/charset=[Ii][Ss][Oo]-8859-2/charset=utf-8/" "${filename}"
                fi
            fi

        fi
    done
}

# Convert Polish language pack
convert_polish () {

    convert_to_utf8 iso8859-2 /usr/share/mailman/pl/* /etc/mailman/pl/* /var/lib/mailman/messages/pl/LC_MESSAGES/mailman.po

    if [ -r /var/lib/mailman/messages/pl/LC_MESSAGES/mailman.po ] ; then

        cd /var/lib/mailman/messages/pl/LC_MESSAGES || exit 1

        read -r -d '' mailman_messages_patch <<EOF
--- mailman.po
+++ mailman.po
@@ -1370,7 +1370,7 @@
 "    confirmation step."
 msgstr ""
 "Wprowadź kod potwierdzający,\\n"
-"    który otrzymałeś w emailu. Następnie naciśnij <em>OK</em>,\\n"
+"    który otrzymałeś w emailu. Następnie naciśnij <em>OK</em>,"
 
 #: Mailman/Cgi/confirm.py:214
 msgid "Confirmation string:"
@@ -1610,7 +1610,7 @@
 "    Naciśnij <em>Wypisz</em>, aby potwierdzić\\n"
 "    wypisanie się.\\n"
 "\\n"
-"    <p>Kliknij <em>Anuluj</em>, aby zrezygnować.\\n"
+"    <p>Kliknij <em>Anuluj</em>, aby zrezygnować."
 
 #: Mailman/Cgi/confirm.py:491 Mailman/Cgi/options.py:767
 #: Mailman/Cgi/options.py:911 Mailman/Cgi/options.py:921
EOF

        if patch -p0 -N --dry-run --silent <<< "${mailman_messages_patch}" > /dev/null 2>&1 ; then
            patch -p0 -N <<< "${mailman_messages_patch}"
            msgfmt mailman.po -o mailman.mo
        fi

    fi

    if [ -r /usr/lib/mailman/Mailman/Defaults.py ] ; then

        cd /usr/lib/mailman || exit 1

        read -r -d '' mailman_defaults_patch <<EOF
--- Mailman/Defaults.py
+++ Mailman/Defaults.py
@@ -1509,7 +1509,7 @@
 add_language('lt',    _('Lithuanian'),          'iso-8859-13', 'ltr')
 add_language('nl',    _('Dutch'),               'iso-8859-1',  'ltr')
 add_language('no',    _('Norwegian'),           'iso-8859-1',  'ltr')
-add_language('pl',    _('Polish'),              'iso-8859-2',  'ltr')
+add_language('pl',    _('Polish'),              'utf-8',       'ltr')
 add_language('pt',    _('Portuguese'),          'iso-8859-1',  'ltr')
 add_language('pt_BR', _('Portuguese (Brazil)'), 'iso-8859-1',  'ltr')
 add_language('ro',    _('Romanian'),            'iso-8859-2',  'ltr')
EOF

        if patch -p0 -N --dry-run --silent <<< "${mailman_defaults_patch}" > /dev/null 2>&1 ; then
            patch -p0 -N <<< "${mailman_defaults_patch}"
        fi

    fi

}

for lang in "${@}"; do
    case ${lang} in
        pl)   shift ; convert_polish ;;
        *)    shift ;;
    esac
done
