#!/usr/bin/env python3

from __future__ import annotations

import sys

from _examples_build_lib import (
    build_projects,
    filter_by_framework,
    find_platformio_projects,
    make_argparser,
    repo_root,
)


def main(argv: list[str]) -> int:
    parser = make_argparser("Build Zephyr PlatformIO example projects under examples/ (all envs).")
    args = parser.parse_args(argv)

    examples_dir = (repo_root() / args.examples_dir).resolve()
    if not examples_dir.exists():
        print(f"Examples dir not found: {examples_dir}", file=sys.stderr)
        return 2

    projects = find_platformio_projects(examples_dir)
    projects = filter_by_framework(projects, framework="zephyr")
    if not projects:
        print(f"No Zephyr projects found under: {examples_dir}", file=sys.stderr)
        return 2

    if args.list:
        for project_dir in projects:
            print(str(project_dir.relative_to(repo_root())))
        return 0

    return build_projects(
        projects,
        verbose=args.verbose,
        log_dir=args.log_dir,
        tail_lines=args.tail,
        quiet=args.quiet,
        firmware_out=args.firmware_out,
    )


if __name__ == "__main__":
    raise SystemExit(main(sys.argv[1:]))
