#!/usr/bin/env python3
"""This script is designed to help with the Debian FreeDict packaging. It can
fetch all available databases from
https://www.freedict.org/freedict-database.xml
and generate a orig source tar ball or generate debian/control and
debian/copyright.

Please use `--help` to find out more details.

IMPORTANT: you must run this script from the root of the freedict source,
otherwise the operations will fail.

Generating debian/copyright and debian/control
==============================================

Since FreeDict packages contain a lot of very similar dictionaries, the process
is made more convenient by generating the mentioned files. d/control is made up
of a d/control.HEAD file (make sure this one exists), usually with the normal
source stanza and followed by auto-generated stanzas, derived from information
of the debian/freedict-database.xml. The XML API file is fetched automatically,
if required.

The d/copyright file is generated using licensecheck and a bit of guessing
logic. The file d/copyright.snippets/TAIL is appended and usually contains the
licence definition stanzas. If the copyright file contains "FIXME", the
maintainer may also like to create a file d/copyright.snippets/xxx-yyy plain
text As soon as such a file is found, no licence information will be queried for
the specified dictionary.
"""

# pylint: disable=multiple-imports,too-few-public-methods,global-statement
from collections import defaultdict
from pathlib import Path

import argparse
import datetime
import json
import os
import re
import shutil
import subprocess
import sys
import textwrap
import urllib.request
import xml.etree.ElementTree as ET

from packaging.version import Version

XML_URL = "http://www.freedict.org/freedict-database.xml"
XML_API_PATH = "debian/freedict-database.xml"
ISO_639_TABLE = Path("/usr/share/iso-codes/json/iso_639-3.json")
__DICT2LONGNAME = {}  # filled by dictionarycode2longdescription


def dictionarycode2longdescription(string):
    """Translate a dictionary code into a human-readable string.
    A dictionary is encoded using a hyphen-delimited pair of ISO 639-3 codes.
    This function translates this into a language pair of English names,
    delimited by a hyphen."""
    global __DICT2LONGNAME
    if not __DICT2LONGNAME:
        __DICT2LONGNAME = parse_iso_table()
    string = string.split("-")
    return __DICT2LONGNAME[string[0]] + "-" + __DICT2LONGNAME[string[1]]


def parse_iso_table():
    """Map ISO 639-3 codes and bibliographic aliases to English names from iso-codes."""
    with ISO_639_TABLE.open(encoding="utf-8") as source:
        languages = json.load(source)["639-3"]
    names = {}
    for language in languages:
        name = re.sub(r"\s*\(.*?\)\s*", "", language["name"])
        names[language["alpha_3"]] = name
        if "bibliographic" in language:
            names[language["bibliographic"]] = name
    return names


def get_xml_content(fetch_new=False):
    """Either read contents of freedict-database.xml or fetch it from the web
first and save it."""
    if fetch_new or not os.path.exists(XML_API_PATH):
        print(f"Downloading API from {XML_URL}")
        with urllib.request.urlopen(XML_URL) as req:
            data = req.read().decode("utf-8")
        with open(XML_API_PATH, "w", encoding="UTF-8") as file:
            file.write(data)
    else:  # read from file
        with open(XML_API_PATH, encoding="UTF-8") as file:
            data = file.read()
    return data

def run_licensecheck_on(file_path):
    """Try to extract licence with licence check."""
    with subprocess.Popen(
            ["licensecheck", "--copyright", "--deb-fmt", file_path], stdout=subprocess.PIPE
            ) as proc:
        try:
            text = proc.communicate()[0].decode("utf-8")
            text = text.lstrip().split("\n", maxsplit=1)[0]
        except IndexError:
            return None
    sre = re.search(r"^.+: (.+)$", text)
    if not sre:
        sre = re.search(r"^.No copyright. (.+)$", text.split("\n")[0])
    if not sre:
        return None
    match = sre.groups()[0]
    return (None if "UNKNOWN" in match else match)

def normalise_text(node):
    """Join text across child elements and collapse whitespace to single spaces."""
    return " ".join("".join(node.itertext()).split())


