#!/usr/bin/env python3
# Copyright: 2026, Maximiliano Curia <maxy@debian.org>
#
# License: ISC
#  Permission to use, copy, modify, and/or distribute this software for any
#  purpose with or without fee is hereby granted, provided that the above
#  copyright notice and this permission notice appear in all copies.
#  .
#  THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
#  REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
#  AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
#  INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
#  LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
#  OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
#  PERFORMANCE OF THIS SOFTWARE.

"""DEP-5 licence name -> /usr/share/common-licenses file.

Maps the canonical DEP-5 names decopy emits to the base-files common-licenses
filename that holds the full text, so a standalone License paragraph can point
at the shared copy instead of duplicating it. The +/only distinction and the
GFDL invariants choice do not change the referenced text file (only the notice
header does), so an "or-later" name resolves to the same entry as its base and
the GFDL-NIV (no-invariants) name shares its version's file.
"""

from __future__ import annotations

import typing as t


class CommonLicense(t.NamedTuple):
    file: str  # basename under /usr/share/common-licenses
    friendly_name: str  # e.g. "the GNU General Public License version 2"


COMMON_LICENSES: dict[str, CommonLicense] = {
    "GPL-1": CommonLicense("GPL-1", "the GNU General Public License version 1"),
    "GPL-2": CommonLicense("GPL-2", "the GNU General Public License version 2"),
    "GPL-3": CommonLicense("GPL-3", "the GNU General Public License version 3"),
    "LGPL-2": CommonLicense(
        "LGPL-2", "the GNU Library General Public License version 2"
    ),
    "LGPL-2.1": CommonLicense(
        "LGPL-2.1", "the GNU Lesser General Public License version 2.1"
    ),
    "LGPL-3": CommonLicense(
        "LGPL-3", "the GNU Lesser General Public License version 3"
    ),
    "AGPL-3": CommonLicense(
        "AGPL-3.0", "the GNU Affero General Public License version 3"
    ),
    # decopy detection emits GFDL-<v> (invariants allowed) and GFDL-NIV-<v>
    # (no invariants); both reference the same version's common-licenses text.
    "GFDL-1.1": CommonLicense(
        "GFDL-1.1", "the GNU Free Documentation License version 1.1"
    ),
    "GFDL-NIV-1.1": CommonLicense(
        "GFDL-1.1", "the GNU Free Documentation License version 1.1"
    ),
    "GFDL-1.2": CommonLicense(
        "GFDL-1.2", "the GNU Free Documentation License version 1.2"
    ),
    "GFDL-NIV-1.2": CommonLicense(
        "GFDL-1.2", "the GNU Free Documentation License version 1.2"
    ),
    "GFDL-1.3": CommonLicense(
        "GFDL-1.3", "the GNU Free Documentation License version 1.3"
    ),
    "GFDL-NIV-1.3": CommonLicense(
        "GFDL-1.3", "the GNU Free Documentation License version 1.3"
    ),
    "Apache-2": CommonLicense("Apache-2.0", "the Apache License, Version 2.0"),
    "Artistic-1": CommonLicense("Artistic", "the Artistic License"),
    "Artistic-2": CommonLicense("Artistic-2.0", "the Artistic License version 2.0"),
    "BSL-1": CommonLicense("BSL-1.0", "the Boost Software License version 1.0"),
    "CC-BY-3": CommonLicense(
        "CC-BY-3.0", "the Creative Commons Attribution 3.0 licence"
    ),
    "CC-BY-4": CommonLicense(
        "CC-BY-4.0", "the Creative Commons Attribution 4.0 International Public Licence"
    ),
    "CC-BY-SA-3": CommonLicense(
        "CC-BY-SA-3.0", "the Creative Commons Attribution-ShareAlike 3.0 licence"
    ),
    "CC-BY-SA-4": CommonLicense(
        "CC-BY-SA-4.0",
        "the Creative Commons Attribution-ShareAlike 4.0 International Public Licence",
    ),
    "CC0": CommonLicense("CC0-1.0", "the Creative Commons CC0 1.0 Universal licence"),
    "MPL-1.1": CommonLicense("MPL-1.1", "the Mozilla Public License version 1.1"),
    "MPL-2": CommonLicense("MPL-2.0", "the Mozilla Public License version 2.0"),
    "OFL-1.1": CommonLicense("OFL-1.1", "the SIL Open Font License version 1.1"),
}


def common_license_for(name: str) -> CommonLicense | None:
    """The common-licenses entry for a DEP-5 name, or None if it has no copy.

    Strips a trailing "+" (or-later) so it resolves to the same file as the
    base name; the GFDL-NIV names are enumerated and matched directly.
    """
    return COMMON_LICENSES.get(name.rstrip("+"))
