#! /usr/bin/env python3

# Copyright © Michal Čihař <michal@weblate.org>
#
# SPDX-License-Identifier: MIT

import json
import os
import subprocess

subprocess.check_call(
    [
        "php",
        "./modules/cldr-to-gettext-plural-rules/bin/export-plural-rules",
        "--reduce=no",
        "--output=cldr.json",
        "json",
    ]
)

with open("cldr.json", "rb") as handle:
    LANGUAGES = json.load(handle)

os.unlink("cldr.json")

with open("cldr.csv", "w") as handle:
    handle.write("code,name,nplurals,formula\n")
    for code in sorted(LANGUAGES):
        data = LANGUAGES[code]
        handle.write(
            "{},{},{},{}\n".format(code, data["name"], data["plurals"], data["formula"])
        )