def read_header(path):
    """Read through the closing TEI header tag, keeping local tag names and omitting notes."""
    with path.open("rb") as source:
        for event, node in ET.iterparse(source, events=("start", "end")):
            node.tag = node.tag.rsplit("}", 1)[-1]
            if event == "end" and node.tag == "notesStmt":
                node.clear()
            if event == "end" and node.tag == "teiHeader":
                return node
            if event == "start" and node.tag in ("text", "body"):
                break
    raise ValueError(f"{path}: missing TEI header")


def person_name(node):
    """Strip contact details from a TEI name and return an empty string for placeholders."""
    name = normalise_text(node).partition("<")[0].strip()
    name = re.split(r",\s*[^, ]+ (?:at|@) ", name, flags=re.I)[0]
    return "" if name.startswith("[") else name


def identities(header):
    """Yield (identifier, name) pairs from the TEI header.

    Read xml:id from name, persName and author elements. A missing xml:id
    yields None so the name is still available for first-name or surname lookup.
    Also pair a revision change's sole who reference with its sole name child,
    stripping the leading # from the reference.
    """
    for tag in ("name", "persName", "author"):
        for node in header.iter(tag):
            name = person_name(node)
            if name:
                yield node.get("{http://www.w3.org/XML/1998/namespace}id"), name
    for change in header.findall(".//revisionDesc//change"):
        ids = change.get("who", "").split()
        names = change.findall("name")
        if len(ids) == len(names) == 1 and person_name(names[0]):
            yield ids[0].lstrip("#"), person_name(names[0])


class HeaderCache:
    """Cache TEI headers by path and index contributor names on demand."""

    def __init__(self):
        self.headers = {}
        self.by_id = defaultdict(set)
        self.by_token = defaultdict(set)
        self.indexed = False

    def get(self, path):
        path = Path(path)
        if path not in self.headers:
            self.headers[path] = read_header(path)
        return self.headers[path]

    def resolve(self, handle, local):
        """Return the unique contributor name for a handle, or None.

        Look up the ID in the current dictionary's header first, then in the
        TEI headers of all language-pair dictionaries in the working directory.
        Match the handle as a first name or surname across those dictionaries
        if the ID is absent, or among its candidates if they disagree.
        """
        key = handle.lstrip("#")
        matches = local.get(key)
        if not matches:
            if not self.indexed:
                for path in Path(".").glob("???-???/*.tei"):
                    if path.stem != path.parent.name:
                        continue
                    for ident, name in identities(self.get(path)):
                        if ident:
                            self.by_id[ident].add(name)
                        words = name.casefold().split()
                        for token in (words[0], words[-1]):
                            self.by_token[token].add(name)
                self.indexed = True
            matches = self.by_id.get(key)
            if not matches:
                matches = self.by_token.get(key.casefold())
            elif len(matches) > 1:
                matches = matches.intersection(self.by_token.get(key.casefold(), ()))
        if matches and len(matches) == 1:
            return next(iter(matches))


def acknowledgements(change):
    """Yield names and affiliations from explicit 'thanks to' revision credits, best effort."""
    for fragment in change.itertext():
        text = " ".join(fragment.split())
        for match in re.finditer(r"\bthanks to ((?:Dr\. )?[^();.!?]+)", text, re.I):
            yield re.split(r",?\s+for\s+", match[1], maxsplit=1, flags=re.I)[0].strip()


def date_collective_notice(notices, changes, fallback):
    """Date the collective credit using copyright notices and TEI revision dates.

    Keep each named holder's dates. Use the packaging fallback years only
    when neither the notices nor the revisions supply a year.
    """
    year = r"\b[0-9]{4}\b"
    span = rf"{year}(?:\s*[-–—]+\s*{year})?"
    collective = re.compile(
        rf"(?<!\S)(?:(?P<period>{span})[ ,]+)?(?:by )?(?:various authors|FreeDict)\b", re.I
    )
    years = []
    for notice in notices:
        text = re.sub(r"https?://\S+|\S*@\S+", "", notice)
        years.extend(re.findall(year, text))
    for change in changes:
        years.extend(re.findall(year, change.get("when", "")))
        for date in change.iter("date"):
            years.extend(re.findall(year, date.get("when") or normalise_text(date)))

    if not any(collective.search(notice) for notice in notices):
        notices.append(fallback)
    if not years:
        years = re.findall(year, fallback)
    if years:
        first, last = min(years), max(years)
        period = first if first == last else f"{first}-{last}"
        for index, notice in enumerate(notices):
            match = collective.search(notice)
            if match:
                if match["period"]:
                    start, end = match.span("period")
                    notices[index] = notice[:start] + period + notice[end:]
                else:
                    notices[index] = notice[:match.start()] + period + " " + notice[match.start():]


