#!/usr/bin/python3

# Copyright © 2012, 2013 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 datetime
import os
import re
import sys
import urllib.request

basedir = os.path.join(
    os.path.dirname(__file__),
    os.pardir,
)
sys.path[:0] = [basedir]

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 = 'http://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()
            if our_plural_forms != [gettext_plural_forms]:
                print('[{}]'.format(language))
                if our_plural_forms != None:
                    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()

def do_languages():
    url = 'http://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('"(\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:
            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()

template = '''\
# This file has been generated automatically by private/update-gettext-data.
# Do not edit.
# Last update: {today}
'''

def do_string_formats():
    url = 'http://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')
    formats = re.findall('"([\w-]+)"', contents)
    path = os.path.join(basedir, 'data', 'string-formats')
    with open(path + '.tmp', 'wt', encoding='ASCII') as stdout:
        print(template.format(today=datetime.date.today()), file=stdout)
        for item in sorted(formats):
            print(item, file=stdout)
    os.rename(path + '.tmp', path)
    print('# Saved {} string formats'.format(len(formats)))

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

if __name__ == '__main__':
    main()

# vim:ts=4 sw=4 et
