#!/usr/bin/env python3
# Copyright: 2018, 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.


"""Regex substitution tables and copyright-detection patterns.

Leaf data module: the SUBS tables consumed by the license detail parsers
and find_licenses, plus the copyright-notice regexes used by holders.py.
Contains no logic; the parsers live in license_parsers, the rules in res.
"""

from __future__ import annotations

import re as reo

from ._regex import rex
from .re_types import ReFlags, ReSub, ReSubGroup

COMMENTS_SUBS: tuple[ReSub, ...] = (
    ReSub(
        reo.compile(
            # Fortran (fortran is weird: https://gcc.gnu.org/ml/gcc/2000-01/msg00250.html)
            r"^C[ \t]"
            # C / C++ (avoid dropping :// from uris)
            r"|(?<!:)//+"
            # m4
            r"|^dnl\s"
            # Generic
            r"|^[\s\W]+\s",
            rex.IGNORECASE | rex.MULTILINE,
        ),
        r" ",
    ),
    # Non text (and compress white)
    ReSub(reo.compile(r"[^ \w\'.,@:;()/+-]"), r" "),
    ReSub(reo.compile(r" {2,}"), r" "),
)

COPYRIGHT_PRE_IGNORE_REGEXES: tuple[ReFlags, ...] = (
    # #define foo(c) -- not copyright
    ReFlags(r"^#\s*define\s+.*\([^)]*c[^)]*\)"),
    # po files data
    ReFlags(r"^msgstr\s"),
    ReFlags(r"^#.{,2} msgstr\s"),
    ReFlags(r"^msgid\s"),
    ReFlags(r"^#.{,2} msgid\s"),
    # mostly gpl stuff
    ReFlags(
        r"We\sprotect\syour\srights\swith\s"
        r"(?:two\ssteps:|a\stwo-step\smethod:)\s\(1\)\s(?:we\s)?copyright",
    ),
    ReFlags(r"and\sput\sthe\sfollowing\scopyright"),
    ReFlags(r"connection\swith\sthe\scopyright"),
    ReFlags(r"applicable\scopyright\slaw\sincludes"),
    ReFlags(r"Original\scopyright\sfollows"),
    ReFlags(r"compilation\sand\sits\sresulting\scopyright"),
    ReFlags(r"under\sapplicable\scopyright\slaw"),
    ReFlags(r"copyright\son\sthe\sProgram,\sand"),
    ReFlags(r"copyright\son\sthe\ssoftware,\sand"),
    ReFlags(r"can\sgive\sappropriate\scopyright\spermission"),
    ReFlags(r"requiring\scopyright\spermission"),
    ReFlags(r"copyright\streaty\sadopted\son"),
    ReFlags(r"copyright\sby\stheir\scontributors"),
    ReFlags(r"the\ same\ copyright\ terms"),
    ReFlags(r"for\ copyright\ years"),
    ReFlags(r"Added\ copyright\ notes"),
    ReFlags(r"adjust\ copyright"),
    ReFlags(r"use\ copyright\ ranges"),
    ReFlags(r"copyright\ does(?:\ _?NOT_?|n\'t)"),
    ReFlags(r"copyright\ unless"),
    ReFlags(r'sign a "?copyright disclaimer"?'),
    # FSF address
    ReFlags(r"59\ Temple\ Place,\ Suite\ 330,\ Boston,\ MA\ \ ?02111-1307,?\ \ ?USA"),
    ReFlags(
        r"51\ Franklin\ St(?:reet)?,\ Fifth\ Floor,\ Boston,\ MA\ \ ?02110-1301,?\ \ ?USA",  # noqa: E501
    ),
    # partial fsf address as found in kcoreaddons
    ReFlags(r"\bBoston,\ MA\ \ ?02110-\d+,?\ \ ?USA"),
    # public domain
    ReFlags(r"disclaims\ copyright"),
    # creative commons
    ReFlags(r"the\ applicable\ copyright"),
    ReFlags(r"under\ copyright\ law\ or"),
    ReFlags(r"free\ from\ copyright\ or"),
    ReFlags(r"WIPO\ Copyright\ Treaty"),
    # Academic
    ReFlags(r"requirements\ and\ penalties\ of\ the\ U\.S\.\ Copyright"),
    ReFlags(r"Grant\ of\ Copyright\ License\."),
    ReFlags(r"prohibited\ by\ U\.S\.\ copyright\ law"),
    # Artistic
    ReFlags(r"under\ the\ copyright\ of\ this\ Package"),
    # MPL-1.1
    ReFlags(r"\(c\)\ +Representations"),
    # MPL-2.0
    ReFlags(r"copyright\ doctrines\ of\ fair\ use"),
    # Ms-PL
    ReFlags(r"Copyright\ Grant.?\ Subject\ to"),
    # Ms-PL
    ReFlags(r"copyright\ license\ to"),
    # OpenLDAP 2.8
    ReFlags(r"copyright\ in\ this\ Software"),
    # djvulibre d/copyright
    ReFlags(r"COPYRIGHT\ at\ the\ top\ level"),
    # found in abiword
    ReFlags(r"Trademark\ and\ copyright\ issues\ are"),
    # found in decopy
    ReFlags(r"group_by\ copyright"),
    ReFlags(r"copyright\ file"),
    ReFlags(r"copyright\ attribution"),
    ReFlags(r"copyright\ format"),
    ReFlags(r"copyright\ helper"),
    ReFlags(r"copyright\ detection"),
    # found in krita code
    ReFlags(r"Copyright\sflag"),
    # found in plan code
    ReFlags(r"{\scopyright\s=\sc;\s}"),
    # python lambda using "copyright" as a parameter name (ruff A006 fixture)
    ReFlags(r"\blambda\b[^:\n]*\bcopyright\b", 0),
    # krita gmic
    ReFlags(r"Quick\scopyright\s:\sgimp_quick_copyright"),
    # found in muffin/nemo/cinnamon-c-c
    ReFlags(r"\bcopyright\s(?:info|headers?|lines?|messages?|reasons?|symbols?)\b"),
    # found in muffin
    ReFlags(r"\bUpdate\scopyright\b"),
    # found in breakpad
    ReFlags(r"LTEXT.*Copyright.*IDC_STATIC"),
    # found in a changelog
    ReFlags(r"copyright\ shortname"),
    # found in a translation
    ReFlags(r"copyright\ es\ ilegal"),
    ReFlags(r"copyright\ existente"),
    ReFlags(r"copyright\ existent"),
    # found in kdepimlibs
    ReFlags(r"copyright\ on\ that\ code"),
    # found in boringssl
    ReFlags(r"copyright\ on\ this\ code"),
    # found in telepathy-logger-qt
    ReFlags(r"for\ copyright\ in"),
    # changelog entry
    ReFlags(r"Fix\ copyright"),
    # found in marble
    ReFlags(r"concern\ over\ copyright"),
    ReFlags(r"copyright\ &copy;\ @copyright@"),
    # found in boringssl
    ReFlags(r"Copyright\ with\ his\ permission"),
    # found in dom_distiller_js
    ReFlags(r"optional\ string\ copyright\ ="),
    # found in ffmpeg
    ReFlags(r"copyright\ &\ original\ defaults"),
    ReFlags(r"--copyright"),
    ReFlags(r"skip\ copyright"),
    ReFlags(r"Copyright\ (?:Exists|chunk|length|offset|signature|it\.)"),
    # found in icu
    ReFlags(r"U_COPYRIGHT_STRING"),
    ReFlags(r"LATIN\ CAPITAL\ LETTER\ C"),
    ReFlags(r"copyright\ at\ the\ end"),
    ReFlags(r"copyright\ generation"),
    ReFlags(r"print\ our\ copyright"),
    ReFlags(r"copyright\ into\ the"),
    # found in spacezero
    ReFlags(r"AUTHOR\(S\)\ and\ COPYRIGHT\ sections"),
    # damn you (c)
    ReFlags(r"\bif\b.*\(\s?c\s?\)"),
    ReFlags(r"\bwhile\b.*\(\s?c\s?\)"),
    ReFlags(r"Similar\sto\s\(\s?c\s?\)"),
    ReFlags(r"\(\s?c\s?\)\sit\smakes\suse"),
    ReFlags(r"replaced\s\(C\)"),
    ReFlags(r"\(c\)\ have"),
    ReFlags(r"\(c\)\ the\ sum"),
    ReFlags(r"\(unsigned\ int\)"),
    ReFlags(r"\(\*?a\),\ ?\(b\),\ ?\(c\)"),
    # This is intended to match code using (c)
    # the .* here is a bit too greedy.
    ReFlags(r"\(\w\)\s*[^\ \t\n\r\f\va-zA-Z0-9_].*[^\s,;]\s*\(\w\)"),
    ReFlags(r"\(c\).*\(c2\)"),
    ReFlags(r"ISUPPER\ ?\(c\)"),
    ReFlags(r"\(c\)\ You\ must\ retain"),
    ReFlags(r"\(c\)\ ?[\*/&|<>\+-]?="),
    ReFlags(r"\(c\)\ ?\)"),
    ReFlags(r"sizeof\([^)]+\)"),
    ReFlags(r"\(c\)(?:->|\.)[^ ]"),
    ReFlags(r"=\s*\(c\)\s*\*.*;"),
    ReFlags(r"\(c\)\s*[*/+-]\s*\("),
    ReFlags(r"\(c\)\ and\ its\ data"),
    ReFlags(r"character\ \(c\)"),
    ReFlags(r"\(c\)\ differs\ for"),
    ReFlags(r"combining\ mark\ \(c\)"),
    ReFlags(r"\(c\)\ *processed"),
    ReFlags(r"\(c\)\ data\ (?:[^ ]+\ ){,5}has\ been\ received"),
    ReFlags(r"shown\sby\s\(c\)\ssupports"),
    ReFlags(r"AND\s\(c\)"),
    # more text
    ReFlags(r"following\scopyright\sand\slicense "),
    ReFlags(r"\bcopyright\ssign\b"),
    # found in qca
    ReFlags(r'"Country\ Code\ \(C\)"'),
    # found in cinnamon
    ReFlags(r"\bcopyright\ and\b"),
    # template used in po
    ReFlags(r"\bCopyright:?\s(?:\(\s*C\s*\)\s)?YEAR\b"),
    # found in cinnamon-settings-daemon
    ReFlags(r"free\sof\sknown\scopyright\srestrictions"),
    ReFlags(r"[-\s–—]%[ds][-\s–—]"),  # noqa: RUF001
    # template (marble)
    ReFlags(r"<\*\*authorname\*\*>"),
    # template (kcoreaddons)
    ReFlags(r"%{AUTHOR}"),
    # found in kcoreaddons
    ReFlags(
        r'i18n\("Copyright (?:[^ ]+ ){,4}'
        r"(?:Developer|Foo Foundation|Bar Foundation|Baz Foundation)",
    ),
    # tagged to be ignored
    ReFlags(r"krazy:exclude(?:all)?=[^\s]*copyright"),
    # found in kde-l10n
    ReFlags(r"copyright\ of\ every\ file\ belongs\ to\ its\s"),
    # Test case in ark
    ReFlags(
        r"UNRAR\s+[0-9.]+\ (\(iconv\)|beta\ [0-9])\ freeware\s+"
        r"Copyright\ \(c\)\ [0-9-]+\ Alexander\ Roshal",
    ),
    # dll files interfaces
    ReFlags(r"\{[0-9A-F-]{32,}\}"),
)

COPYRIGHT_PRE_IGNORE_RES: tuple[rex.Pattern[str], ...] = tuple(
    rex.compile(re_flag.re, re_flag.flags) for re_flag in COPYRIGHT_PRE_IGNORE_REGEXES
)

COPYRIGHT_INDICATOR_REGEXES: tuple[ReFlags, ...] = (
    # Multiline indicator
    ReFlags(
        r"(?:^|\s|\W)"
        r"("
        r"(?:[^ ]+\ ){,8}(?:is|are)\ "
        r"(?:"
        r"copyright\ by\ the[\s]+"
        r"|Copyright(?:\ \(C\))?[:\s]+"
        r"|the\ Copyright\ (?:<\w+>)?property\ of(?:[:\s]+|$)"
        r")"
        r")"
        r"(?=\S|$)",
    ),
    # The full word
    ReFlags(r"(?:^|\s|\W)(copyright(?:\s*\(c\))?(?:[:\s]+))(?=\S|$)"),
    # Legally-valid abbreviation
    ReFlags(r"(?:^|\s|\W)(copr\.(?:[:\s]+))(?=\S|$)"),
    # Unicode character COPYRIGHT SIGN
    ReFlags(r"(?:^|\s)(©(?:[:\s]+))(?=\S|$)"),
    # Unicode copyright sign encoded in iso8859
    ReFlags(r"(?:^|\s)(\xa9(?:[:\s]+))(?=\S|$)"),
    # HTML copyright sign
    ReFlags(r"(?:^|\s|\W)(&copy;(?:[:\s]+))(?=\S|$)"),
    # texi macro
    ReFlags(r"(?:^|\s|\W)(@copyright\{?\}?(?:[:\s]+))(?=\S|$)"),
    # html copyright sign
    ReFlags(r"(?:^|\s|\W)(&\#169;(?:[:\s]+))(?=\S|$)"),
    # Free text found in some debian/copyright
    ReFlags(
        r"(?:^|\s|\W)((?:Upstream\ Authors?\ ?(?:and|,)\ )?Copyright\ Holders(?:[:\s]+))(?=\S|$)",  # noqa: E501
    ),
    # Legally-null representation of sign
    ReFlags(r"(?:^|\s|\W)((?:\(\s?c\s?\))[:\s]*)(?=\S|$)"),
    ReFlags(r"(?:^|\s|\W)(SPDX-FileCopyrightText(?:\s*\(c\))?(?:[:\s]+))(?=\S|$)"),
)

COPYRIGHT_INDICATOR_RES: tuple[rex.Pattern[str], ...] = tuple(
    rex.compile(re_flag.re, re_flag.flags) for re_flag in COPYRIGHT_INDICATOR_REGEXES
)

COPYRIGHT_INDICATOR_RE: rex.Pattern[str] = rex.compile(
    "|".join(f"{re_flag.re}" for re_flag in COPYRIGHT_INDICATOR_REGEXES),
    rex.IGNORECASE | rex.MULTILINE,
)

COPYRIGHT_PRE_INDICATOR_RE: rex.Pattern[str] = rex.compile(
    r"Copyright|copr\.|©|\xa9|&copy;|&\#169;|\(\s?c\s?\)",
    rex.IGNORECASE | rex.MULTILINE,
)

COPYRIGHT_POST_IGNORE_REGEXES: tuple[ReFlags, ...] = (
    # Discussing copyright information
    ReFlags(r"\binformation\b"),
    # Discussing the notice
    ReFlags(r"\bnotices?\b"),
    ReFlags(r"\bstatements?\b"),
    ReFlags(r"\bclaims?\b"),
    ReFlags(r"\bstrings?\b"),
    # GPL template
    ReFlags(r"\b\<?name\ of\ author\>?\b"),
    # template
    ReFlags(r"\bYEAR\s+YOUR\s+NAME\b"),
    ReFlags(r"\bYour\s+Name\s+\<your\.email@example\.com\>"),
    ReFlags(r"{{copyright}}"),
    # Part of a sentence
    # |and|or used in: and/or its subsidiary(-ies).
    # |in used in .in tld
    ReFlags(r"\b\ in\ \b"),
    # |to used in "1990 to 1995"
    ReFlags(r"^\s*is\b"),
    ReFlags(r"\bif\b"),
    ReFlags(r"\bholders?\b"),
    ReFlags(r"\bowners?\b"),
    ReFlags(r"\bownership\b"),
    # template
    ReFlags(r"\bIDC_STATIC\b"),
    # (c) conditional
    ReFlags(r"(?:^|\s)&&(?:\s|$)", 0),
    ReFlags(r"(?:^|\s)\|\|(?:\s|$)", 0),
    # ugly one letter expressions
    ReFlags(r"(?:^|\s)\(\s?[abd-f]\s?\)(?:\s|$)"),
    # GPL text
    ReFlags(r"^\s*laws?[.,:]"),
    # if (c) {
    ReFlags(r"^\s*\{\s*$", 0),
    # Copyright: License
    ReFlags(r"^\s*L?GPL$\s*$"),
    # template
    ReFlags(r"^\s*@\w+\{\w+\}\s*$", 0),
    # copyright of Qt has been transferred
    ReFlags(r"^\s*of\ [^\s]+\ has\b"),
    # copyright applied to
    ReFlags(r"^\s*applied\ to\b"),
    # QPL false positive
    ReFlags(r"^\s*Everyone\ is\ permitted\ to\ copy\b"),
)

