import posixpath


def compute_canonical_url(category, unit, version, jurisdiction_code):
    """
    Compute the unique canonical URL for a license with the given attributes.
    Note that a "Tool" can have a jurisdiction, but is language-independent
    (unlike a "LegalCode", which is associated with a language).

    E.g.

    https://creativecommons.org/licenses/BSD/
    https://creativecommons.org/licenses/GPL/2.0/
    https://creativecommons.org/licenses/LGPL/2.1/
    https://creativecommons.org/licenses/MIT/
    https://creativecommons.org/licenses/by/2.0/
    https://creativecommons.org/licenses/publicdomain/
    https://creativecommons.org/publicdomain/zero/1.0/
    https://creativecommons.org/publicdomain/mark/1.0/
    https://creativecommons.org/licenses/nc-sampling+/1.0/
    https://creativecommons.org/licenses/devnations/2.0/
    https://creativecommons.org/licenses/by/3.0/nl/
    https://creativecommons.org/licenses/by-nc-nd/3.0/br/
    https://creativecommons.org/licenses/by/4.0/
    https://creativecommons.org/licenses/by-nc-nd/4.0/
    """
    base = "https://creativecommons.org"

    if unit in ["BSD", "MIT"]:
        canonical_url = posixpath.join(
            base,
            category,
            unit,
        )
    else:
        canonical_url = posixpath.join(
            base,
            category,
            unit,
            version,
        )
    if jurisdiction_code:
        canonical_url = posixpath.join(canonical_url, jurisdiction_code)
    canonical_url = posixpath.join(canonical_url, "")
    return canonical_url