def copyright_fields(header, cache, fallback):
    """Return Debian Copyright and optional Comment fields for one TEI header.

    Combine notices in availability and sourceDesc with contributors named in
    the title statement and revision history. Add the fallback attribution
    unless a notice already credits FreeDict or various authors. Use the cache
    to resolve revision handles. Credit people thanked in revisions as holders
    and put unresolved handles in Comment.
    """
    holders, comments = [], []
    marker = re.compile(r"(?:copyright\s*:?\s*(?:\(c\))?|©|\(c\))\s*", re.I)
    for section in header.findall(".//availability") + header.findall(".//sourceDesc"):
        for node in section.iter():
            if node.tag not in ("p", "item") or node.find(".//item") is not None:
                continue
            statement = normalise_text(node)
            if marker.match(statement):
                holders.extend(part.strip(" ;.") for part in marker.split(statement)
                               if part.strip(" ;."))
    changes = header.findall(".//revisionDesc//change")
    date_collective_notice(holders, changes, fallback)

    for path in (".//titleStmt/author", ".//titleStmt/respStmt/name"):
        holders.extend(person_name(node) for node in header.findall(path))
    local = defaultdict(set)
    for ident, name in identities(header):
        if ident:
            local[ident].add(name)
    for change in changes:
        for node in (change.findall("name") + change.findall(".//head/name")
                     + change.findall(".//persName")):
            holders.append(person_name(node))
        for handle in change.get("who", "").split():
            name = cache.resolve(handle, local)
            if name:
                holders.append(name)
            else:
                comments.append(f"Revision contributor: {handle}")
        for item in change.findall(".//item"):
            # A revision item may also name publications and conferences.
            # Keep names explicitly introduced as authors in running prose.
            preceding = item.text or ""
            for child in item:
                if child.tag == "name" and re.search(r"\bby\s*$", preceding, re.I):
                    holders.append(person_name(child))
                preceding = child.tail or ""
            credit, colon, _ = normalise_text(item).partition(":")
            for name in credit.split(" and ") if colon else ():
                words = name.replace("'", "").replace("-", "").split()
                if len(words) >= 2 and all(w.isalpha() and w[0].isupper()
                                           and not w.isupper() for w in words):
                    holders.append(name)
        holders.extend(acknowledgements(change))
    fields = ["Copyright: " + "\n ".join(dict.fromkeys(filter(None, holders)))]
    if comments:
        fields.append("Comment: " + "\n ".join(dict.fromkeys(comments)))
    return "\n".join(fields) + "\n"


def header_licence(header, fallback):
    """Refine a generic licensecheck result using the publication terms."""
    best = fallback
    for availability in header.findall(".//availability"):
        for statement in availability.findall(".//p") or [availability]:
            text = normalise_text(statement).casefold()
            links = " ".join(ref.get("target", "") for ref in statement.iter()).casefold()
            gpl = re.search(r"\bgpl\b|gnu general public licen[cs]e", text)
            url_version = re.search(r"/gpl-(\d+)(?:\.0)?\b", links)
            if gpl or url_version:
                # A paragraph may introduce a second licence after the GPL terms.
                terms = text[gpl.end():] if gpl else text
                terms = re.split(r"creative commons|\battribution-share", terms)[0]
                version = re.search(r"(?:ver(?:sion|\.)?\s*|^\s*[-v])([123])\b", terms)
                version = version or url_version
                if version:
                    best = "GPL-" + version[1]
                    if re.search(r"\blater version\b|\bor later\b|\d\+", terms):
                        return best + "+"
                elif best == "FIXME":
                    best = "GPL"
            else:
                cc = re.search(r"attribution[- ]share\s*alike|\bcc[- ]by-sa\b", text)
                cc_url = re.search(r"/licenses/by-sa/(\d+(?:\.\d+)?)", links)
                if cc or cc_url:
                    version = cc_url
                    if cc:
                        version = re.match(r"[ ,]*(?:licen[cs]e[, ]*)?(?:version\s*)?"
                                           r"\(?v?(\d+(?:\.\d+)?)", text[cc.end():]) or cc_url
                    return "CC-BY-SA" + ("-" + version[1] if version else "")
    return best