COPYRIGHT_POST_IGNORE_RES: tuple[rex.Pattern[str], ...] = tuple(
    rex.compile(re_flag.re, re_flag.flags) for re_flag in COPYRIGHT_POST_IGNORE_REGEXES
)

CRUFT_SUBS: tuple[ReSub, ...] = (
    # Strip Markdown link syntax before name/year parsing
    ReSub(reo.compile(r"\[([^\]]+)\]\[[^\]]*\]"), r"\1"),  # [text][ref]  ->  text
    ReSub(reo.compile(r"\[([^\]]+)\]\([^)]*\)"), r"\1"),  # [text](url)  ->  text
    # "All rights reserved" ends the holder list: drop it and anything after it
    # on the line (e.g. "... All Rights Reserved. NO WARRANTY." -> holder only).
    ReSub(
        reo.compile(
            r"""(?:(?:some|all)? rights reserved"""
            r"""|(?:some|all) rights)\b.*""",
            reo.IGNORECASE,
        ),
        r"",
    ),
    ReSub(reo.compile(r"It can be distributed", reo.IGNORECASE), r""),
    ReSub(reo.compile(r"and contributors", reo.IGNORECASE), r""),
    # Drop the fsf address from the copyright
    ReSub(reo.compile(r"Franklin St(?:reet)?, Fifth Floor,?", reo.IGNORECASE), r""),
    ReSub(reo.compile(r"et al", reo.IGNORECASE), r""),
    ReSub(reo.compile(r"\band$", reo.IGNORECASE), r""),
    ReSub(reo.compile(r"\\$"), r""),
    ReSub(reo.compile(r"""[\s,.;\*#'"]*$"""), r""),
    ReSub(reo.compile(r"\(\sc\s\)", reo.IGNORECASE), r""),
    ReSub(reo.compile(r"</?\w{,2}/?>"), r""),  # Generic tag, <i> <br/>, etc
    ReSub(reo.compile(r"^(?:of|and)\s", reo.IGNORECASE), r""),
    # Unicode replacement char, present in badly converted files
    ReSub(reo.compile(r"�"), r""),
    ReSub(reo.compile(r"\s{2,}"), r" "),
    ReSub(reo.compile(r"^\s+"), r""),
    # Strip leading assignment operator (e.g. "copyright = u'...'")
    ReSub(reo.compile(r"^=\s*"), r""),
    ReSub(reo.compile(r"\s+$"), r""),
    ReSub(reo.compile(r"\\@"), r"@"),
    ReSub(reo.compile(r"&ndash;"), r"-"),
)

EMAIL_SUBS: tuple[ReSub, ...] = (
    ReSub(reo.compile(r"</?tt>"), r""),
    ReSub(reo.compile(r"%20"), r" "),
    # (HT|X)ML
    ReSub(reo.compile(r"(^\s?<!--|-->\s?$)"), r" "),
    ReSub(reo.compile(r"&lt;?"), r"<"),
    ReSub(reo.compile(r"&gt;?"), r">"),
    ReSub(reo.compile(r"&#x40;"), r"@"),
    ReSub(reo.compile(r"&ldquo;?"), r'"'),
    ReSub(reo.compile(r"\(c\)$", reo.IGNORECASE), r""),
    ReSub(reo.compile(r"\\[nt]$", reo.IGNORECASE), r""),
    # Expensive fix for ") at the end of the string
    ReSub(reo.compile(r"((?P<paren>\()?(?(paren).*?|))(?(paren)|\)+)?$"), r"\1"),
    ReSub(reo.compile(r"\s+\(?(where|at|@)\)?\s+", reo.IGNORECASE), r"@"),
    ReSub(reo.compile(r"\(at\)", reo.IGNORECASE), r"@"),
    ReSub(reo.compile(r"\s+\(?do?[tm]\)?\s+", reo.IGNORECASE), r"."),
    # Ugly fix for >mail@example.com<
    ReSub(reo.compile(r"(?:^|(?<=\s))\s*\>\s*(?=\w(?:\w|[.-])*@)"), r"<"),
    ReSub(reo.compile(r"\<\s*$"), r">"),
    ReSub(
        reo.compile(
            r"(?:^|(?<=\s))\s*((?!\<)\w(?:\w|[.-])*@"
            r"?:\w(?:\w|-)+(?:\.\w(?:\w|-)+)+(?<!>))\s*(?:(?=\s)|$)",
        ),
        r"<\1>",
    ),
    ReSub(reo.compile(r"\s\s+"), r" "),
    ReSub(reo.compile(r"^\s"), r""),
    ReSub(reo.compile(r"\s$"), r""),
)

NAME_CRUFT_SUBS: tuple[ReSub, ...] = (
    ReSub(reo.compile(r"</item>", reo.IGNORECASE), r""),
    ReSub(reo.compile(r"^>", reo.IGNORECASE), r""),
    ReSub(reo.compile(r"<$", reo.IGNORECASE), r""),
    ReSub(reo.compile(r"\\[nt]$", reo.IGNORECASE), r""),
    ReSub(reo.compile(r"^\(\s*c\s*\)\s*", reo.IGNORECASE), r""),
    ReSub(reo.compile(r"^and$", reo.IGNORECASE), r""),
    ReSub(reo.compile(r"^and\s+(?:others|contributors)?\s*", reo.IGNORECASE), r""),
    ReSub(reo.compile(r"\bbut is\b.*$", reo.IGNORECASE), r""),
)

ARTISTIC_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r'The "?Clarified "?Artistic License"? '
            r"Preamble ",
            rex.IGNORECASE,
        ),
        "ClArtistic",
    ),
    ReSub(
        rex.compile(
            r"You may charge a reasonable copying fee for any "
            r"distribution of this Package. You may charge "
            r"any fee you choose for support of this Package. "
            r"You may not charge a fee for this Package itself. "
            r"However, you may distribute this Package in aggregate "
            r"with other \(possibly commercial\) programs as "
            r"part of a larger \(possibly commercial\) software "
            r"distribution provided that you do not advertise "
            r"this Package as a product of your own. You may embed "
            r"this Package[\' ]?s interpreter within an executable "
            r"of yours \(by linking\);? this shall be construed as "
            r"a mere form of aggregation, provided that the complete "
            r"Standard Version of the interpreter is so embedded."
            r".+"
            r"Aggregation of this Package with a commercial "
            r"distribution is always permitted provided that "
            r"the use of this Package is embedded; that is, "
            r"when no overt attempt is made to make this Package[\' ]?s "
            r"interfaces visible to the end user of the commercial "
            r"distribution. Such use shall not be construed as a "
            r"distribution of this Package.",
            rex.IGNORECASE,
        ),
        "Artistic-1-Perl",
    ),
    ReSub(
        rex.compile(
            r"Aggregation of this Package with a commercial "
            r"distribution is always permitted provided that "
            r"the use of this Package is embedded; that is, "
            r"when no overt attempt is made to make this Package[\' ]?s "
            r"interfaces visible to the end user of the commercial "
            r"distribution. Such use shall not be construed as a "
            r"distribution of this Package.",
            rex.IGNORECASE,
        ),
        "Artistic-1-cl8",
    ),
    ReSub(
        rex.compile(
            r'The "?Artistic License"? Preamble.*?'
            r"You may reuse parts of this Package in your own programs, "
            r"provided that you explicitly state where you got them from",
            rex.IGNORECASE | rex.DOTALL,
        ),
        "Artistic-dist",
    ),
    ReSub(
        rex.compile(
            r'The "?Artistic License"? '
            r"Preamble "
            r"The intent of this document is to state the conditions under "
            r"which a Package may be copied, such that the Copyright Holder "
            r"maintains some semblance of artistic control over the "
            r"development of the package, while giving the users of the "
            r"package the right to use and distribute the Package in a "
            r"more[- ]or[- ]less customary fashion, plus the right to make "
            r"reasonable modifications.",
            rex.IGNORECASE,
        ),
        "Artistic-1",
    ),
    ReSub(
        rex.compile(
            r"The Net Boolean Public License "
            r"Version ([\d.]+)",
            rex.IGNORECASE,
        ),
        r"NBPL-\1",
    ),
    ReSub(
        rex.compile(
            r"The Open Group Test Suite License "
            r"Preamble",
            rex.IGNORECASE,
        ),
        r"OGTSL",
    ),
    ReSub(
        rex.compile(
            r"The OpenLDAP Public License "
            r"Version ([\d.]+)",
            rex.IGNORECASE,
        ),
        r"OLDAP-\1",
    ),
)

