"""CLI wrapper that delegates to the bundled Rust binary."""

from __future__ import annotations

import os
import subprocess
import sys
from pathlib import Path


def _bin_path() -> Path:
    base = Path(__file__).resolve().parent
    exe = "falcorn.exe" if sys.platform.startswith("win") else "falcorn"
    return base / "bin" / exe


def _run_bin(args: list[str]) -> int:
    env = os.environ.copy()
    bin_path = _bin_path()
    if not bin_path.exists():
        sys.stderr.write(
            "falcorn CLI binary not found. "
            "Reinstall with a wheel that includes the CLI or build from source.\n"
        )
        return 1

    proc = subprocess.run([str(bin_path), *args], env=env)
    return proc.returncode


def main() -> None:
    raise SystemExit(_run_bin(sys.argv[1:]))
