#!/usr/bin/env python3
"""Run the project-local CMake for the current Linux architecture."""

from __future__ import annotations

import os
import platform
import subprocess
import sys
from pathlib import Path

ARCH_ALIASES = {
    "amd64": "x86_64",
    "x86_64": "x86_64",
    "arm64": "aarch64",
    "aarch64": "aarch64",
}


def main() -> int:
    if platform.system().lower() != "linux":
        print("scripts/bin/cmake supports project-local Linux tools only", file=sys.stderr)
        return 127

    arch = ARCH_ALIASES.get(platform.machine().lower().replace("-", "_"))
    if arch is None:
        print(f"unsupported architecture for project-local cmake: {platform.machine()}", file=sys.stderr)
        return 127

    tool = Path(__file__).resolve().parent / f"linux-{arch}" / "cmake" / "bin" / "cmake"
    if not tool.is_file():
        print(
            f"project-local cmake not found: {tool}\n"
            "Install the matching Linux amd64/arm64 CMake bundle under scripts/bin/linux-<arch>/cmake/.",
            file=sys.stderr,
        )
        return 127

    if not os.access(tool, os.X_OK):
        print(f"project-local cmake is not executable: {tool}", file=sys.stderr)
        return 126

    completed = subprocess.run([str(tool), *sys.argv[1:]], check=False)
    return completed.returncode


if __name__ == "__main__":
    sys.exit(main())