BSD_SUBS: tuple[ReSub, ...] = (
    # Specific BSD variants (checked before the generic clause-count rules
    # below); each anchors on a clause unique to that variant.
    # The three Sun/Oracle variants share the "ARE HEREBY EXCLUDED" disclaimer;
    # the nuclear clause (and its "licensed" wording) or its absence tells them
    # apart. Anchoring on that disclaimer keeps them off the otherwise-identical
    # -2014 nuclear form, which carries the standard BSD disclaimer instead.
    ReSub(
        rex.compile(
            r"ARE HEREBY EXCLUDED.*not designed, licensed or intended for use "
            r"in the design, construction, operation or maintenance of any "
            r"nuclear",
            rex.IGNORECASE,
        ),
        "BSD-3-clause-no-nuclear-license",
    ),
    ReSub(
        rex.compile(
            r"ARE HEREBY EXCLUDED.*not designed or intended for use in the "
            r"design, construction, operation or maintenance of any nuclear",
            rex.IGNORECASE,
        ),
        "BSD-3-clause-no-nuclear-warranty",
    ),
    ReSub(
        rex.compile(
            r"DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR "
            r"RELATING TO USE, MODIFICATION OR DISTRIBUTION",
            rex.IGNORECASE,
        ),
        "BSD-3-clause-Sun",
    ),
    ReSub(
        rex.compile(
            r"this paragraph are duplicated in all such forms and that any "
            r"documentation, advertising materials",
            rex.IGNORECASE,
        ),
        "BSD-4.3TAHOE",
    ),
    ReSub(
        rex.compile(r"BSD-4-Clause-Shortened", rex.IGNORECASE),
        "BSD-4-clause-shortened",
    ),
    ReSub(
        rex.compile(
            r"BSD-4-Clause \(University of California-Specific\)", rex.IGNORECASE
        ),
        "BSD-4-clause-UC",
    ),
    ReSub(rex.compile(r"The Clear BSD License", rex.IGNORECASE), "BSD-3-clause-clear"),
    ReSub(
        rex.compile(
            r"Flex carries the copyright used for BSD software", rex.IGNORECASE
        ),
        "BSD-3-clause-flex",
    ),
    ReSub(
        rex.compile(
            r"subject to receipt of any required approvals from the "
            r"U\.S\. Dept\. of Energy",
            rex.IGNORECASE,
        ),
        "BSD-3-clause-LBNL",
    ),
    ReSub(
        rex.compile(
            r"not designed, licensed or intended for use in the design, "
            r"construction, operation or maintenance of any military",
            rex.IGNORECASE,
        ),
        "BSD-3-clause-no-military",
    ),
    ReSub(
        rex.compile(
            r"reproduce at minimum a disclaimer substantially similar to the "
            r"NO WARRANTY disclaimer",
            rex.IGNORECASE,
        ),
        "BSD-3-clause-acpica",
    ),
    ReSub(
        rex.compile(
            r"the entire permission notice in its entirety, including the "
            r"disclaimer of warranties",
            rex.IGNORECASE,
        ),
        "BSD-3-clause-Tso",
    ),
    ReSub(
        rex.compile(
            r"the following disclaimer listed in this license in the documentation",
            rex.IGNORECASE,
        ),
        "BSD-3-clause-Open-MPI",
    ),
    ReSub(
        rex.compile(
            r"Neither the name of the HP nor the names of its contributors",
            rex.IGNORECASE,
        ),
        "BSD-3-clause-HP",
    ),
    ReSub(
        rex.compile(
            r"All modifications to the source code must be clearly marked as such",
            rex.IGNORECASE,
        ),
        "BSD-Mark-Modifications",
    ),
    ReSub(
        rex.compile(
            r"must appear in all copies of the software, derivative works or "
            r"modified versions",
            rex.IGNORECASE,
        ),
        "BSD-Inferno-Nettverk",
    ),
    ReSub(
        rex.compile(
            r"FREE FOR COMMERCIAL AND NON-COMMERCIAL USE as long as the "
            r"following conditions are adhered to",
            rex.IGNORECASE,
        ),
        "BSD-Systemics",
    ),
    ReSub(
        rex.compile(
            r"each copyright holder and contributor hereby grants to those "
            r"receiving rights under this license",
            rex.IGNORECASE,
        ),
        "BSD-2-clause-patent",
    ),
    ReSub(
        rex.compile(
            r"not subject to any export provision of the United States "
            r"Department of Commerce, and may be exported to any country or planet",
            rex.IGNORECASE,
        ),
        "BSD-2-clause-Darwin",
    ),
    ReSub(
        rex.compile(
            r"the following disclaimer, without modification, immediately at "
            r"the beginning of the file",
            rex.IGNORECASE,
        ),
        "BSD-source-beginning-file",
    ),
    ReSub(
        rex.compile(
            r"Redistributions of source code must retain the above copyright "
            r"notice, this list of conditions and the following disclaimer as "
            r"the first lines of this file",
            rex.IGNORECASE,
        ),
        "BSD-2-clause-first-lines",
    ),
    ReSub(
        rex.compile(
            r"this list of conditions and the following disclaimer\.\s*"
            r"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS",
            rex.IGNORECASE,
        ),
        "BSD-1-clause",
    ),
    ReSub(
        rex.compile(
            r"4. If you include any Windows specific code \(or a derivative "
            r"thereof\) fromg? the apps directory \(application code\) you "
            r"must include an acknowledgement:? "
            r'"?This product includes software written by Tim Hudson '
            r'\(tjh@cryptsoft.com\)"?',
            rex.IGNORECASE,
        ),
        "SSLeay",
    ),
    ReSub(
        rex.compile(
            r"Redistributions in any form must be accompanied by "
            r"information on how to obtain complete source code for the "
            r"software that uses (?:[^ ]+ ){,4}and any accompanying "
            r"software that uses the software that uses (?:[^ ]+ ){,4}"
            r"The source code must either be included in the "
            r"distribution or be available for no more than the cost of "
            r"distribution plus a nominal fee, and must be freely "
            r"redistributable under reasonable conditions\. For an "
            r"executable file, complete source code means the source code "
            r"for all modules it contains\. It does not include source code "
            r"for modules or files that typically accompany the major "
            r"components of the operating system on which the executable "
            r"file runs\. ",
            rex.IGNORECASE,
        ),
        "BSD-like-TMate",
    ),
    ReSub(
        rex.compile(
            r"with the name\(s\) of the authors as specified in the "
            r"copyright notice\(s\) substituted where indicated",
            rex.IGNORECASE,
        ),
        "BSD-like-radvd",
    ),
    ReSub(
        rex.compile(
            r"All advertising materials mentioning features or use of "
            r"this software must display the following "
            r"acknowledge?ment.*This product includes software "
            r"developed by",
            rex.IGNORECASE,
        ),
        "BSD-4-clause",
    ),
    ReSub(
        rex.compile(
            r"You acknowledge that this software is not designed"
            r"(?:, licensed)? "
            r"or intended for use in the design, construction, operation or "
            r"maintenance of any nuclear facility.",
            rex.IGNORECASE,
        ),
        "BSD-3-clause-no-nuclear",
    ),
    ReSub(
        rex.compile(
            r"Redistributions of source code must retain the above "
            r"copyright notice, this list of conditions and the following "
            r"disclaimer.? "
            r"(?:\* )?"
            r"(?:The name(?:\(?s\)?)? .*? may not|Neither the "
            r"(?:names? .*?|authors?) ?n?or the names of(?: "
            r"(?:its|their|other|any))? contributors may) be used to "
            r"endorse or promote products derived from this software",
            rex.IGNORECASE,
        ),
        "BSD-source-code",
    ),
    ReSub(
        rex.compile(
            r"Redistributions of any form whatsoever must retain the "
            r"following acknowledgment:? \'?This product includes software "
            r"developed by ",
            rex.IGNORECASE,
        ),
        "BSD-3-clause-attribution",
    ),
    # bzip2 carries its version in the notice; name the two shipped variants
    # before the generic bzip2 rule below.
    ReSub(
        rex.compile(r"Version 1\.0\.5 of", rex.IGNORECASE),
        "BSD-like-bzip2-1.0.5",
    ),
    ReSub(
        rex.compile(r"version 1\.0\.6 of", rex.IGNORECASE),
        "BSD-like-bzip2-1.0.6",
    ),
    ReSub(
        rex.compile(
            r"The origin of this software must not be misrepresented;? "
            r"you must not claim that you wrote the original software. "
            r"If you use this software in a product, an acknowledgment "
            r"in the product documentation would be appreciated but is "
            r"not required."
            r".+"
            r"Altered source versions must be plainly marked as such, "
            r"and must not be misrepresented as being the original "
            r"software."
            r".+"
            r"The name of the author may not be used to endorse or "
            r"promote products derived from this software without "
            r"specific prior written permission.",
            rex.IGNORECASE,
        ),
        "BSD-like-bzip2",
    ),
    ReSub(rex.compile(r"Intel Open Source License ", rex.IGNORECASE), "Intel"),
    ReSub(
        rex.compile(
            r"All redistributions must comply with the conditions imposed by "
            r"the University of California on certain embedded code, which "
            r"copyright Notice and conditions for redistribution are as "
            r"follows",
            rex.IGNORECASE,
        ),
        "Sendmail",
    ),
    ReSub(
        rex.compile(
            r"Redistributions in any form must be accompanied by "
            r"information on how to obtain complete source code for "
            r"the DB software and any accompanying software that uses the "
            r"DB software\. The source code must either be included in the "
            r"distribution or be available for no more than the cost of "
            r"distribution plus a nominal fee, and must be freely "
            r"redistributable under reasonable conditions\. For an "
            r"executable file, complete source code means the source code "
            r"for all modules it contains\. It does not include source code "
            r"for modules or files that typically accompany the major "
            r"components of the operating system on which the executable "
            r"file runs\.",
            rex.IGNORECASE,
        ),
        "Sleepycat",
    ),
    ReSub(
        rex.compile(
            r"Any altered version must clearly and properly represent the "
            r"origin of this software",
            rex.IGNORECASE,
        ),
        "BSD-like-ngrep",
    ),
    ReSub(
        rex.compile(
            r"Part 1: CMU/?UCD copyright notice",
            rex.IGNORECASE,
        ),
        "Net-SNMP",
    ),
    ReSub(
        rex.compile(
            r"Use, Modification and Redistribution \(including distribution "
            r"of any modified or derived work\) in source and/?or binary "
            r"forms is permitted",
            rex.IGNORECASE,
        ),
        "BSD-like-JPNIC",
    ),
    ReSub(
        rex.compile(
            r"(?:The name(?:\(?s\)?)? .*? may not|Neither the "
            r"(?:names? .*?|authors?) n?or the names of(?: "
            r"(?:its|their|other|any))? contributors may) be used to "
            r"endorse or promote products derived from this software",
            rex.IGNORECASE,
        ),
        "BSD-3-clause",
    ),
    ReSub(
        rex.compile(
            r"Any additions, deletions, or changes to the original files "
            r"must be clearly indicated in accompanying documentation\. "
            r"including the reasons for the changes, and the names of "
            r"those who made the modifications\.",
            rex.IGNORECASE,
        ),
        "BSD-like-Mup",
    ),
    ReSub(
        rex.compile(
            r"Redistributions in any form must be accompanied by information "
            r"on how to obtain complete source code for this software and "
            r"any accompanying software that uses this software\. The source "
            r"code must either be included in the distribution or be "
            r"available in a timely fashion for no more than the cost of "
            r"distribution plus a nominal fee, and must be freely "
            r"redistributable under reasonable and no more restrictive "
            r"conditions\. For an executable file, complete source code "
            r"means the source code for all modules it contains\. It does "
            r"not include source code for modules or files that typically "
            r"accompany the major components of the operating system on "
            r"which the executable file runs\. ",
            rex.IGNORECASE,
        ),
        "BSD-like-TOSL",
    ),
    ReSub(
        rex.compile(
            r"The views and conclusions contained in the software and "
            r"documentation are those of the authors and should not "
            r"be interpreted as representing official policies, "
            r"either expressed or implied, of the FreeBSD Project.",
            rex.IGNORECASE,
        ),
        "BSD-2-clause-FreeBSD",
    ),
    ReSub(
        rex.compile(
            r"views and conclusions contained in the software and "
            r"documentation are those of the authors and should not be "
            r"interpreted as representing official policies",
            rex.IGNORECASE,
        ),
        "BSD-2-clause-views",
    ),
    ReSub(
        rex.compile(
            r"This code is derived from software contributed to The "
            r"NetBSD Foundation by",
            rex.IGNORECASE,
        ),
        "BSD-2-clause-NetBSD",
    ),
    ReSub(
        rex.compile(
            r"Nothing in this license shall be deemed to grant "
            r"any rights to trademarks, copyrights, patents, trade "
            r"secrets or any other intellectual property of A.M.P.A.S. "
            r"or any contributors, except as expressly stated herein, "
            r"and neither the name of A.M.P.A.S. nor of any other "
            r"contributors to this software, may be used to endorse "
            r"or promote products derived from this software without "
            r"specific prior written permission of A.M.P.A.S. or "
            r"contributor, as appropriate.",
            rex.IGNORECASE,
        ),
        "BSD-2-clause-AMPAS",
    ),
    ReSub(
        rex.compile(
            r"This code is derived from software contributed to The "
            r"NetBSD Foundation by",
            rex.IGNORECASE,
        ),
        "BSD-2-clause-NetBSD",
    ),
    ReSub(
        rex.compile(
            r"Redistributions of source code must retain the(?: above)? "
            r"copyright notice",
            rex.IGNORECASE,
        ),
        "BSD-2-clause",
    ),
    ReSub(
        rex.compile(
            r"BSD PROTECTION LICENSE "
            r"TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION, AND "
            r"MODIFICATION",
            rex.IGNORECASE,
        ),
        "BSD-protection",
    ),
    ReSub(
        rex.compile(
            r"Permission to use, copy, modify, and/?or distribute this "
            r"software for any purpose with or without fee is hereby "
            r"granted. "
            r'THE SOFTWARE IS PROVIDED "?AS IS"? AND THE AUTHOR DISCLAIMS '
            r"ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL "
            r"IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO "
            r"EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, "
            r"INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER "
            r"RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN "
            r"ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, "
            r"ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE "
            r"OF THIS SOFTWARE.",
            rex.IGNORECASE,
        ),
        "0BSD",
    ),
    ReSub(
        rex.compile(
            r"Redistributions of the Code in binary form must be "
            r"accompanied by this GPG-signed text in any documentation "
            r"and, each time the resulting executable program or a "
            r"program dependent thereon is launched, a prominent "
            r"display \(?e\.g\., splash screen or banner text\)? "
            r"of the Author[\' ]?s attribution information, which "
            r"includes:? "
            r'\(?a\)? Name \(?[" ]AUTHOR[" ]\)?, '
            r'\(?b\)? Professional identification \(?[" ]PROFESSIONAL '
            r'IDENTIFICATION[" ]\)?, and '
            r'\(?c\)? URL \(?[" ]URL[" ]\)?.',
            rex.IGNORECASE,
        ),
        "AAL",
    ),
    ReSub(
        rex.compile(
            r"The Condor Team may publish revised and/or new versions "
            r"of this Condor Public License "
            r'\((?:``|[" ])?this License(?:\'\'|[" ])?\) '
            r"from time to time\. Each "
            r"version will be given a distinguishing version number\. "
            r"Once Software has been published under a particular "
            r"version of this License, you may always continue to use "
            r"it under the terms of that version\. You may also "
            r"choose to use such Software under the terms of "
            r"any subsequent version of this License published "
            r"by the Condor Team\. No one other than the Condor "
            r"Team has the right to modify the terms of this License\.",
            rex.IGNORECASE,
        ),
        "Condor-1.1",
    ),
    ReSub(
        rex.compile(
            r"This product includes software developed by the EU DataGrid",
            rex.IGNORECASE,
        ),
        "EUDatagrid",
    ),
    ReSub(
        rex.compile(
            r"Access and use of this software shall impose the following "
            r"obligations and understandings on the user\. The user is "
            r"granted the right, without any fee or cost, to use, copy, "
            r"modify, alter, enhance and distribute this software, and "
            r"any derivative works thereof, and its supporting "
            r"documentation for any purpose whatsoever, provided that "
            r"this entire notice appears in all copies of the software, "
            r"derivative works and supporting documentation\. Further, "
            r"(?:[^\s]+ ){,8}requests that the user credit "
            r"(?:[^\s]+ ){,8}in any publications that result from the "
            r"use of this software or in any product that includes "
            r"this software, although this is not an obligation\. The "
            r"names "
            r"(?:[^\s]+ ){,8}however, may not be used in any advertising "
            r"or publicity to endorse or promote any products or "
            r"commercial entity unless specific written permission is "
            r"obtained from "
            r"(?:[^\s]+ ){,8}The user also understands that "
            r"(?:[^\s]+ ){,8}is not obligated to provide the user with any "
            r"support, consulting, training or assistance of any kind with "
            r"regard to the use, operation and performance of this software "
            r"nor to provide the user with any updates, revisions, new "
            r'versions or "?bug fixes',
            rex.IGNORECASE,
        ),
        "NetCDF",
    ),
    ReSub(
        rex.compile(
            r"You may make and give away verbatim copies of the source "
            r"form of the software without restriction, provided that you "
            r"duplicate all of the original copyright notices and "
            r"associated disclaimers\. "
            r".{,4}You may modify your copy of the software in any way, "
            r"provided that you do at least ONE of the following:? ",
            rex.IGNORECASE,
        ),
        "Ruby",
    ),
    ReSub(
        rex.compile(
            r"The copyright holder has authorized the removal of this clause",
            rex.IGNORECASE,
        ),
        "Inner-Net-2.0",
    ),
    ReSub(
        rex.compile(
            r"Redistribution and use in source and binary forms -? ?with or "
            r"without modification -? ?are permitted for any purpose, "
            r"provided that redistributions in source form retain this "
            r"entire copyright notice and indicate the origin and nature of "
            r"any modifications\. "
            r"I\'d appreciate being given credit for this package in the "
            r"documentation of software which uses it, but that is not a "
            r"requirement\. ",
            rex.IGNORECASE,
        ),
        "Spencer-99",
    ),
)

# Title prefix shared by the CC_BY_SUBS rules. The "Creative Commons"/"CC" lead
# is optional (some ports open with a bare "Attribution-…" title), and the mod
# tokens tolerate the hyphenated/spaced spellings used by the jurisdiction ports
# ("Non-Commercial", "Share-Alike", "Attribution - Share-Alike 2.0 England").
# The "Attribution" head also accepts the localized spellings used by the
# non-English ports (German Namensnennung, Dutch Naamsvermelding, French
# Paternité, Japanese katakana アトリビューション); clean_comments() turns the
# em-dash separators those titles use into spaces, so [ -] still covers them.
_CC_BY = r"(?:Attribution|Namensnennung|Naamsvermelding|Paternité|アトリビューション)"
_CC_TITLE = (
    r"(?:https?[: ]/?/?creativecommons.org/licenses/by"
    rf"|(?:(?:Creative Commons|CC) (?:Legal Code )?)?{_CC_BY})[ -]+"
)
# Modifier tokens carry their localized spellings too. German uses two NC
# spellings across versions ("Nicht-kommerziell" v2.0 / "Keine kommerzielle
# Nutzung" v3.0); list the longer phrases before the bare "nc"/"sa"/"nd".
_CC_NC = (
    r"(?:Non-?Commercial|Nicht-kommerziell|Keine kommerzielle Nutzung"
    r"|Pas d'Utilisation Commerciale|nc)"
)
_CC_SA = (
    r"(?:Share-?Alike|Weitergabe unter gleichen Bedingungen"
    r"|Partage Des Conditions Initiales A l'Identique|シェアアライク|sa)"
)
_CC_ND = r"(?:No-?Deriv(?:atives|s)?|Keine Bearbeitungen?|nd)"

CC_BY_SUBS: tuple[ReSub | ReSubGroup, ...] = (
    ReSub(
        rex.compile(
            _CC_TITLE + rf"(?:(?:{_CC_SA}|{_CC_ND})[ -]+){{,2}}{_CC_NC}",
            rex.IGNORECASE,
        ),
        r"-NC",
    ),
    ReSub(
        rex.compile(
            _CC_TITLE + rf"(?:(?:{_CC_NC}|{_CC_ND})[ -]+){{,2}}{_CC_SA}",
            rex.IGNORECASE,
        ),
        r"-SA",
    ),
    ReSub(
        rex.compile(
            _CC_TITLE + rf"(?:(?:{_CC_NC}|{_CC_SA})[ -]+){{,2}}{_CC_ND}",
            rex.IGNORECASE,
        ),
        r"-ND",
    ),
    ReSub(
        rex.compile(
            _CC_TITLE + rf"(?:(?:{_CC_NC}|{_CC_SA}|{_CC_ND})[ -]*){{,2}}/?"
            r"(\d(?:\.\d[\d.]*)?)[^\d.]",
            rex.IGNORECASE,
        ),
        r"-\1",
    ),
    # Jurisdiction port suffix, appended after the version (e.g. CC-BY-3-US).
    # A port has exactly one jurisdiction, so this is a first-match-wins group.
    # Each marker is a phrase distinctive to that jurisdiction's legal code; the
    # NL/FR ports carry no country name in the text, so the localized language
    # word (Dutch "Naamsvermelding", French "Paternité") stands in for the single
    # jurisdiction shipped in that language.
    ReSubGroup(
        (
            ReSub(rex.compile(r"United States", rex.IGNORECASE), r"-US"),
            ReSub(rex.compile(r"England and Wales", rex.IGNORECASE), r"-UK"),
            ReSub(rex.compile(r"Australia", rex.IGNORECASE), r"-AU"),
            ReSub(rex.compile(r"intergovernmental", rex.IGNORECASE), r"-IGO"),
            ReSub(rex.compile(r"Deutschland", rex.IGNORECASE), r"-DE"),
            ReSub(rex.compile(r"Naamsvermelding", rex.IGNORECASE), r"-NL"),
            ReSub(rex.compile(r"Paternité", rex.IGNORECASE), r"-FR"),
            ReSub(rex.compile(r"ジャパン", rex.IGNORECASE), r"-JP"),
        ),
    ),
)

