#!/usr/bin/env python3
"""
Check the number of versions of metadata
present for framework bindings.
"""
import pathlib

ROOT = pathlib.Path(__file__).resolve().parent.parent

WHITELIST = {"pyobjc-framework-PubSub"}  # removed in macOS 10.14

incorrect = 0
versions_seen = set()

for p in sorted(ROOT.glob("pyobjc-framework-*")):
    if not (p / "metadata").exists():
        continue

    for fwk in (p / "metadata").glob("raw*"):
        versions = set()

        for f in fwk.iterdir():
            _, _, version = f.name.partition("-")
            versions.add(version[:-7])

        if p.name not in WHITELIST:
            versions_seen.update(versions)

        if len(versions) != 1:
            if fwk.name == "raw":
                print(f"{p.name}: {len(versions)} metadata scans")
            else:
                print(f"{p.name}/{fwk.name}: {len(versions)} metadata scans")

            if p.name not in WHITELIST:
                incorrect += 1

if len(versions_seen) != 1:
    print(f"Multiple metadata versions seen: {', '.join(sorted(versions_seen))}")
if incorrect:
    print(f"{incorrect} frameworks with too much metadata")

if incorrect or len(versions_seen) != 2:
    # XXX: ^^^ Test for 2 versions_seen while transitioning to macOS 26 SDK
    raise SystemExit(1)