def find_licence(dictionary, header):
    """Find out licence of dictionary.
First run licence-check, afterwards use a self-brewed licence checker.
Currently only GPL and CC-BY-SA is detected, else FIXMe is output. It tries to output
something like GPL, GPL-2, GPL-3, GPL-2+, GPL-3+"""


    tei_fn = "{0}{1}{0}.{2}".format(dictionary.name, os.sep, "tei")
    licence = run_licensecheck_on(tei_fn)
    if not licence:
        licence = run_licensecheck_on(dictionary.name + os.sep + "COPYING.tei")

        if not licence:
            licence = "FIXME"  # backup solution

    NO_COPYRIGHT_BY_LICENSECHECK = "*No copyright* "
    licence = licence.removeprefix(NO_COPYRIGHT_BY_LICENSECHECK)

    # For GPL without version/options, we need to perform additional work.
    if licence not in ("FIXME", "GPL"):  # found a licence, return
        return licence

    return header_licence(header, licence)


def recursive_text(node):
    text = ""
    if node.text.strip() != "":
        text = node.text
    for child in node:
        text += "\n" + recursive_text(child)
    return text

class Dictionary:
    """A `named tuple` to represent a dictionary from the API."""
    def __init__(self, name):
        self.name = name
        self.headwords, self.version = None, None
        self.status, self.maintainer, self.source_release = None, None, None

    def set(self, key, val):
        """Map the XML attribute names  to the internal name."""
        match key:
            case "headwords": self.headwords = val
            case "edition": self.version = val
            case "status": self.status = val
            case "maintainerName": self.maintainer = val
            case _: raise ValueError(f"invalid key: {key}")

class Action:
    """An action to perform on the FD API."""
    def __init__(self, package_name, dictionaries):
        self.dictionaries = dictionaries
        # name of the debian (source) package
        self.package_name = package_name

    def perform_action(self):
        pass