# Documentation / open-government / open-data licences (run by parse_doc only
# after the DocLicense gate in LICENSES_RES fires). Each rule pairs the title
# with a substantive clause from the licence body, so a file that only mentions
# the title (e.g. a reference or a `License:` field) is not matched. First match
# wins. The UK OGL / OPL / NCGL family shares one grant clause; the title tells
# them apart.
_OGL_GRANT = r"grants you a worldwide, royalty-free, perpetual, non-exclusive licence"
DOC_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            rf"Open Government Licence v1\.0.{{0,400}}{_OGL_GRANT}",
            rex.IGNORECASE,
        ),
        "OGL-UK-1",
    ),
    ReSub(
        rex.compile(
            rf"Open Government Licence v2\.0.{{0,400}}{_OGL_GRANT}",
            rex.IGNORECASE,
        ),
        "OGL-UK-2",
    ),
    ReSub(
        rex.compile(
            rf"Open Government Licence v3\.0.{{0,400}}{_OGL_GRANT}",
            rex.IGNORECASE,
        ),
        "OGL-UK-3",
    ),
    ReSub(
        rex.compile(
            rf"Open Government Licence - Canada.{{0,400}}{_OGL_GRANT}", rex.IGNORECASE
        ),
        "OGL-Canada-2",
    ),
    ReSub(
        rex.compile(
            rf"Open Parliament Licence v3\.0.{{0,400}}{_OGL_GRANT}",
            rex.IGNORECASE,
        ),
        "OPL-UK-3",
    ),
    ReSub(
        rex.compile(
            r"Non-Commercial Government Licence for public sector information"
            rf".{{0,400}}{_OGL_GRANT}",
            rex.IGNORECASE,
        ),
        "NCGL-UK-2",
    ),
    ReSub(
        rex.compile(
            r"Norwegian Licence for Open Government Data \(NLOD\) 2\.0.{0,200}"
            r"This licence grants you the right to copy, use and distribute "
            r"information",
            rex.IGNORECASE,
        ),
        "NLOD-2",
    ),
    ReSub(
        rex.compile(
            r"LICENCE OUVERTE / OPEN LICENCE Version 2\.0.{0,200}concède au "
            r"Réutilisateur un droit non exclusif et gratuit de libre "
            r"Réutilisation",
            rex.IGNORECASE,
        ),
        "etalab-2",
    ),
    ReSub(
        rex.compile(
            r"Common Documentation License Version 1\.0.{0,200}Permission is "
            r"granted to copy and distribute verbatim copies of this License",
            rex.IGNORECASE,
        ),
        "CDL-1",
    ),
    ReSub(
        rex.compile(
            r"This is the Community Data License Agreement Permissive, "
            r"Version 1\.0 \( Agreement \)\. Data is provided to You under "
            r"this Agreement",
            rex.IGNORECASE,
        ),
        "CDLA-Permissive-1",
    ),
    ReSub(
        rex.compile(
            r"This is the Community Data License Agreement - Permissive, "
            r"Version 2\.0 \(the agreement \)\. Data Provider\(s\) and Data "
            r"Recipient\(s\) agree as follows",
            rex.IGNORECASE,
        ),
        "CDLA-Permissive-2",
    ),
    ReSub(
        rex.compile(
            r"This is the Community Data License Agreement Sharing, "
            r"Version 1\.0 \( Agreement \)\. Data is provided to You under "
            r"this Agreement",
            rex.IGNORECASE,
        ),
        "CDLA-Sharing-1",
    ),
    ReSub(
        rex.compile(
            r"The Open Data Commons Attribution License is a license "
            r"agreement intended to allow users to freely share, modify, and "
            r"use this Database",
            rex.IGNORECASE,
        ),
        "ODC-By-1",
    ),
    ReSub(
        rex.compile(
            r"Jede Nutzung ist unter den Bedingungen dieser Datenlizenz "
            r"Deutschland Namensnennung Version 2\.0 zul",
            rex.IGNORECASE,
        ),
        "DL-DE-BY-2",
    ),
    ReSub(
        rex.compile(
            r"Datenlizenz Deutschland Zero Version 2\.0 Jede Nutzung ist ohne "
            r"Einschränkungen oder Bedingungen zul",
            rex.IGNORECASE,
        ),
        "DL-DE-Zero-2",
    ),
    ReSub(
        rex.compile(
            r"Open Publication License v1\.0.{0,80}REQUIREMENTS ON BOTH "
            r"UNMODIFIED AND MODIFIED VERSIONS The Open Publication works may "
            r"be reproduced and distributed",
            rex.IGNORECASE,
        ),
        "OPUBL-1",
    ),
)

# Font licences (run by parse_font only after the FontLicense gate in
# LICENSES_RES fires). Each rule anchors on a substantive grant/permission
# clause from the font licence body, not merely the foundry or font name, so a
# file that just mentions a font is not matched. First match wins.
FONT_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"ARPHIC PUBLIC LICENSE specifically permits and encourages you "
            r"to use this software",
            rex.IGNORECASE,
        ),
        "Arphic-1999",
    ),
    ReSub(
        rex.compile(
            r"Permission to use, copy, modify and distribute this font is "
            r"hereby granted, provided that both the copyright notice and this "
            r"permission notice appear in all copies of the font",
            rex.IGNORECASE,
        ),
        "Baekmuk",
    ),
    ReSub(
        rex.compile(
            r"granted permission under all Bitstream propriety rights to use, "
            r"copy, modify, sublicense, sell, and redistribute",
            rex.IGNORECASE,
        ),
        "Bitstream-Charter",
    ),
    ReSub(
        rex.compile(
            r"obtaining a copy of the fonts accompanying this license "
            r"\( Fonts \) and associated documentation files "
            r"\(the Font Software \)",
            rex.IGNORECASE,
        ),
        "Bitstream-Vera",
    ),
    ReSub(
        rex.compile(
            r"Any use of the code is permitted as long as this copyright "
            r"notice is preserved in the code",
            rex.IGNORECASE,
        ),
        "Kastrup",
    ),
    ReSub(
        rex.compile(
            r"are hereby granted a nonexclusive, royalty-free copyright and "
            r"design patent license to use this code",
            rex.IGNORECASE,
        ),
        "Lucida-Bitmap-Fonts",
    ),
    ReSub(
        rex.compile(
            r"ParaType Ltd grants you the right to use, copy, modify the fonts "
            r"and distribute modified and unmodified copies",
            rex.IGNORECASE,
        ),
        "ParaType-Free-Font-1.3",
    ),
    ReSub(
        rex.compile(
            r"Unlimited permission is granted to use, copy, and distribute it, "
            r"with or without modification, either commercially and "
            r"noncommercially",
            rex.IGNORECASE,
        ),
        "mplus",
    ),
    ReSub(
        rex.compile(
            r"UBUNTU FONT LICENCE Version 1\.0.{0,200}This licence allows the "
            r"licensed fonts to be used, studied, modified and redistributed "
            r"freely",
            rex.IGNORECASE,
        ),
        "Ubuntu-font-1",
    ),
)

# Open-hardware licences (run by parse_hw only after the HardwareLicence gate in
# LICENSES_RES fires). Each rule pairs the versioned title with a substantive
# preamble clause; the CERN v2 Permissive/Strongly/Weakly variants share one
# preamble and are told apart by the variant word in the title. First match wins.
_CERN_V2 = r"freedom to use, study, modify, share and distribute hardware designs"
HW_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"CERN Open Hardware Licence v1\.1.{0,400}wishes to disseminate its "
            r"hardware designs",
            rex.IGNORECASE,
        ),
        "CERN-OHL-1.1",
    ),
    ReSub(
        rex.compile(
            r"CERN Open Hardware Licence v1\.2.{0,400}wishes to provide a tool "
            r"to foster collaboration",
            rex.IGNORECASE,
        ),
        "CERN-OHL-1.2",
    ),
    ReSub(
        rex.compile(
            rf"CERN Open Hardware Licence Version 2 - Permissive.{{0,400}}{_CERN_V2}",
            rex.IGNORECASE,
        ),
        "CERN-OHL-P-2",
    ),
    ReSub(
        rex.compile(
            r"CERN Open Hardware Licence Version 2 - Strongly Reciprocal"
            rf".{{0,400}}{_CERN_V2}",
            rex.IGNORECASE,
        ),
        "CERN-OHL-S-2",
    ),
    ReSub(
        rex.compile(
            r"CERN Open Hardware Licence Version 2 - Weakly Reciprocal"
            rf".{{0,400}}{_CERN_V2}",
            rex.IGNORECASE,
        ),
        "CERN-OHL-W-2",
    ),
    ReSub(
        # The title page ("based closely on the Apache License") or the standard
        # licence header ("you may not use this file except in compliance"); both
        # are version-specific, so the header is detected as well as the body.
        rex.compile(
            r"SOLDERPAD HARDWARE LICENSE version 0\.5 .{0,200}based closely on "
            r"the Apache License Version 2\.0"
            r"|Solderpad Hardware License, Version 0\.5(?!\d).{0,160}you may not "
            r"use this file except in compliance",
            rex.IGNORECASE,
        ),
        "SHL-0.5",
    ),
    ReSub(
        rex.compile(
            r"SOLDERPAD HARDWARE LICENSE version 0\.51.{0,200}based closely on "
            r"the Apache License Version 2\.0"
            r"|Solderpad Hardware License, Version 0\.51.{0,160}you may not "
            r"use this file except in compliance",
            rex.IGNORECASE,
        ),
        "SHL-0.51",
    ),
    ReSub(
        rex.compile(
            r"The TAPR Open Hardware License Version 1\.0.{0,400}Open Hardware "
            r"is a thing",
            rex.IGNORECASE,
        ),
        "TAPR-OHL-1",
    ),
)

# US-Government / agency public-domain notices (run by parse_usgov_pd only after
# the USGovPD gate in LICENSES_RES fires). Several share the "Section 105 / works
# of <agency> employees are not subject to copyright" clause, so each rule pins
# the substantive public-domain clause together with the agency that names the
# id (e.g. NIST-PD vs NTIA-PD differ only by agency). First match wins.
USGOV_PD_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"This software was developed by employees of the National "
            r"Institute of Standards and Technology \(NIST\), and others\. "
            r"This software has been contributed to the public domain",
            rex.IGNORECASE,
        ),
        "NIST-PD",
    ),
    ReSub(
        rex.compile(
            r"This software was developed by employees of the National "
            r"Institute of Standards and Technology \(NIST\), an agency of the "
            r"Federal Government and is being made available as a public "
            r"service",
            rex.IGNORECASE,
        ),
        "NIST-PD-fallback",
    ),
    ReSub(
        rex.compile(
            r"This software was developed at the National Institute of "
            r"Standards and Technology \(NIST\) by employees of the Federal "
            r"Government in the course of their official duties",
            rex.IGNORECASE,
        ),
        "NIST-PD-TNT",
    ),
    ReSub(
        rex.compile(
            r"NIST-developed software is provided by NIST as a public "
            r"service\. You may use, copy, and distribute copies of the "
            r"software in any medium, provided that you keep intact this "
            r"entire notice",
            rex.IGNORECASE,
        ),
        "NIST-Software",
    ),
    ReSub(
        rex.compile(
            r"developed by employees of the National Telecommunications and "
            r"Information Administration \(NTIA\), an agency of the Federal "
            r"Government and is provided to you as a public service",
            rex.IGNORECASE,
        ),
        "NTIA-PD",
    ),
    ReSub(
        rex.compile(
            r"PUBLIC DOMAIN NOTICE National Center for Biotechnology "
            r"Information",
            rex.IGNORECASE,
        ),
        "NCBI-PD",
    ),
    ReSub(
        rex.compile(
            r"This library \(libselinux\) is public domain software, i\.e\. "
            r"not copyrighted",
            rex.IGNORECASE,
        ),
        "libselinux-1",
    ),
)

# Miscellaneous public-domain / "do as you wish" notices that share no common
# origin (run by parse_pd_misc only after the PdMisc gate in LICENSES_RES fires).
# Each rule anchors on the notice's distinctive dedication clause. First match
# wins. The SAX v2 form starts "SAX2 is Free", which keeps it off the v1 SAX-PD
# (already detected) that embeds the same "abandon ... SAX 2.0" sentence.
PD_MISC_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(r"In place of a legal notice, here is a blessing", rex.IGNORECASE),
        "blessing",
    ),
    ReSub(
        rex.compile(
            r"In case this attempt to disclaim copyright and place the "
            r"software in the public domain is deemed null and void",
            rex.IGNORECASE,
        ),
        "bcrypt-Solar-Designer",
    ),
    ReSub(
        rex.compile(
            r"SAX2 is Free I hereby abandon any property rights to SAX 2\.0",
            rex.IGNORECASE,
        ),
        "SAX-PD-2",
    ),
    ReSub(
        rex.compile(
            r"There is no license associated with the code and you may use it "
            r"for any purpose personal or commercial",
            rex.IGNORECASE,
        ),
        "SGP4",
    ),
    ReSub(
        rex.compile(
            r"My symlinks utility pre-dates the open source licensing fad by a "
            r"number of years",
            rex.IGNORECASE,
        ),
        "Symlinks",
    ),
    ReSub(
        rex.compile(
            r"DO WHAT THE FUCK YOU WANT TO BUT IT'S NOT MY FAULT PUBLIC LICENSE",
            rex.IGNORECASE,
        ),
        "WTFNMFPL",
    ),
    ReSub(
        rex.compile(
            r"dedicate any and all copyright interest in this code to the "
            r"public domain",
            rex.IGNORECASE,
        ),
        "Unlicense-libtelnet",
    ),
    ReSub(
        rex.compile(
            r"LZMA SDK is written and placed in the public domain by Igor "
            r"Pavlov\. Some code in LZMA is based on public domain code",
            rex.IGNORECASE,
        ),
        "LZMA-SDK-9.11-to-9.20",
    ),
    ReSub(
        rex.compile(
            r"Some code in LZMA SDK is based on public domain code from "
            r"another developers",
            rex.IGNORECASE,
        ),
        "LZMA-SDK-9.22",
    ),
)

# Linux man-pages licences (run by parse_manpage only after the ManPage gate in
# LICENSES_RES fires). The three "copyleft" variants share the same two-paragraph
# FSF manual permission; the specific tails (the "level of care" sentence, the
# "...herein. Formatted" sequence) come first, and the bare two-paragraph form is
# the last fallback. Its negative lookahead skips the Latex2e family, which adds
# a "translations of this manual" paragraph after the same two paragraphs.
MANPAGE_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"distribute possibly modified copies of this page provided the "
            r"header is included verbatim",
            rex.IGNORECASE,
        ),
        "Linux-man-pages-1-para",
    ),
    ReSub(
        rex.compile(
            r"may not have taken the same level of care in the production of "
            r"this manual, which is licensed free of charge",
            rex.IGNORECASE,
        ),
        "Linux-man-pages-copyleft",
    ),
    ReSub(
        rex.compile(
            r"for damages resulting from the use of the information contained "
            r"herein\. Formatted or processed versions",
            rex.IGNORECASE,
        ),
        "Linux-man-pages-copyleft-var",
    ),
    ReSub(
        rex.compile(
            r"make and distribute verbatim copies of this manual provided the "
            r"copyright notice and this permission notice are preserved"
            r".{0,300}permission notice identical to this one\."
            r"(?!\s*Permission is granted to copy and distribute translations)",
            rex.IGNORECASE,
        ),
        "Linux-man-pages-copyleft-2-para",
    ),
)

