#!/usr/bin/env python3

# Copyright © 2012-2017 Jakub Wilk <jwilk@jwilk.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the “Software”), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import re
import sys
import urllib.request

sys.path[0] += '/..'

from lib import gettext
from lib import ling

def strip_plural_forms(s):
    # Remove superfluous parentheses around the plural formula:
    return re.sub(r'\bplural=\((.*?)\);$', r'plural=\1;', s)

def do_plurals():
    regexp = re.compile(r'\s+{\s*"([a-zA-Z_]+)".*"(nplurals.*?)"')
    url = 'https://git.savannah.gnu.org/cgit/gettext.git/plain/gettext-tools/src/plural-table.c'
    okay = set()
    with urllib.request.urlopen(url) as file:
        for line in file:
            line = line.decode('ASCII').rstrip()
            match = regexp.match(line)
            if match is None:
                continue
            language, gettext_plural_forms = match.groups()
            language = ling.parse_language(language)
            gettext_plural_forms = strip_plural_forms(gettext_plural_forms)
            our_plural_forms = language.get_plural_forms() or []
            if gettext_plural_forms not in our_plural_forms:
                print('[{}]'.format(language))
                if our_plural_forms:
                    if len(our_plural_forms) == 1:
                        print('# plural-forms =', *our_plural_forms)
                    else:
                        print('# plural-forms =')
                        for pf in our_plural_forms:
                            print('# ', pf)
                print('plural-forms =', gettext_plural_forms)
                print()
            else:
                okay.add(str(language))
    n_okay = len(okay)
    print('# No plural-forms changes required for {} languages'.format(n_okay))
    print()

blacklist = {
    # https://lists.gnu.org/archive/html/bug-gettext/2015-06/msg00057.html
    ('pap', 'AN'),
}

def do_languages():
    url = 'https://git.savannah.gnu.org/cgit/gettext.git/plain/gettext-tools/src/msginit.c'
    okay = set()
    with urllib.request.urlopen(url) as file:
        contents = file.read()
    match = re.compile(br'locales_with_principal_territory\s*\[\]\s*=\s*[{](.*?)[}]', re.DOTALL).search(contents)
    if match is None:
        raise ValueError
    contents = match.group(1)
    contents = re.compile(br'/[*].*?[*]/', re.DOTALL).sub(b'', contents)
    contents = contents.decode('ASCII')
    data = {}
    primary_languages = set(ling.get_primary_languages())
    for item in re.findall(r'"(\w+)"', contents):
        language, gettext_cc = item.split('_')
        if language not in primary_languages:
            continue
        data[language] = gettext_cc
        our_cc = ling.parse_language(language).get_principal_territory_code()
        if our_cc != gettext_cc:
            if (language, gettext_cc) in blacklist:
                if our_cc is not None:
                    okay.add(language)
                continue
            print('[{}]'.format(language))
            if our_cc is not None:
                print('# WAS: principal-territory =', our_cc)
            if ling.lookup_territory_code(gettext_cc) is None:
                print('# BAD: principal-territory =', gettext_cc)
            else:
                print('principal-territory =', gettext_cc)
            print()
        else:
            okay.add(language)
    for language in sorted(primary_languages):
        if '_' in language:
            continue
        cc = ling.parse_language(language).get_principal_territory_code()
        if cc is None:
            continue
        if language not in data:
            print('# WAS: [{}]'.format(language))
            print('# WAS: principal-territory =', cc)
            print()
    n_okay = len(okay)
    print('# No principal-territory changes required for {} languages'.format(n_okay))
    print()

def do_string_formats():
    url = 'https://git.savannah.gnu.org/cgit/gettext.git/plain/gettext-tools/src/message.c'
    with urllib.request.urlopen(url) as file:
        contents = file.read()
    match = re.compile(br'\sformat_language\s*\[NFORMATS\]\s*=\s*[{](.*?)[}]', re.DOTALL).search(contents)
    if match is None:
        raise ValueError
    contents = match.group(1)
    contents = re.compile(br'/[*].*?[*]/', re.DOTALL).sub(b'', contents)
    contents = contents.decode('ASCII')
    their_formats = frozenset(
        re.findall(r'"([\w-]+)"', contents)
    )
    our_formats = frozenset(
        gettext.string_formats.keys()
    )
    difference = their_formats ^ our_formats
    print('[formats]')
    for fmt in difference:
        if fmt in our_formats:
            print('# MISSING: {fmt} = ...'.format(fmt=fmt))
        else:
            print('{fmt} = # TODO'.format(fmt=fmt))
    n_okay = len(our_formats - difference)
    print('# No changes required for {} string formats'.format(n_okay))
    print()

def main():
    do_plurals()
    do_languages()
    do_string_formats()

if __name__ == '__main__':
    main()

# vim:ts=4 sts=4 sw=4 et