class GenerateControlCopyright(Action):
    def __init__(self, package_name, dictionaries, no_desc_version):
        super().__init__(package_name, dictionaries)
        # keep/strip version number from pkg description
        self.__desc_version = not no_desc_version

    def perform_action(self):
        """Write both control as well as the copyright file."""
        self.write_control()
        self.write_copyright()

    def gen_control_for_dictionary(self, platform, dictionary, depends=None,
            recommends = None, suggests=None, provides=None):
        """Generate a list of tokens that, if joined by "" resembles a packet description."""
        tokens = [f"\nPackage: {platform}-freedict-{dictionary.name}\n"]
        human_readable_platform = None
        match platform:
            case "dict":
                human_readable_platform = "DICT format"
            case "stardict":
                human_readable_platform = "StarDict format"
            case _:
                raise ValueError("Unknown platform while generating control "
                        f"file: {platform}")
        tokens.append("Architecture: all\n")
        tokens.append("Depends: ${misc:Depends}")
        tokens.append(f" {depends}" if depends else "\n")
        if recommends:
            tokens.append(f"Recommends: {recommends}\n")
        if suggests:
            tokens.append(f"Suggests: {suggests}\n")
        if provides:
            tokens.append(f"Provides: {provides}\n")
        status = ""
        if dictionary.status:
            status = f" (FreeDict status: {dictionary.status})"
        longname = dictionarycode2longdescription(dictionary.name)
        tokens.append(
            f"Description: {longname} dictionary in the {human_readable_platform}\n"
        )
        version = ""
        if self.__desc_version:
            version = f", version {dictionary.version}"
        longdesc = (
            f"This is the {longname} dictionary from the FreeDict "
            f"project{version}. It contains {dictionary.headwords} "
            f"headwords{status}. The {human_readable_platform} can be used "
            "with various dictionary applications."
        )
        # format description to 80 characters per line
        tokens.append(
            "\n".join(
                textwrap.wrap(
                    longdesc, width=79, initial_indent=" ", subsequent_indent=" "
                )
            )
        )
        tokens.append("\n")
        return tokens

    def write_control(self):
        """Generate debian/control from debian/control.HEAD and the gathered
        dictionary data."""
        head = Path("debian/control.HEAD").read_text(encoding="utf-8")
        tokens = [head.rstrip(), "\n\n"]

        for dictionary in self.dictionaries:
            tokens.extend(self.gen_control_for_dictionary("dict", dictionary,
                    suggests = ("dictd | dicod, dict | goldendict "
                        "| dictionaryreader.app"),
                    provides = "dictd-dictionary"))
            tokens.extend(self.gen_control_for_dictionary("stardict",
                    dictionary,
                    recommends = ("sdcv (>= 0.5~) | stardict (>= 3.0~) "
                        "| stardict-gtk (>= 3.0~) | goldendict-ng | qstardict")))

        with open("debian/control", "w", encoding="UTF-8") as file:
            file.write("".join(tokens))

    def write_copyright(self):
        """Generate debian/copyright from debian/copyright.HEAD and the gathered
        dictionary data."""
        upstream_last_touched = int(
            subprocess.check_output(["dpkg-parsechangelog", "-S", "version"])
            .decode(sys.getdefaultencoding())
            .split(".", 1)[0]
            .strip()
        )
        cprght_snippets = "{1}{0}{2}{0}".format(os.sep, "debian", "copyright.snippets")
        HEAD = open(cprght_snippets + "HEAD", encoding="utf-8").read()
        string = [HEAD, "\n"]
        headers = HeaderCache()
        for dictionary in self.dictionaries:
            # is there a snippet for an exception to licensecheck?
            exception_snippet = cprght_snippets + dictionary.name
            if os.path.exists(exception_snippet):
                with open(exception_snippet) as f:
                    string.append("\n" + f.read())
            else:
                string.append(f"\nFiles: {dictionary.name}/*\n")
                header = headers.get(Path(dictionary.name) / f"{dictionary.name}.tei")
                fallback = f"2000-{upstream_last_touched} FreeDict contributors"
                string.append(copyright_fields(header, headers, fallback))
                string.append(f"License: {find_licence(dictionary, header)}\n")
        with open(cprght_snippets + "TAIL", encoding="UTF-8") as f:
            string += ["\n\n", f.read()]

        document = "".join(string)
        with open("debian/copyright", "w", encoding="utf-8") as f:
            f.write(document)
        if "FIXME" in document:
            print(
                'NOTE: some licences could not be extracted, search for "FIXME" in debian/copyright.'
            )


class FetchSource(Action):
    """Fetch the sources of all dictionaries and the tools directory."""

    def __init__(self, package_name, dictionaries):
        super().__init__(package_name, dictionaries)
        self.date = self.gen_date()
        self.dirname = f"{package_name}-{self.date}.orig"
        self.exclude_dictionaries = []
        if len(sys.argv) == 4:  # there's the -x option given
            if sys.argv[2] == "-x":
                self.exclude_dictionaries = sys.argv[3].split(" ")

    def gen_date(self):
        """Return date in format "yyyy.mm.dd"."""
        d = datetime.datetime.now()
        return (
            str(d.year)
            + "."
            + str(d.month).zfill(2).replace(" ", "0")
            + "."
            + str(d.day).zfill(2).replace(" ", "0")
        )

    def prepare_environment(self):
        """Perform all actions which are needed before downloading the
        source.
        """
        if os.path.exists(self.dirname):
            print(f"Removing {self.dirname}; possibly left over from an interrupted run.")
            shutil.rmtree(self.dirname)
        os.mkdir(self.dirname)
        os.chdir(self.dirname)

    def clean_up(self):
        """
        Compress the original source, move it to the right destination and
        remove download directory."""
        tarname = f"{self.package_name}_{self.date}.orig.tar.xz"
        os.chdir("..")
        ret = subprocess.call(["tar", "cJf", tarname, self.dirname])
        if ret:
            sys.exit(9)
        print("Moving tar archive upward to.", os.path.join("..", tarname))
        os.rename(tarname, ".." + os.sep + tarname)
        shutil.rmtree(self.dirname)

    def perform_action(self):
        """Download all upstream source packages."""
        self.prepare_environment()
        imported = 0
        tarname = f"{self.package_name}_{self.date}.orig.tar.xz"
        for dictionary in self.dictionaries:
            if dictionary.name in self.exclude_dictionaries:
                print(f"Skip {dictionary.name} (specified via commmand line)")
                continue
            elif dictionary.source_release is None:
                print(f"Skip {dictionary.name} (no source release)")
                continue
            fname = dictionary.source_release.split("/")[-1]
            print(f"Fetching {dictionary.name} from {dictionary.source_release}")
            try:
                with urllib.request.urlopen(dictionary.source_release) as u:
                    data = u.read()
            except urllib.error.HTTPError as h:
                if int(h.code) == 404:
                    reason = f"{h!s}: {dictionary.source_release}"
                    raise urllib.error.URLError(reason) from None
                raise h from None

            with open(fname, "wb") as f:
                f.write(data)
            print("Extracting", fname)
            if fname.endswith(".zip"):
                os.system('unzip -qq "%s"' % fname)
            elif any(
                fname.endswith(suf) for suf in (".tar.bz2", ".tar.gz", ".tar.xz")
            ):
                subprocess.run(["tar", "xf", fname], check=True)
            else:
                print(f'E: unknown format of "{fname}".')
                sys.exit(0)

            subprocess.run(["tar", "xf", fname], check=True)
            os.remove(fname)
            imported += 1
        print("Imported %d dictionaries." % imported)
        self.clean_up()