# CTAN / TeX licences (run by parse_ctan only after the CtanTex gate in
# LICENSES_RES fires). These share the TeX/CTAN ecosystem but no single clause;
# each rule anchors on its distinctive permission/rename clause. First match
# wins.
CTAN_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"Unlimited copying and redistribution of this package and/?or its "
            r"individual files are permitted as long as there are no "
            r"modifications",
            rex.IGNORECASE,
        ),
        "Knuth-CTAN",
    ),
    ReSub(
        rex.compile(
            r"This file may be freely copied and distributed, provided that no "
            r"changes whatsoever are made\. All users are asked to help keep",
            rex.IGNORECASE,
        ),
        "MMIXware",
    ),
    ReSub(
        rex.compile(
            r"except that this permission notice may be stated in a "
            r"translation approved by the Foundation",
            rex.IGNORECASE,
        ),
        "Latex2e-translated-notice",
    ),
    ReSub(
        rex.compile(
            r"This software may be freely used, transmitted, reproduced, or "
            r"modified provided that the copyright notice and this permission "
            r"is retained",
            rex.IGNORECASE,
        ),
        "magaz",
    ),
    ReSub(
        rex.compile(
            r"The style package is copyrighted but may be used and extended in "
            r"any way, as long as a pointer to the original author is "
            r"maintained",
            rex.IGNORECASE,
        ),
        "swrule",
    ),
    ReSub(
        rex.compile(
            r"This file may be distributed, modified, and used in other works "
            r"with just one restriction: modified versions must clearly "
            r"indicate the modification",
            rex.IGNORECASE,
        ),
        "threeparttable",
    ),
    ReSub(
        rex.compile(
            r"This software may be freely transmitted, reproduced, or modified "
            r"for any purpose provided that this copyright notice is left "
            r"intact",
            rex.IGNORECASE,
        ),
        "ulem",
    ),
    ReSub(
        rex.compile(
            r"THE TTYP0 LICENSE Permission is hereby granted, free of charge, "
            r"to any person obtaining a copy of this font software",
            rex.IGNORECASE,
        ),
        "TTYP0",
    ),
)

# Standards-body licences - Unicode, plus the W3C-derived family (run by
# parse_std only after the StandardsBody gate fires). The gate deliberately omits
# the "W3C SOFTWARE NOTICE AND LICENSE" title, so the already-detected W3C /
# W3C-19980720 are untouched. The two Unicode DFS years differ only by the ICU
# directory (2016), so the ICU rule precedes the bare-title 2015 fallback. First
# match wins.
STD_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"UNICODE LICENSE V3 COPYRIGHT AND PERMISSION NOTICE", rex.IGNORECASE
        ),
        "Unicode-3",
    ),
    ReSub(
        rex.compile(
            r"UNICODE, INC\. LICENSE AGREEMENT - DATA FILES AND SOFTWARE"
            r".{0,400}source\.icu-project\.org",
            rex.IGNORECASE,
        ),
        "Unicode-DFS-2016",
    ),
    ReSub(
        rex.compile(
            r"UNICODE, INC\. LICENSE AGREEMENT - DATA FILES AND SOFTWARE",
            rex.IGNORECASE,
        ),
        "Unicode-DFS-2015",
    ),
    ReSub(
        rex.compile(r"OGC Software License, Version 1\.0", rex.IGNORECASE),
        "OGC-1",
    ),
    ReSub(
        rex.compile(
            r"WordNet Release 3\.0 This software and database is being "
            r"provided to you, the LICENSEE, by Princeton University",
            rex.IGNORECASE,
        ),
        "WordNet",
    ),
    ReSub(
        rex.compile(
            r"By obtaining and/?or copying this work, you \(the licensee\) "
            r"agree",
            rex.IGNORECASE,
        ),
        "W3C-20150513",
    ),
)

# Source-available / business copyleft licences - each carries a distinctive
# title-and-version line, paired here with a substantive body clause (release
# date, canonical URL, or the licence's own summary sentence) so a bare
# reference to the name does not match. parse_srcavail names the variant after
# the gate fires. The two Parity versions share a title stem but differ in their
# one-line summary; the two copyleft-next releases differ only by release date.
# Single-".0"-suffixed ids let the family parser strip the trailing ".0"
# (SSPL-1.0 -> SSPL-1, Elastic-2.0 -> Elastic-2); ids with multiple trailing
# zero components carry the fully-stripped name as the repl (Parity-6.0.0 ->
# Parity-6, PolyForm-Noncommercial-1.0.0 -> PolyForm-Noncommercial-1).
SRCAVAIL_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"copyleft-next 0\.3\.0 \( this License \) "
            r"Release date: 2013-05-16",
            rex.IGNORECASE,
        ),
        "copyleft-next-0.3.0",
    ),
    ReSub(
        rex.compile(
            r"copyleft-next 0\.3\.1 \( this License \) "
            r"Release date: 2016-04-29",
            rex.IGNORECASE,
        ),
        "copyleft-next-0.3.1",
    ),
    ReSub(
        rex.compile(
            r"The Parity Public License 6\.0\.0.{0,200}This license lets you "
            r"use and share this software for free, as long as you contribute",
            rex.IGNORECASE,
        ),
        "Parity-6",
    ),
    ReSub(
        rex.compile(
            r"The Parity Public License 7\.0\.0.{0,200}This license allows you "
            r"to use and share this software for free, but you have to share "
            r"software that builds on it alike",
            rex.IGNORECASE,
        ),
        "Parity-7",
    ),
    ReSub(
        rex.compile(
            r"PolyForm Noncommercial License 1\.0\.0 "
            r"https://polyformproject\.org/licenses/noncommercial",
            rex.IGNORECASE,
        ),
        "PolyForm-Noncommercial-1",
    ),
    ReSub(
        rex.compile(
            r"PolyForm Small Business License 1\.0\.0 "
            r"https://polyformproject\.org/licenses/small-business",
            rex.IGNORECASE,
        ),
        "PolyForm-Small-Business-1",
    ),
    ReSub(
        rex.compile(
            r"Server Side Public License VERSION 1, OCTOBER 16, 2018",
            rex.IGNORECASE,
        ),
        "SSPL-1.0",
    ),
    # SSPL standardLicenseHeader: the application notice grants under the SSPL
    # by reference ("under the terms of the Server Side Public License,
    # version 1").
    ReSub(
        rex.compile(
            r"under the terms of the Server Side Public License, version 1",
            rex.IGNORECASE,
        ),
        "SSPL-1.0",
    ),
    ReSub(
        rex.compile(
            r"Business Source License 1\.1 License text copyright", rex.IGNORECASE
        ),
        "BUSL-1.1",
    ),
    # BUSL standardLicenseHeader: the application notice puts use under the BUSL
    # by reference (verbatim typo "govered" from the upstream template).
    ReSub(
        rex.compile(
            r"Use of this software is govered by the Business Source License",
            rex.IGNORECASE,
        ),
        "BUSL-1.1",
    ),
    ReSub(
        rex.compile(
            r"Elastic License 2\.0 URL: "
            r"https://www\.elastic\.co/licensing/elastic-license",
            rex.IGNORECASE,
        ),
        "Elastic-2.0",
    ),
    ReSub(
        rex.compile(
            r"Sustainable Use License Version 1\.0 Acceptance By using the "
            r"software, you agree to all of the terms",
            rex.IGNORECASE,
        ),
        "SUL-1.0",
    ),
)

# European Space Agency Public License - three copyleft strengths sharing one
# template (run by parse_esa_pl only after the EsaPl gate fires). Each rule
# pairs the variant's title-and-version line with the start of the shared
# Definitions clause, so a bare reference to the family name declines.
ESA_PL_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"European Space Agency Public License \(ESA-PL\) Permissive "
            r"\(Type 3\) v2\.4 1 Definitions",
            rex.IGNORECASE,
        ),
        "ESA-PL-permissive-2.4",
    ),
    ReSub(
        rex.compile(
            r"European Space Agency Public License \(ESA-PL\) Weak Copyleft "
            r"\(Type 2\) v2\.4 1 Definitions",
            rex.IGNORECASE,
        ),
        "ESA-PL-weak-copyleft-2.4",
    ),
    ReSub(
        rex.compile(
            r"European Space Agency Public License \(ESA-PL\) Strong Copyleft "
            r"\(Type 1\) v2\.4 1 Definitions",
            rex.IGNORECASE,
        ),
        "ESA-PL-strong-copyleft-2.4",
    ),
)

# TU-Berlin (GSM 06.10) notice - v2.0 is v1.0's notice plus an appended
# permissive grant, so the v2.0 rule (anchored on that addition) must precede
# the v1.0 base-notice fallback. Run by parse_tu_berlin after the TuBerlin gate.
# The trailing ".0" is stripped (TU-Berlin-1.0 -> TU-Berlin-1).
TU_BERLIN_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(r"we append this additional permission", rex.IGNORECASE),
        "TU-Berlin-2.0",
    ),
    ReSub(
        rex.compile(
            r"Any use of this software is permitted provided that this notice "
            r"is not removed and that neither the authors nor the Technische "
            r"Universitaet Berlin",
            rex.IGNORECASE,
        ),
        "TU-Berlin-1.0",
    ),
)

# Mulan Permissive Software License (Chinese text) - the two versions differ by
# the "第1版"/"第2版" (v1/v2) marker in the grant clause. Run by parse_mulan after
# the MulanPsl gate. The trailing ".0" is stripped (MulanPSL-1.0 -> MulanPSL-1).
MULAN_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(r"修改及分发受木兰宽松许可证 第1版", rex.IGNORECASE),
        "MulanPSL-1.0",
    ),
    ReSub(
        rex.compile(r"修改及分发受木兰宽松许可证 第2版", rex.IGNORECASE),
        "MulanPSL-2.0",
    ),
    # English standardLicenseHeader application notice (grant by reference).
    ReSub(
        rex.compile(
            r"licensed under (?:the )?Mulan PSL v1\. You can use this software "
            r"according to the terms and conditions of the Mulan PSL v1",
            rex.IGNORECASE,
        ),
        "MulanPSL-1.0",
    ),
    ReSub(
        rex.compile(
            r"licensed under (?:the )?Mulan PSL v2\. You can use this software "
            r"according to the terms and conditions of the Mulan PSL v2",
            rex.IGNORECASE,
        ),
        "MulanPSL-2.0",
    ),
)

# OSL-template derivatives (Rosen's Open Software License boilerplate reused
# under a new name). Run by parse_osl_derived after the OslDerived gate; each
# rule pairs the licence's own title-and-version with the shared "applies to any
# original work of authorship" clause, so a bare name reference declines. The
# trailing ".0" is stripped (TGPPL-1.0 -> TGPPL-1, UCL-1.0 -> UCL-1).
OSL_DERIVED_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"Transitive Grace Period Public Licence \( ?TGPPL ?\) v\. 1\.0 "
            r"This Transitive Grace Period Public Licence \(the License ?\) "
            r"applies to any original work of authorship",
            rex.IGNORECASE,
        ),
        "TGPPL-1.0",
    ),
    ReSub(
        rex.compile(
            r"Upstream Compatibility License v\. 1\.0 \(UCL-1\.0\) This Upstream "
            r"Compatibility License \(the License ?\) applies to any original "
            r"work of authorship",
            rex.IGNORECASE,
        ),
        "UCL-1.0",
    ),
    # standardLicenseHeader application notices (grant by reference).
    ReSub(
        rex.compile(
            r"Licensed under the Transitive Grace Period Public Licence "
            r"version 1\.0",
            rex.IGNORECASE,
        ),
        "TGPPL-1.0",
    ),
    ReSub(
        rex.compile(
            r"Licensed under the Upstream Compatibility License 1\.0",
            rex.IGNORECASE,
        ),
        "UCL-1.0",
    ),
)

# Microsoft Use of Data Agreements - the Computational (C-UDA) and Open (O-UDA)
# variants share a template; run by parse_uda after the UdaData gate. The
# trailing ".0" is stripped (C-UDA-1.0 -> C-UDA-1, O-UDA-1.0 -> O-UDA-1).
UDA_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"Computational Use of Data Agreement, Version 1\.0 \(the C-UDA",
            rex.IGNORECASE,
        ),
        "C-UDA-1.0",
    ),
    ReSub(
        rex.compile(
            r"Open Use of Data Agreement, Version 1\.0 \(the O-UDA",
            rex.IGNORECASE,
        ),
        "O-UDA-1.0",
    ),
)

# ASWF Digital Assets License - the two versions differ only by the title
# version line. Run by parse_aswf after the AswfAssets gate (which fires on the
# distinctive "digital assets" redistribution clause). The trailing ".0" is
# stripped (ASWF-Digital-Assets-1.0 -> ASWF-Digital-Assets-1).
ASWF_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(r"ASWF Digital Assets License v1\.1", rex.IGNORECASE),
        "ASWF-Digital-Assets-1.1",
    ),
    ReSub(
        rex.compile(r"ASWF Digital Assets License v1\.0", rex.IGNORECASE),
        "ASWF-Digital-Assets-1.0",
    ),
)

# MITRE terms-of-use grants (CAPEC, CVE) - both authorise reuse provided the
# MITRE copyright designation is reproduced. Run by parse_mitre after the
# MitreTou gate; each rule names the dataset granted.
MITRE_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"license to use Common Attack Pattern Enumeration and "
            r"Classification \(CAPEC",
            rex.IGNORECASE,
        ),
        "CAPEC-tou",
    ),
    ReSub(
        # Anchor on the grant verb, not a bare "CVE" mention: the short form
        # collides with generic CVE references in source/docs (verified against
        # codesearch.debian.net), while this grant clause is unique to the licence.
        rex.compile(
            r"and distribute Common Vulnerabilities and Exposures \(CVE",
            rex.IGNORECASE,
        ),
        "cve-tou",
    ),
)

# CMU/Mach permission notice (Carnegie Mellon, reused by NRL) - both share the
# "ALLOWS FREE USE ... IN ITS AS IS CONDITION" disclaimer that fires the CmuMach
# gate; run by parse_cmu_mach. The documentation variant carries the extra
# "supporting documentation" clause; the bare gate disclaimer is the no-doc form.
CMU_MACH_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(r"appear in supporting documentation", rex.IGNORECASE),
        "CMU-Mach",
    ),
    ReSub(
        rex.compile(
            r"ALLOWS FREE USE OF THIS SOFTWARE IN ITS AS IS CONDITION",
            rex.IGNORECASE,
        ),
        "CMU-Mach-nodoc",
    ),
)

# Tatu Ylonen's original SSH permission notice - both forms share the "the code I
# have written ... can be used freely" grant that fires the Ssh gate; run by
# parse_ssh. The full OpenSSH notice adds the "legal status ... some combination"
# paragraph; the short form is just the grant clause.
SSH_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"The legal status of this program is some combination of all these",
            rex.IGNORECASE,
        ),
        "SSH-OpenSSH",
    ),
    ReSub(
        rex.compile(
            r"the code I have written for this software can be used freely "
            r"for any purpose",
            rex.IGNORECASE,
        ),
        "SSH-short",
    ),
)

# Boehm GC permission notice - both forms share the "a notice that the code was
# modified is included" clause that fires the BoehmGc gate; run by parse_boehm.
# The without-fee variant grants "without fee"; the base form omits it.
BOEHM_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"use or copy this software for any purpose is hereby granted "
            r"without fee",
            rex.IGNORECASE,
        ),
        "Boehm-GC-without-fee",
    ),
    ReSub(
        rex.compile(
            r"granted to use or copy this program for any purpose", rex.IGNORECASE
        ),
        "Boehm-GC",
    ),
)

CECILL_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"Une liste de questions fréquemment posées se "
            r"trouve sur le site web officiel de la famille "
            r"des licences CeCILL "
            r"\(https?[: ]/?/?www.cecill.info/index.fr.html\) "
            r"pour toute clarification qui serait nécessaire\.",
            rex.IGNORECASE,
        ),
        "CeCILL-2.1",
    ),
    ReSub(
        rex.compile(
            r"Frequently asked questions can be found on the "
            r"official website of the CeCILL licenses family "
            r"\(https?[: ]/?/?www.cecill.info/index.en.html\)"
            r"for any necessary clarification\. ",
            rex.IGNORECASE,
        ),
        "CeCILL-2.1",
    ),
    ReSub(
        rex.compile(r"un modèle de diffusion en logiciel libre", rex.IGNORECASE),
        "CeCILL-2",
    ),
    ReSub(
        rex.compile(
            r"The exercising of these rights is conditional "
            r"upon certain obligations for users so as to preserve "
            r"this status for all subsequent redistributions\.",
            rex.IGNORECASE,
        ),
        "CeCILL-2",
    ),
    ReSub(
        rex.compile(
            r"As a counterpart to the access to the source "
            r"code and rights to copy, modify and redistribute "
            r"granted by the license, users are provided only "
            r"with a limited warranty and the software\'s author, "
            r"the holder of the economic rights, and the successive "
            r"licensors only have limited liability\.",
            rex.IGNORECASE,
        ),
        "CeCILL-1.1",
    ),
    ReSub(
        rex.compile(r"un modèle de diffusion (?:« ?)?open source ?»?", rex.IGNORECASE),
        "CeCILL-1",
    ),
    ReSub(
        rex.compile(
            r"Nevertheless, access to the source code, and "
            r"the resulting rights to copy, modify and redistribute "
            r"only provide users with a limited warranty and the "
            r"software\'s author, the holder of the economic "
            r"rights, and the successive licensors only have "
            r"limited liability\. ",
            rex.IGNORECASE,
        ),
        "CeCILL-1",
    ),
)


