#!/usr/bin/env python3
import argparse

from hil_common import load_config_if_present, main_guard, new_artifact_dir, pio_command, run_checked


def main() -> int:
    parser = argparse.ArgumentParser(description="Build the configured HIL firmware without accessing serial ports")
    parser.add_argument("--artifact-dir")
    args = parser.parse_args()
    config = load_config_if_present()
    environment = config.get("hil", {}).get("environment", "hil")
    artifact = new_artifact_dir("build") if not args.artifact_dir else __import__("pathlib").Path(args.artifact_dir)
    artifact.mkdir(parents=True, exist_ok=True)
    run_checked([pio_command(), "run", "-e", environment], log=artifact / "build.log")
    print(f"Build log: {artifact / 'build.log'}")
    return 0


if __name__ == "__main__":
    main_guard(main)