class CheckForMoreRecentDicts(Action):
    """Check whether the downstream copies are outdated."""
    def __init__(self, package_name, dictionaries):
        super().__init__(package_name, dictionaries)

    def perform_action(self):
        """Check whether any dictionary is more recent upstream."""
        version_regex = re.compile("<edition>(.+?)</edition>")
        def version_of(v):
            return Version(v.split("-")[0])
        updates_required = []
        for dictionary in self.dictionaries:
            version = None
            with open(f"{dictionary.name}/{dictionary.name}.tei",
                    encoding="UTF-8") as file:
                for line in file:
                    match = version_regex.search(line)
                    if match:
                        version = match.groups()[0]
                        break
            if not version:
                raise ValueError(f"unable to get version from {dictionary.name}")
            if version_of(version) < version_of(dictionary.version):
                updates_required.append((dictionary, version))
        if updates_required:
            print("Updates required for:")
            for dictionary, old_version in updates_required:
                print(f"  {dictionary.name}: old {old_version}, new {dictionary.version}")
        else:
            print("All dictionaries up-to-date.")

class Criteria:
    """A criteria matcher: Use the string
    sourceURL:wikdict
    to filter for all dictionaries fwith wikdict in their URL and use
    sourceURL!wikdict
    to filter for all  dictionaries not derived from wikdict. The first part is
    an XML attribute name, the second bit is a regular expression.
    This class can cope with empty criteria: all matches will be true."""

    def __init__(self, criteria):
        self.__rgx = None
        self.__delim = None
        self.__criteria_attr = None
        if criteria and (not ":" in criteria and not "!" in criteria):
            raise ValueError(
                "criteria needs to consist of a dictionary "
                "attribute followed by `:`  or `!` followed by a regular "
                "expression"
            )
        if criteria:
            self.__delim = ":" if ":" in criteria else "!"
            self.__criteria_attr, self.__rgx = criteria.split(self.__delim, 1)
            self.__rgx = re.compile(self.__rgx)

    def matches(self, dictnode):
        """Test whether given dictnode matches configured criteria."""
        if not self.__criteria_attr:
            return True  # no criteria to match on, include unconditionally
        attr = dictnode.get(self.__criteria_attr)
        if not attr:
            # **only** include positive matches, yet nothing to match on found
            return not self.__delim == ":"
        if self.__delim == ":":
            return bool(self.__rgx.search(attr))
        return not bool(self.__rgx.search(attr))


def clean_up_tree(root, criteria):
    """Iterate over XML tree and delete those <dictionary/>-nodes which have no
    release or which don't match one of the given criteria."""
    criteria_matched = False  # track whether a criteria was matchd
    criteria = Criteria(criteria)
    dictionary_idx = 0
    dictionaries = list(root)
    while dictionary_idx < len(dictionaries) - 1:
        dictionary = dictionaries[dictionary_idx]
        # criteria matched and has children (releases)
        if criteria.matches(dictionary) and list(dictionary):
            criteria_matched = True
            dictionary_idx += 1
        else:
            print(f'Skipping {dictionary.attrib["name"]}, ', end="")
            print("no releases" if not list(dictionary) else "didn't match criteria")
            root.remove(dictionary)
            dictionaries = list(root)
    if not criteria_matched:
        print("Warning: given criteria never matched")