CNRI_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"Licensee may not use CNRI trademarks or trade "
            r"name, including [^ ]+ or CNRI, in a trademark "
            r"sense to endorse or promote products or services "
            r"of Licensee, or any third party\. Licensee may "
            r"use the mark [^ ]+ in connection with Licensee\'s "
            r"derivative versions that are based on or incorporate "
            r"the Software, but only in the form ",
            rex.IGNORECASE,
        ),
        "CNRI-Jython",
    ),
    ReSub(
        rex.compile(
            r"URL:? ?https?[: ]/?/?hdl\.handle\.net/"
            r"1895.22/1006",
            rex.IGNORECASE,
        ),
        "CNRI-Jython",
    ),
    ReSub(
        rex.compile(
            r"Notwithstanding the foregoing, with regard to "
            r"derivative works based on "
            r".+"
            r"that incorporate non[- ]separable material that "
            r"was previously distributed under the "
            r"GNU General Public License",
            rex.IGNORECASE,
        ),
        "CNRI-Python-GPL-Compatible",
    ),
    ReSub(
        rex.compile(
            r"URL:? ?https?[: ]/?/?hdl\.handle\.net/"
            r"1895.22/1013",
            rex.IGNORECASE,
        ),
        "CNRI-Python-GPL-Compatible",
    ),
    ReSub(
        rex.compile(
            r"URL:? ?https?[: ]/?/?hdl\.handle\.net/"
            r"1895.22/1011",
            rex.IGNORECASE,
        ),
        "CNRI-Python",
    ),
)

EFL_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"If the binary program depends on a modified version "
            r"of this package, you are encouraged to publicly "
            r"release the modified version of this package",
            rex.IGNORECASE,
        ),
        "EFL-2",
    ),
    ReSub(
        rex.compile(
            r"if the binary program depends on a modified version "
            r"of this package, you must publicly release the "
            r"modified version of this package",
            rex.IGNORECASE,
        ),
        "EFL-1",
    ),
)

GNU_VERSION_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"version ([\d.]+) (?:of the License,? )?or "
            r"(?:\(? ?at your option ?\)? )?"
            r"version ([\d.]+),? or,? (?:(?:any )?later versions? "
            r"(?:accepted|approved) by the "
            r"membership of KDE ?e\.?V\.?|at the discretion of KDE ?e\.?V\.? "
            r"(?:[^ ]+ ){,20}any later version)",
            rex.IGNORECASE,
        ),
        r"\1+\2+KDEeV",
    ),
    ReSub(
        rex.compile(
            r"General Public License version ([\d.]+) (?:and|or),? "
            r"(?:\(? ?at your option ?\)? )?version "
            r"([\d.]+) as published by the Free Software Foundation",
            rex.IGNORECASE,
        ),
        r"\1+\2",
    ),
    ReSub(
        rex.compile(
            r"General Public License as published by the Free Software "
            r"Foundation[,;]? (?:either )?version ([\d.]+) "
            r"(?:of the License,? )?"
            r"(?:and|or),? (?:\(? ?at your option ?\)? )?(?:version )?"
            r"([\d.]+?)[.,;]? ",
            rex.IGNORECASE,
        ),
        r"\1+\2",
    ),
    ReSub(
        rex.compile(
            r"version ([\d.]+) of the License,? or,? "
            r"(?:\(? ?at your option ?\)? )?"
            r"(?:any later version accepted by the "
            r"membership of KDE e.V.|at the discretion of KDE ?e\.?V\.? "
            r"(?:[^ ]+ ){,20}any later version)",
            rex.IGNORECASE,
        ),
        r"\1+KDEeV",
    ),
    ReSub(
        rex.compile(
            r"either version ([^ ]+?)(?: of the License)?[;,] "
            r"or[ ,(]+at your option[ ,)]+any later version",
            rex.IGNORECASE,
        ),
        r"\1+",
    ),
    ReSub(
        rex.compile(
            r"(?:modify it|used) under the terms of the GNU (?:Affero )?"
            r"(?:Lesser(?:/Library)? |Library(?:/Lesser)? )?General Public "
            r"Licen[cs]e (?:as published by the Free Software Foundation[;,]? )?"
            r"\(?(?:either )?version ?([^ ]+?)[;,)]* (?:as published by the Free "
            r"Software Foundation[;,]? )?(?:of the Licen[sc]e,? )?or "
            r"(?:later|(?:\(? ?at your option ?\)?,? )?any later version)",
            rex.IGNORECASE,
        ),
        r"\1+",
    ),
    ReSub(
        rex.compile(
            r"licensed under (?:[^ ]+ ){,10}[AL]?GPL,? ?(?:version|v)"
            r" ?([\d.]+) (?:(?:or )?(?:higher|later)|or[, (]+at your "
            r"option[, )]+any later version)",
            rex.IGNORECASE,
        ),
        r"\1+",
    ),
    ReSub(
        rex.compile(
            r"General Public License,? Version ([\d.]+) or later",
            rex.IGNORECASE,
        ),
        r"\1+",
    ),
    ReSub(
        rex.compile(
            r"under the terms of (?:the )?version ([^ ]+) or later of the "
            r"(?:GNU|[AL]?GPL)",
            rex.IGNORECASE,
        ),
        r"\1+",
    ),
    ReSub(
        rex.compile(
            r"GNU (?:Affero )?(?:Lesser(?:/Library)? |Library )?"
            r"General Public License (?:as )?published by the Free "
            r"Software Foundation[;,] either version ([^ ]+?)[.,]? "
            r"(?:of the License )?or "
            r"(?:\(at your option\) )?any later version",
            rex.IGNORECASE,
        ),
        r"\1+",
    ),
    ReSub(
        rex.compile(
            r"licensed under (?:[^ ]+ ){,10}[AL]?GPL ?(?:version|v)"
            r" ?([\d.]+?)[.,]? ",
            rex.IGNORECASE,
        ),
        r"\1",
    ),
    ReSub(
        rex.compile(
            r"(?:used|modify it) under the terms (?:and conditions )?"
            r"of the GNU "
            r"(?:Affero )?"
            r"(?:Lesser(?:/Library)? |Library )?General Public "
            r"License,? (?:as published by the Free Software "
            r"Foundation;? )?(?:either )?"
            r"Version ([\d.]+?)(?:[.,;]|as)? ",
            rex.IGNORECASE,
        ),
        r"\1",
    ),
    ReSub(
        rex.compile(
            r"GNU (?:Affero )?(?:Lesser(?:/Library)? |Library )?"
            r"General Public License, version ([^ ]+?)[ .,;]",
            rex.IGNORECASE,
        ),
        r"\1",
    ),
    ReSub(
        rex.compile(
            r"version ([\d.]+?)[.,]? (?:\(?only\)?.? )?"
            r"(?:of the GNU (?:Affero )?"
            r"(?:Lesser(?:/Library)? |Library )?"
            r"General Public License )?(?:as )?published by the "
            r"Free Software Foundation",
            rex.IGNORECASE,
        ),
        r"\1",
    ),
    ReSub(
        rex.compile(
            r"GNU (?:Affero )?(?:Lesser(?:/Library)? |Library )?"
            r"General Public License (?:as )?published by the Free "
            r"Software Foundation[;,] version ([^ ]+?)[.,]? ",
            rex.IGNORECASE,
        ),
        r"\1",
    ),
    ReSub(
        rex.compile(
            r"(?:General Public License|GPL) (?:Vers.?ion )?([\d.]+?)[.,]? ",
            rex.IGNORECASE,
        ),
        r"\1",
    ),
)

