#!/usr/bin/env python3
"""Regenerate the editor definitions of the C bridge, trxc.*.

The definitions come from the `// trxc.` signature above each C function a
module registers; see tools/shared/trxc.py for the format. A registered
function without a signature fails the hook.
"""

import os
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[3]
OUTPUT = ROOT / "tools/lint/emmylua/trxc.lua"

sys.path.insert(0, str(Path(__file__).resolve().parents[2]))

from shared.trxc import read_tree, to_meta  # noqa: E402


def main() -> int:
    os.chdir(ROOT)
    signatures, problems = read_tree(Path("src/trx"))
    for problem in problems:
        print(problem, file=sys.stderr)
    text = to_meta(signatures, "tools/lint/gen/trxc_meta")
    if not OUTPUT.exists() or OUTPUT.read_text(encoding="utf-8") != text:
        OUTPUT.write_text(text, encoding="utf-8")
    return 1 if problems else 0


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