def parse_api(root) -> list[str, dict]:
    """Parse all dictionaries from the given XML FreeDict API. Returns a sorted
    list (by ISO-639-3 dictionary name)."""
    dictionaries = []
    for child in root:
        if not list(child) or not child.attrib.get("name") \
                or not child.tag.endswith("dictionary"):
            continue  # skip dictionaries without releases or non-dictionary nodes
        dictionary = Dictionary(child.attrib["name"])
        for key in ("headwords", "edition", "status", "maintainerName"):
            try:
                dictionary.set(key, child.attrib[key])
            except KeyError as e:
                if e.args[0] != "status":
                    raise KeyError(
                        "missing attribute for %s: %s"
                        % (child.attrib["name"], e.args[0])
                    ) from None
        for release in child:
            if not release.attrib.get("platform") \
                    or release.attrib["platform"] != "src":
                continue
            dictionary.source_release = release.attrib["URL"]
            break # only one release
        dictionaries.append(dictionary)
    return dictionaries


def parse_args():
    if os.getcwd().endswith("debian"):
        os.chdir("..")
    if not any(re.match(r"[a-z]{3}-[a-z]{3}", f) for f in os.listdir(os.getcwd())):
        print("You must run this script from the FreeDict packaging root.")
        sys.exit(127)
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument(
        "--criteria",
        dest="criteria",
        default=None,
        help=(
            "A criteria is a XML attribute name (from the Dictionary tag),"
            " separated by a delimiter, followed by a regular expression. "
            "This can be used to filter dictionaries. If attribute name and"
            " regular expression are separated by a colon `:`, the "
            "dictionary node MUST match the given expression, if it "
            "separated by a exclamation mark `!`, these dictionaries will "
            "be dropped."
        ),
    )
    parser.add_argument(
        "--dc",
        dest="gen_control_copyright",
        action="store_true",
        default=False,
        help="generate debian/copyright and debian/control",
    )
    parser.add_argument(
        "--need-update",
        dest="check_for_upstream_dict_updates",
        action="store_true",
        default=False,
        help="check whether any package needs an update (implies -u",
    )
    parser.add_argument(
        "--no-desc-version",
        dest="no_desc_version",
        action="store_true",
        default=False,
        help="omit the version number in package descriptions (only "
        "useful when using --dc)",
    )
    parser.add_argument(
        "--orig",
        dest="fetch_orig",
        action="store_true",
        default=False,
        help="fetch a new orig source tar ball to ../",
    )
    parser.add_argument(
        "-u",
        dest="update_xml_api",
        action="store_true",
        default=False,
        help="Update FreeDict XML API file and exit.",
    )
    if len(sys.argv) == 1:
        parser.print_usage()
    return parser.parse_args()


def main():
    # are we in the correct directory?
    # cmd args
    args = parse_args()
    # perform an update of the XML API if requested *or* if  the check for newer
    # upstream dictionaries is requested
    perform_xml_api_update = (True if args.check_for_upstream_dict_updates
        else args.update_xml_api)
    xmlsrc = get_xml_content(perform_xml_api_update)
    actions = []
    # must be checked first
    # actions can be combined
    if args.fetch_orig:
        xmlsrc = get_xml_content(fetch_new=True)  # fetch latest FreeDict API file
        actions += [FetchSource]
    if args.gen_control_copyright:
        actions += [GenerateControlCopyright]
    if args.check_for_upstream_dict_updates:
        actions += [CheckForMoreRecentDicts]
    package_name = subprocess.check_output(["dpkg-parsechangelog",
            "-S", "Source"]) \
        .decode(sys.getdefaultencoding()) \
        .strip()

    # usual operation
    root = ET.fromstring(xmlsrc)
    clean_up_tree(root, criteria=args.criteria)
    dictionaries = parse_api(root)
    for obj in actions:
        inst = None
        if obj == GenerateControlCopyright:
            #pylint: disable=too-many-function-args
            inst = obj(package_name, dictionaries, args.no_desc_version)
        else:
            inst = obj(package_name, dictionaries)
        inst.perform_action()


if __name__ == "__main__":
    main()