GNU_EXCEPTION_SUBS: tuple[ReSub | ReSubGroup, ...] = (
    ReSub(
        rex.compile(
            r"As a special exception, if you create a document which uses "
            r"this font, and embed this font or unaltered portions of this "
            r"font into the document, this font does not by itself cause "
            r"the resulting document to be covered by the GNU General "
            r"Public License. This exception does not however invalidate "
            r"any other reasons why the document might be covered by the "
            r"GNU General Public License. If you modify this font, you may "
            r"extend this exception to your version of the font, but you "
            r"are not obligated to do so. If you do not wish to do so, "
            r"delete this exception statement from your version.",
            rex.IGNORECASE,
        ),
        r"Font",
    ),
    ReSubGroup(
        (
            ReSub(
                rex.compile(
                    r"under certain conditions as described in "
                    r"each individual source file",
                    rex.IGNORECASE,
                ),
                r"OpenSSL-cryptsetup",
            ),
            ReSub(
                rex.compile(
                    r"link the code of portions of this program with the OpenSSL"
                    r"\s+library\.\s+You must obey",
                    rex.IGNORECASE,
                ),
                r"OpenSSL-sqlitestudio",
            ),
            ReSub(
                rex.compile(
                    r"combining OpenSSL with KVIrc",
                    rex.IGNORECASE,
                ),
                r"OpenSSL-kvirc",
            ),
            ReSub(
                rex.compile(
                    r"permission for vsftpd to be linked to the OpenSSL",
                    rex.IGNORECASE,
                ),
                r"OpenSSL-vsftpd",
            ),
            ReSub(
                rex.compile(
                    r"permission to link the code of its release "
                    r"of x11vnc with the OpenSSL",
                    rex.IGNORECASE,
                ),
                r"OpenSSL-x11vnc",
            ),
            ReSub(
                rex.compile(
                    r"Special exception for linking OpenVPN with OpenSSL",
                    rex.IGNORECASE,
                ),
                r"openvpn-openssl",
            ),
            ReSub(
                rex.compile(
                    r"permission to dynamically link rsync with the OpenSSL",
                    rex.IGNORECASE,
                ),
                r"rsync-linking",
            ),
            ReSub(
                rex.compile(
                    r"Additional permission under GPLv3 section 7"
                    r".{1,400}"
                    r"linking or combining it with OpenSSL",
                    rex.IGNORECASE | rex.DOTALL,
                ),
                r"GPL-3-389-ds",
            ),
            ReSub(
                rex.compile(
                    r"As a special exception, the copyright holders give permission "
                    r"to link the code of portions of this program with the OpenSSL "
                    r"library",
                    rex.IGNORECASE,
                ),
                r"OpenSSL",
            ),
        )
    ),
    ReSubGroup(
        (
            ReSub(
                rex.compile(
                    r"(?:"
                    r"AUTOCONF CONFIGURE SCRIPT EXCEPTION"
                    r"|"
                    r"configuration script generated by Autoconf"
                    r".{1,250}"
                    r"section 7 of the GNU General Public License, version 3"
                    r")",
                    rex.IGNORECASE | rex.DOTALL,
                ),
                r"AutoConf-3",
            ),
            ReSub(
                rex.compile(
                    r"the respective Autoconf Macro[\' ]?s copyright owner "
                    r"gives unlimited permission",
                    rex.IGNORECASE,
                ),
                r"AutoConf-macro",
            ),
            ReSub(
                rex.compile(
                    r"(?:"
                    r"As a special exception to the GNU General Public License, "
                    r"if you distribute this file as part of a program that "
                    r"contains a configuration script generated by Autoconf"
                    r"|"
                    r"the Free Software Foundation gives unlimited permission to "
                    r"copy, distribute and modify the configure scripts that are "
                    r"the output of Autoconf\."
                    r")",
                    rex.IGNORECASE,
                ),
                r"AutoConf",
            ),
        )
    ),
    ReSubGroup(
        (
            ReSub(
                rex.compile(
                    r"when this file is copied by Bison into a Bison output "
                    r"file, you may use that output file without restriction",
                    rex.IGNORECASE,
                ),
                r"Bison-1.24",
            ),
            ReSub(
                rex.compile(
                    r"As a special exception, you may create a larger work that "
                    r"contains part or all of the Bison parser skeleton and "
                    r"distribute that work under terms of your choice",
                    rex.IGNORECASE,
                ),
                r"Bison",
            ),
        )
    ),
    ReSubGroup(
        (
            ReSub(
                rex.compile(
                    r"with the following modification:\s*"
                    r"As a special exception, the copyright holders of this "
                    r"library give you permission to link this library with "
                    r"independent modules to produce an executable",
                    rex.IGNORECASE | rex.DOTALL,
                ),
                r"Independent-modules",
            ),
            ReSub(
                rex.compile(
                    r"As a special exception, the copyright holders of this "
                    r"library give you permission to link this library with "
                    r"independent modules to produce an executable, regardless "
                    r"of the license terms of these independent modules, and to "
                    r"copy and distribute the resulting executable under terms "
                    r"of your choice, provided that you also meet, for each "
                    r"linked independent module, the terms and conditions of "
                    r"the license of that module\.",
                    rex.IGNORECASE,
                ),
                r"ClassPath",
            ),
        )
    ),
    ReSubGroup(
        (
            ReSub(
                rex.compile(
                    r"(?:"
                    r"GCC RUNTIME LIBRARY EXCEPTION"
                    r"|"
                    r"This GCC Runtime Library Exception"
                    r".{1,100}"
                    r"section 7"
                    r")",
                    rex.IGNORECASE | rex.DOTALL,
                ),
                r"GCC-3.1",
            ),
            ReSub(
                rex.compile(
                    r"In addition to the permissions in the GNU (?:Lesser )?"
                    r"General Public License, the Free Software Foundation "
                    r"gives you unlimited permission to link the compiled "
                    r"version of this file",
                    rex.IGNORECASE,
                ),
                r"GCC",
            ),
        )
    ),
    ReSub(
        rex.compile(
            r"As a special exception to the (?:[^ ]+ ){,5}if you "
            r"distribute this file as part of a program or library that "
            r"is built using GNU Libtool, you may include this file under "
            r"the same distribution terms that you use for the rest of that "
            r"program",
            rex.IGNORECASE,
        ),
        r"LibTool",
    ),
    ReSub(
        rex.compile(
            r"In addition, as a special exception, (?:[^\s]+ ){,8}"
            r"gives You the additional right to link the code of "
            r"this Program with code not covered under the GNU "
            r'General Public License \(?[" ]?Non[- ]?GPL Code[" ]?\)? and '
            r"to distribute linked combinations including the "
            r"two, subject to the limitations in this paragraph. "
            r"Non-GPL Code permitted under this exception must only "
            r"link to the code of this Program through those well "
            r"defined interfaces identified in the file named EXCEPTION "
            r'found in the source code files \(?the "?Approved '
            r'Interfaces[" ]?[\) ]?\. The files of Non[- ]?GPL Code may '
            r"instantiate templates or use macros or inline functions "
            r"from the Approved Interfaces without causing the resulting "
            r"work to be covered by the GNU General Public License. Only "
            r"(?:[^\s]+ ){,8}may make changes or additions to the list of "
            r"Approved Interfaces. You must obey the GNU General Public "
            r"License in all respects for all of the Program code and "
            r"other code used in conjunction with the Program except the "
            r"Non-GPL Code covered by this exception. If you modify this "
            r"file, you may extend this exception to your version of the "
            r"file, but you are not obligated to do so. If you do not "
            r"wish to provide this exception without modification, you "
            r"must delete this exception statement from your version and "
            r"license this file solely under the GPL without exception.",
            rex.IGNORECASE,
        ),
        r"389",
    ),
    ReSub(
        rex.compile(
            r"This copyright does NOT cover user programs that "
            r"run in CLISP and third-party packages not part "
            r"of CLISP, if a",
            rex.IGNORECASE,
        ),
        r"CLISP",
    ),
    ReSub(
        rex.compile(
            r'A FOSS application developer \([“" ]?you[”" ]? or '
            r'[“" ]?your[”" ]?\) may distribute a Derivative '
            r"Work provided that you and the Derivative Work "
            r"meet all of the following conditions",
            rex.IGNORECASE,
        ),
        r"DigiRule-FOSS",
    ),
    ReSub(
        rex.compile(
            r"As a special exception, if other files instantiate "
            r"templates or use macros or inline functions from "
            r"this file, or you compile this file and link "
            r"it with other works to produce a work based on "
            r"this file, this file does not by itself cause "
            r"the resulting work to be covered by the GNU General "
            r"Public License\. However the source code for "
            r"this file must still be made available in accordance "
            r"with section \(3\) of the GNU General Public License\.",
            rex.IGNORECASE,
        ),
        r"eCos-2",
    ),
    ReSub(
        rex.compile(
            r"The FLTK library and included programs are provided "
            r"under the terms of the GNU Library General Public "
            r"License \(LGPL\) with the following exceptions",
            rex.IGNORECASE,
        ),
        r"FLTK",
    ),
    ReSub(
        rex.compile(
            r"As a special exception, the copyright holder of "
            r"FreeRTOS gives you permission to link FreeRTOS "
            r"with independent modules that communicate with "
            r"FreeRTOS solely through the FreeRTOS API interface, "
            r"regardless of the license terms of these independent "
            r"modules, and to copy and distribute the resulting "
            r"combined work under terms of your choice, provided "
            r"that "
            r"Every copy of the combined work is accompanied by "
            r"a written statement that details to the recipient "
            r"the version of FreeRTOS used and an offer by yourself "
            r"to provide the FreeRTOS source code \(including any "
            r"modifications you may have made\) should the "
            r"recipient request it.",
            rex.IGNORECASE,
        ),
        r"FreeRTOS",
    ),
    ReSub(
        rex.compile(
            r"As a special exception, if you link this library "
            r"with other files to produce an executable, this "
            r"library does not by itself cause the resulting "
            r"executable to be covered by the GNU General Public "
            r"License.",
            rex.IGNORECASE,
        ),
        r"GNU-JavaMail",
    ),
    ReSub(
        rex.compile(
            r"In addition, as a special exception, .+"
            r"gives permission to link the code of this program "
            r"with the proprietary Java implementation provided "
            r"by Sun \(or other vendors as well\), and distribute "
            r"linked combinations including the two.",
            rex.IGNORECASE,
        ),
        r"i2p-Java",
    ),
    ReSub(
        rex.compile(r"Special exception for LZMA compression module", rex.IGNORECASE),
        r"LZMA",
    ),
    ReSub(
        rex.compile(
            r"if other files instantiate templates "
            r"or use macros or inline functions from this file, "
            r"or you compile this file and link it with other "
            r"files to produce an executable, this file does "
            r"not by itself cause the resulting executable "
            r"to be covered by the GNU General Public License.",
            rex.IGNORECASE,
        ),
        r"MIF",
    ),
    ReSub(
        rex.compile(
            r"Open CASCADE Exception \(version ([\d.]+)\) "
            r"to GNU ",
            rex.IGNORECASE,
        ),
        r"OCCT-\1",
    ),
    ReSub(
        rex.compile(
            r"unlimited permission to link or embed compiled bootloader"
            r" and related files into combinations with other programs",
            rex.IGNORECASE,
        ),
        r"Bootloader",
    ),
    ReSub(
        rex.compile(
            r"use specified GPL-licensed RRDtool",
            rex.IGNORECASE,
        ),
        r"RRDtool-FLOSS",
    ),
    ReSubGroup(
        (
            ReSub(
                rex.compile(r"The Qt Company Qt LGPL Exception", rex.IGNORECASE),
                r"Qt-LGPL-1.1",
            ),
            ReSub(
                rex.compile(r"Digia Qt LGPL Exception", rex.IGNORECASE),
                r"Digia-Qt",
            ),
            ReSub(
                rex.compile(
                    r"Qt LGPL Exception version ([\d.]+) "
                    r"As an additional permission to the GNU ",
                    rex.IGNORECASE,
                ),
                r"Qt\1",
            ),
        )
    ),
    ReSub(
        rex.compile(
            r"U-Boot License Exception:? "
            r"Even though ",
            rex.IGNORECASE,
        ),
        r"u-boot",
    ),
    ReSub(
        rex.compile(
            r"EXCEPTION NOTICE "
            r".{,4}As a special exception, the copyright holders "
            r"of this library give permission for additional "
            r"uses of the text contained in this release of "
            r"the library as licenced under the wxWindows "
            r"Library Licen[cs]e, applying either version 3.1 "
            r"of the Licen[cs]e, or \(at your option\) any later "
            r"version of the Licen[cs]e as published by the "
            r"copyright holders of version 3.1 of the Licen[cs]e "
            r"document\.",
            rex.IGNORECASE,
        ),
        r"WxWindows-3.1+",
    ),
    ReSub(
        rex.compile(
            r"If you modify this.{1,100}runtime libraries of Erlang/OTP",
            rex.IGNORECASE | rex.DOTALL,
        ),
        r"erlang-otp",
    ),
    ReSub(
        rex.compile(
            r"the Harbour Project gives permission for",
            rex.IGNORECASE,
        ),
        r"harbour",
    ),
    ReSub(
        rex.compile(
            r"Additional permission under the GNU Affero GPL version 3 "
            r"section 7",
            rex.IGNORECASE,
        ),
        r"romic",
    ),
    ReSub(
        rex.compile(
            r"Linking stunnel statically or dynamically with other modules",
            rex.IGNORECASE,
        ),
        r"stunnel",
    ),
    ReSub(
        rex.compile(
            r"if you link this library with other files,\s+"
            r"compiled with a Free Software compiler",
            rex.IGNORECASE | rex.DOTALL,
        ),
        r"SWI",
    ),
    ReSub(
        rex.compile(
            r"when this file is read by TeX",
            rex.IGNORECASE,
        ),
        r"Texinfo",
    ),
    ReSub(
        rex.compile(
            r"UNMODIFIED BINARY DISTRIBUTION LICENCE",
            rex.IGNORECASE,
        ),
        r"UBDL",
    ),
    ReSub(
        rex.compile(
            r"link this library with the CGAL library",
            rex.IGNORECASE,
        ),
        r"CGAL-linking",
    ),
    ReSub(
        rex.compile(
            r"work that uses the OCaml Core System",
            rex.IGNORECASE,
        ),
        r"OCaml-LGPL",
    ),
    ReSub(
        rex.compile(
            r"libpri may also be linked to the OpenH323 library",
            rex.IGNORECASE,
        ),
        r"libpri-OpenH323",
    ),
    ReSub(
        rex.compile(
            r"relaxation of clause 6 of the LGPL",
            rex.IGNORECASE,
        ),
        r"polyparse",
    ),
    ReSub(
        rex.compile(
            r"Qwt (?:library|License)",
            rex.IGNORECASE,
        ),
        r"Qwt-1",
    ),
    ReSub(
        rex.compile(
            r"Simple Library Usage License",
            rex.IGNORECASE,
        ),
        r"Simple-Library-Usage",
    ),
    ReSub(
        rex.compile(
            r"The Qt Company GPL Exception",
            rex.IGNORECASE,
        ),
        r"Qt-GPL-1",
    ),
    ReSub(
        rex.compile(
            r"As a special exception, if other files instantiate "
            r"generics from this",
            rex.IGNORECASE,
        ),
        r"GNAT",
    ),
    ReSub(
        rex.compile(
            r"compiled with a GNU compiler to produce an executable",
            rex.IGNORECASE,
        ),
        r"GNU-compiler",
    ),
    ReSub(
        rex.compile(
            r"GPL Cooperation Commitment",
            rex.IGNORECASE,
        ),
        r"GPL-CC",
    ),
    ReSub(
        rex.compile(
            r"non-gpl compatible GStreamer plugins",
            rex.IGNORECASE,
        ),
        r"GStreamer",
    ),
    ReSub(
        rex.compile(
            r"copyright holders of Gmsh give you permission to combine Gmsh",
            rex.IGNORECASE,
        ),
        r"Gmsh",
    ),
    ReSub(
        rex.compile(
            r"this copyright does (?:not|\*not\*) cover user programs that "
            r"use kernel\s+services by normal system calls",
            rex.IGNORECASE,
        ),
        r"Linux-syscall",
    ),
    ReSubGroup(
        (
            ReSub(
                rex.compile(
                    r"Additional permission under GNU GPL version 3 section 7"
                    r".{1,400}"
                    r"Corresponding Source for a non-source form",
                    rex.IGNORECASE | rex.DOTALL,
                ),
                r"GPL-3-linking-source",
            ),
            ReSub(
                rex.compile(
                    r"Additional permission under GNU GPL version 3 section 7",
                    rex.IGNORECASE,
                ),
                r"GPL-3-linking",
            ),
        )
    ),
    ReSub(
        rex.compile(
            r"released under the GNU LGPL and with independent modules "
            r"that communicate with",
            rex.IGNORECASE,
        ),
        r"GPL-3-interface",
    ),
    ReSub(
        rex.compile(
            r"authors of sane-airscan give permission",
            rex.IGNORECASE,
        ),
        r"SANE",
    ),
    ReSub(
        rex.compile(
            r"openjdk\.java\.net.*?with the following "
            r"clarification and special\s+exception",
            rex.IGNORECASE | rex.DOTALL,
        ),
        r"OpenJDK-assembly",
    ),
    ReSub(
        rex.compile(
            r"permission is granted to include these "
            r"font\s+programs in a Postscript or PDF file",
            rex.IGNORECASE | rex.DOTALL,
        ),
        r"PS-or-PDF-font",
    ),
    ReSub(
        rex.compile(
            r"In addition, when this program is distributed "
            r"with Asterisk in any form that would qualify",
            rex.IGNORECASE,
        ),
        r"Asterisk",
    ),
    ReSub(
        rex.compile(
            r"Specific permission is also granted to link "
            r"Asterisk with OpenSSL, OpenH323",
            rex.IGNORECASE,
        ),
        r"Asterisk-linking",
    ),
    ReSub(
        rex.compile(
            r"special exception to the GNU Lesser General Public License version 3.*?"
            r"Combined Work that links statically or dynamically.*?"
            r"without providing any Minimal Corresponding",
            rex.IGNORECASE | rex.DOTALL,
        ),
        r"LGPL-3-linking",
    ),
    ReSub(
        rex.compile(
            r"we declare that, for the purpose applying the LGPL to the Library, "
            r"an executable that results from linking a work that uses the Library "
            r"with the Library is considered a work that uses the Library "
            r"and is therefore NOT covered by the LGPL",
            rex.IGNORECASE,
        ),
        r"LLGPL",
    ),
)

UNIVERSAL_EXCEPTION_SUBS: tuple[ReSub | ReSubGroup, ...] = (
    ReSub(
        rex.compile(r"The Universal FOSS Exception", rex.IGNORECASE),
        r"Universal-FOSS",
    ),
)

APACHE_EXCEPTION_SUBS: tuple[ReSub | ReSubGroup, ...] = (
    ReSub(
        rex.compile(
            r"As an exception.*?portions of this Software "
            r"are embedded into the binary product",
            rex.IGNORECASE | rex.DOTALL,
        ),
        r"Swift",
    ),
    ReSubGroup(
        (
            ReSub(
                rex.compile(
                    r"Solderpad Hardware Licen[cs]e\s+Version\s+2\.1",
                    rex.IGNORECASE,
                ),
                r"SHL-2.1",
            ),
            ReSub(
                rex.compile(
                    r"Solderpad Hardware Licen[cs]e.*?wraparound licen[cs]e",
                    rex.IGNORECASE | rex.DOTALL,
                ),
                r"SHL-2",
            ),
        )
    ),
    ReSub(
        rex.compile(
            r"LLVM Exceptions to the Apache 2\.0 License",
            rex.IGNORECASE,
        ),
        r"LLVM",
    ),
    ReSub(
        rex.compile(
            r"Mini-XML.*?Exceptions to the Apache 2\.0 License",
            rex.IGNORECASE | rex.DOTALL,
        ),
        r"mxml",
    ),
)
MIT_EXCEPTION_SUBS: tuple[ReSub | ReSubGroup, ...] = (
    ReSub(
        rex.compile(
            r"As an exception.*?portions\s+of this Software "
            r"are embedded into a machine-executable object form",
            rex.IGNORECASE | rex.DOTALL,
        ),
        r"fmt",
    ),
)
CC_BY_SA_EXCEPTION_SUBS: tuple[ReSub | ReSubGroup, ...] = (
    ReSub(
        rex.compile(
            r"As a special exception.*?permission to copy, "
            r"modify, and distribute the example code"
            r".*?without restriction",
            rex.IGNORECASE | rex.DOTALL,
        ),
        r"GNOME-examples",
    ),
    ReSub(
        rex.compile(
            r"waives article 3 of the license with respect to these designs",
            rex.IGNORECASE,
        ),
        r"KiCad-libraries",
    ),
)
QPL_EXCEPTION_SUBS: tuple[ReSub | ReSubGroup, ...] = (
    ReSub(
        rex.compile(
            r"special exception to the Q Public Licen[cs]e.*?"
            r"without any of the additional requirements listed in clause 6c",
            rex.IGNORECASE | rex.DOTALL,
        ),
        r"QPL-INRIA-2004",
    ),
)
BSD_EXCEPTION_SUBS: tuple[ReSub | ReSubGroup, ...] = (
    ReSub(
        rex.compile(
            r"EXEMPTION FOR BINARY LIBRARY-LIKE PACKAGES",
            rex.IGNORECASE,
        ),
        r"PCRE2",
    ),
)

# Order matters: the matches are concatenated, so the no-invariants tag
# precedes the version and the "+" suffix trails it (e.g. GFDL-NIV-1.2+).
GFDL_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"with no Invariant Sections",
            rex.IGNORECASE,
        ),
        r"-NIV",
    ),
    ReSub(
        rex.compile(
            r"GNU Free Documentation License[,.; ]*(?:Version|v)? ?"
            r"(\d+(?:\.[\d]+)*)",
            rex.IGNORECASE,
        ),
        r"-\1",
    ),
    ReSub(
        rex.compile(
            r"or any later version",
            rex.IGNORECASE,
        ),
        r"+",
    ),
)

LPPL_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"LaTeX Project Public License"
            r"(?: Distributed from CTAN archives in directory "
            r"macros/latex/base/lppl\.txt)?"
            # The titled form "...License LPPL Version 1.3a" carries the letter
            # suffix that the boilerplate "either version 1.3" reference drops.
            r"(?: \(?LPPL\)?)?"
            r"[,;]? (?:either )?(?:version|v) "
            r"?([\d.]+[a-z]?)[; ]",
            rex.IGNORECASE,
        ),
        r"-\1",
    ),
    ReSub(
        rex.compile(
            r"LaTeX Project Public License,? (?:[^ ]+ ){,16}or "
            r"(?:\(?at your option\)? )?any later version",
            rex.IGNORECASE,
        ),
        r"+",
    ),
)

