#!/usr/bin/env python3

import argparse
import csv
import io
import logging
import re
import sys
from datetime import datetime
from datetime import UTC
from pathlib import Path
from urllib.request import urlopen

SCHEMES_URL = "https://www.iana.org/assignments/uri-schemes/uri-schemes-1.csv"

BOILER_PLATE = """# This file is part of Gajim.
#
# SPDX-License-Identifier: GPL-3.0-only

# Generated by get_iana_data.py @ {current_date}

"""

logging.basicConfig(level="INFO", format="%(levelname)s: %(message)s")
log = logging.getLogger(__name__)


def download_file(url: str) -> str:
    log.info("Download: %s", url)
    with urlopen(url) as f:
        content = f.read()
    return content.decode()


def parse_uri_schemes(content: str) -> list[str]:
    log.info("Parse uri schemes")
    schemes: list[str] = []

    reader = csv.reader(io.StringIO(content), delimiter=",")
    next(reader)  # Skip header row
    for line in reader:
        scheme = line[0].lower().removesuffix(" (obsolete)")
        if not re.fullmatch("[a-z0-9+.-]+", scheme):
            log.warning("unexpected scheme field contents: %s", scheme)
            continue

        schemes.append(scheme)

    return schemes


def generate_output(schemes: list[str], outpath: Path) -> None:
    log.info("Generate output")
    current_date = datetime.now(tz=UTC).isoformat()
    content = BOILER_PLATE.format(current_date=current_date)

    content += "URI_SCHEMES = {\n"
    for scheme in schemes:
        content += f"    '{scheme}',\n"
    content += "}\n"

    outpath.write_text(content)
    log.info("Wrote file to %s", outpath)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Generate IANA data")
    parser.add_argument("out", help="Path to iana module")
    args = parser.parse_args()

    outpath = Path(args.out)
    if not outpath.is_dir():
        sys.exit("Output path is not a directory")

    scheme_content = download_file(SCHEMES_URL)
    schemes = parse_uri_schemes(scheme_content)

    generate_output(schemes, outpath / "uri_schemes.py")
    log.info("Finished !")