MIT_SUBS: tuple[ReSub, ...] = (
    # HPND ("historical permission notice") variants. The HP forms differ only
    # in how many holders they name: the 1986 form keeps the notice "in all
    # copies", the 1989 form "in all source code copies" naming several holders.
    ReSub(
        rex.compile(
            r"this notice appears in all copies, and that the name of",
            rex.IGNORECASE,
        ),
        "MIT-like-HP-1986",
    ),
    ReSub(
        rex.compile(
            r"this notice appears in all source code copies, and that none of "
            r"the names",
            rex.IGNORECASE,
        ),
        "MIT-like-HP-1989",
    ),
    ReSub(
        rex.compile(
            r"distribute, and sell this documentation for any purpose is hereby "
            r"granted",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-doc-sell",
    ),
    ReSub(
        rex.compile(
            r"provided that this copyright and permissions notice appear in all "
            r"copies and derivatives",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-Kevlin-Henney",
    ),
    ReSub(
        rex.compile(
            r"this notice appears in all copies\. This file is distributed "
            r"WITHOUT ANY WARRANTY",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-merchantability-variant",
    ),
    ReSub(
        rex.compile(
            r"available with usual research terms with the aim of retain "
            r"credits of the software",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-INRIA-IMAG",
    ),
    ReSub(
        rex.compile(
            r"sell this software and its documentation for any purpose is "
            r"hereby granted without fee, provided that the above copyright "
            r"notice appear in all copies\. This software is provided",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-sell-regexpr",
    ),
    ReSub(
        rex.compile(
            r"provided that this permission notice appear in supporting "
            r"documentation\. This permission notice shall be included in all "
            r"copies or substantial portions",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-sell-MIT-disclaimer-xserver",
    ),
    # The base "sell" variant: the advertising clause ("name of … not be used
    # in advertising") is what separates it from the plain X11 "sell" grant,
    # which carries no such clause and stays the generic family name.
    ReSub(
        rex.compile(
            r"sell this software and its documentation for any purpose is "
            r"hereby granted without fee, provided that the above copyright "
            r"notice appears in all copies and that both that copyright notice "
            r"and this permission notice appear in supporting documentation, "
            r"and that the name of .{0,30} not be used in advertising",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-sell-variant",
    ),
    # The schema variant of the DocBook grant (cf. the stylesheet rule); the
    # "in perpetuity" + "schema" wording is unique to it.
    ReSub(
        rex.compile(
            r"schema and its accompanying documentation for any purpose and "
            r"without fee is hereby granted in perpetuity",
            rex.IGNORECASE,
        ),
        "MIT-like-DocBook-Schema",
    ),
    # Each licensed portion must carry a "copying is by permission of <holder>"
    # notice in its documentation - distinctive among the MIT/X11-style grants.
    ReSub(
        rex.compile(
            r"Notice must be given in supporting documentation that copying "
            r"distribution is by permission of",
            rex.IGNORECASE,
        ),
        "MIT-like-LOOP",
    ),
    ReSub(
        rex.compile(
            r"NONINFRINGEMENT OF THIRD PARTY RIGHTS\. IN NO EVENT SHALL THE "
            r"AUTHORS BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL",
            rex.IGNORECASE,
        ),
        "MIT-like-clips",
    ),
    ReSub(
        rex.compile(
            r"The above copyright notice and this permission notice shall "
            r"be included in all copies of the Software, its documentation "
            r"and marketing (?:&? )?publicity materials, and acknowledgment "
            r"shall be given in the documentation, materials and software "
            r"packages that this Software was used.",
            rex.IGNORECASE,
        ),
        "MIT-advertising",
    ),
    ReSub(
        rex.compile(
            r"Permission to use, copy, modify, and distribute this software "
            r"and its documentation for any purpose and without fee is "
            r"hereby granted, provided that the above copyright notice "
            r"appear in all copies and that both that the copyright notice "
            r"and this permission notice and warranty disclaimer appear in "
            r"supporting documentation, and that the name of the author not "
            r"be used in advertising or publicity pertaining to distribution "
            r"of the software without specific, written prior permission.",
            rex.IGNORECASE,
        ),
        "Expat-like-Highscore",
    ),
    ReSub(
        rex.compile(
            r"Except as contained in this notice, the name of a copyright "
            r"holder shall not be used in advertising or otherwise to "
            r"promote the sale, use or other dealings in this Software "
            r"without prior written authorization of the copyright holder.",
            rex.IGNORECASE,
        ),
        "MIT-like-icu",
    ),
    ReSub(
        rex.compile(
            r"Except as contained in this notice, the name of a copyright "
            r"holder shall not be used in advertising or otherwise to "
            r"promote the sale, use or other dealings in this Software "
            r"without prior written authorization of the copyright holder.",
            rex.IGNORECASE,
        ),
        "MIT-like-icu",
    ),
    ReSub(
        rex.compile(
            r"The above copyright notice and this permission notice shall "
            r"be included in all copies of the Software and its "
            r"Copyright notices. In addition publicly documented "
            r"acknowledgment must be given that this software has "
            r"been used if no source code of this software is "
            r"made available publicly. This includes acknowledgments "
            r"in either Copyright notices, Manuals, Publicity and "
            r"Marketing documents or any documentation provided "
            r"with any product containing this software. This "
            r"License does not apply to any software that links "
            r"to the libraries provided by this software \(?statically "
            r"or dynamically[\) ]?, but only to the software provided.",
            rex.IGNORECASE,
        ),
        "MIT-enna",
    ),
    ReSub(
        rex.compile(
            r"The above copyright notice and this permission "
            r"notice shall be included in all copies of the "
            r"Software and its documentation and acknowledgment "
            r"shall be given in the documentation and software "
            r"packages that this Software was used.",
            rex.IGNORECASE,
        ),
        "MIT-feh",
    ),
    ReSub(
        rex.compile(
            r"Distributions of all or part of the Software intended to be "
            r"used by the recipients as they would use the unmodified "
            r"Software, containing modifications that substantially alter, "
            r"remove, or disable functionality of the Software, "
            r"outside of the documented configuration mechanisms "
            r"provided by the Software, shall be modified such "
            r"that the Original Author[\' ]?s bug reporting "
            r"email addresses and urls are either replaced "
            r"with the contact information of the parties responsible "
            r"for the changes, or removed entirely.",
            rex.IGNORECASE,
        ),
        "MITNFA",
    ),
    ReSub(
        rex.compile(
            r"The above copyright notice and this permission notice shall "
            r"be included in all copies of the Software and its Copyright "
            r"notices. In addition publicly documented acknowledgment must "
            r"be given that this software has been used if no source code "
            r"of this software is made available publicly. Making the source "
            r"available publicly means including the source for this "
            r"software with the distribution, or a method to get this "
            r"software via some reasonable mechanism \(electronic transfer "
            r"via a network or media\) as well as making an offer to supply "
            r"the source on request. This Copyright notice serves as an "
            r"offer to supply the source on (?:on )?request as well. Instead "
            r"of this, supplying acknowledgments of use of this software in "
            r"either Copyright notices, Manuals, Publicity and Marketing "
            r"documents or any documentation provided with any product "
            r"containing this software. This License does not apply to any "
            r"software that links to the libraries provided by this "
            r"software \(statically or dynamically\), but only to the "
            r"software provided.",
            rex.IGNORECASE,
        ),
        "MIT-Imlib2",
    ),
    ReSub(
        rex.compile(
            r"The above copyright notice including the dates of first "
            r"publication and either this permission notice or a reference "
            r"to https?[: ]/?/?oss.sgi.com/projects/FreeB/ shall be "
            r"included in all copies or substantial portions of the "
            r"Software\.",
            rex.IGNORECASE,
        ),
        "SGI-B-2",
    ),
    ReSub(
        rex.compile(
            r"The above copyright notice including the dates of first "
            r"publication and either this permission notice or a reference "
            r"to https?[: ]/?/?oss.sgi.com/projects/FreeB/ shall be "
            r"included in all copies or substantial portions of the "
            r"Software\.",
            rex.IGNORECASE,
        ),
        "SGI-B-2",
    ),
    ReSub(
        rex.compile(
            r"Permission to use, copy, modify, and distribute this software "
            r"and its documentation for any purpose and without fee is "
            r"hereby granted, provided that the above copyright notice "
            r"appear in all copies and that both the copyright notice and "
            r"this permission notice and warranty disclaimer appear in "
            r"supporting documentation, and that the name of "
            r"(?:[^ ]+ ){,16}not be used in advertising or publicity "
            r"pertaining to distribution of the software without specific, "
            r"written prior permission\. "
            r"(?:[^ ]+ ){,4}disclaims all warranties with regard to this "
            r"software, including all implied warranties of merchantability "
            r"and fitness. In no event shall (?:[^ ]+ ){,4}be liable for "
            r"any special, indirect or consequential damages or any damages "
            r"whatsoever resulting from loss of use, data or profits, "
            r"whether in an action of contract, negligence or other "
            r"tortious action, arising out of or in connection with the use "
            r"or performance of this software\.",
            rex.IGNORECASE,
        ),
        "MIT-like-SMLNJ",
    ),
    ReSub(
        rex.compile(
            r"The end-user documentation included with the redistribution, "
            r'if any, must include the following acknowledgment:? "?This '
            r"product includes software developed by The XFree86 Project, "
            r"Inc \(https?[: ]/?/?www.xfree86.org[/ ]?\) and its "
            r'contributors[" ]?, in the same place and form as other '
            r"third-party acknowledgments\. Alternately, this acknowledgment "
            r"may appear in the software itself, in the same form and "
            r"location as other such third-party acknowledgments\. ",
            rex.IGNORECASE,
        ),
        "XFree86-1.1",
    ),
    ReSub(
        rex.compile(
            r"Permission to use, copy, modify, and distribute this software and its "
            r"documentation for any purpose and without fee is hereby granted, "
            r"provided that the above copyright notice appear in all copies and that "
            r"both that copyright notice and this permission notice appear in "
            r"supporting documentation\. No representations are made about the "
            r"suitability of this software for any purpose\. It is provided "
            r"[\"]?as is[\"]? without express or implied warranty\."
        ),
        "MIT-like-HPND-Netrek",
    ),
    ReSub(
        rex.compile(
            r"Historical Permission Notice and Disclaimer"
            r".{,500}without fee is hereby granted",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND",
    ),
    ReSub(
        rex.compile(
            r"if the software is modified in a manner creating derivative "
            r"copyright rights, appropriate legends may be placed on the "
            r"derivative work",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-DEC",
    ),
    # HPND named variants: each anchors on a substantive clause unique to it,
    # so it is named at SPDX granularity instead of collapsing to plain MIT/X11.
    ReSub(
        rex.compile(
            r"the author disclaims all warranties with regard to this software\.",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-Markus-Kuhn",
    ),
    ReSub(
        rex.compile(
            r"Export of this software outside of the United States of America "
            r"may require an export license",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-UC-export-US",
    ),
    ReSub(
        rex.compile(
            r"distribute this documentation for any purpose",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-doc",
    ),
    ReSub(
        rex.compile(
            r"if you modify this software you must label your software as "
            r"modified software",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-export-US-modify",
    ),
    ReSub(
        rex.compile(
            r"WITHIN THAT CONSTRAINT.{0,1500}PROVIDED AS IS AND WITHOUT ANY "
            r"EXPRESS OR IMPLIED WARRANTIES",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-export2-US",
    ),
    ReSub(
        rex.compile(
            r"THIS SOFTWARE IS NOT DESIGNED FOR USE IN SAFETY CRITICAL SYSTEMS",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-sell-variant-critical-systems",
    ),
    ReSub(
        rex.compile(
            r"appear in supporting documentation\.\s+Disclaimer\s+"
            r"THE SOFTWARE IS PROVIDED AS IS",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-sell-variant-MIT-disclaimer",
    ),
    ReSub(
        rex.compile(
            r"dealings in the software\.\s+Permission to use, copy, modify, "
            r"distribute, and sell",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-sell-variant-MIT-disclaimer-rev",
    ),
    ReSub(
        rex.compile(
            r"original copyright notices appear in all copies.{0,400}"
            r"THE SOFTWARE IS PROVIDED AS IS , WITHOUT WARRANTY OF ANY KIND",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-MIT-disclaimer",
    ),
    # The variants below share their distinctive text with a sibling that has a
    # rule above, so they must stay after it: HPND-UC after HPND-UC-export-US,
    # and HPND-export-US after HPND-export-US-modify / HPND-export2-US.
    ReSub(
        rex.compile(
            r"appear in supporting documentation\. This software is provided "
            r"as is without express or implied warranty",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-Pbmplus",
    ),
    ReSub(
        rex.compile(
            r"notice be given in supporting documentation that copying and "
            r"distribution is by permission",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-Fenneberg-Livingston",
    ),
    ReSub(
        rex.compile(
            r"and without fee is hereby granted, provided that the above "
            r"copyright notice appear in all copies\.\s",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-UC",
    ),
    ReSub(
        rex.compile(
            r"WITHIN THAT CONSTRAINT.{0,800}makes no representations about the "
            r"suitability of this software for any purpose\. It is provided as is",
            rex.IGNORECASE,
        ),
        "MIT-like-HPND-export-US",
    ),
    # MIT/Expat-grant variants routed here via the bare MIT/X11 grant rules; each
    # anchors on a distinctive clause. MIT-0 also matches Clips, which is fine:
    # the Clips rule is first in this list and wins for the Clips text.
    ReSub(
        rex.compile(
            r"to permit persons to whom the Software is furnished to do so\. "
            r"THE SOFTWARE IS PROVIDED AS IS , WITHOUT WARRANTY OF ANY KIND",
            rex.IGNORECASE,
        ),
        "MIT-0",
    ),
    ReSub(
        rex.compile(
            r"MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY "
            r"REFLECTS",
            rex.IGNORECASE,
        ),
        "MIT-Khronos-old",
    ),
    ReSub(
        rex.compile(
            r"without license or royalty fees, to use, copy, modify, and "
            r"distribute this software and its documentation for any purpose, "
            r"provided that the above copyright notice and the following two "
            r"paragraphs",
            rex.IGNORECASE,
        ),
        "MIT-Modern-Variant",
    ),
    ReSub(
        rex.compile(
            r"asked to send the modifications to the original developer so "
            r"that they can be incorporated into the canonical version",
            rex.IGNORECASE,
        ),
        "MIT-STK",
    ),
    ReSub(
        rex.compile(
            r"appear in supporting documentation\. The above copyright notice "
            r"and this permission notice shall be included in all copies or "
            r"substantial portions",
            rex.IGNORECASE,
        ),
        "MIT-open-group",
    ),
    # More X11/MIT-style variants, each anchored on a distinctive clause. The
    # X11-* ids follow SPDX directly; the others use the MIT-like-<id> form.
    ReSub(
        rex.compile(
            r"THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU AS-IS",
            rex.IGNORECASE,
        ),
        "MIT-like-SGI-OpenGL",
    ),
    ReSub(
        rex.compile(
            r"this permission and disclaimer notice appear in supporting "
            r"documentation",
            rex.IGNORECASE,
        ),
        "MIT-like-UMich-Merit",
    ),
    ReSub(
        rex.compile(
            r"REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY OF THIS "
            r"MATERIAL",
            rex.IGNORECASE,
        ),
        "MIT-like-metamail",
    ),
    ReSub(
        rex.compile(
            r"no liability with respect to the infringement of copyrights, "
            r"trade secrets or any patents",
            rex.IGNORECASE,
        ),
        "MIT-like-xlock",
    ),
    ReSub(
        rex.compile(
            r"distribute with modifications, sublicense",
            rex.IGNORECASE,
        ),
        "X11-distribute-modifications-variant",
    ),
    ReSub(
        rex.compile(
            r"sell copies of the Software\. The above copyright notice and "
            r"this permission notice shall be included",
            rex.IGNORECASE,
        ),
        "X11-no-permit-persons",
    ),
    ReSub(
        rex.compile(
            r"shall be included in all copies or substantial portions of the "
            r"Software\. Except as contained in this notice",
            rex.IGNORECASE,
        ),
        "X11-swapped",
    ),
)

ZPL_SUBS: tuple[ReSub, ...] = (
    ReSub(
        rex.compile(
            r"(?:the )?Zope Public License(?: \(?ZPL\)?)?"
            r",? (?:Version|v)?[ ]?([\d.]+?)(?:[.0]*)[^\d.]",
            rex.IGNORECASE,
        ),
        r"-\1",
    ),
)
