#  boots-from -- reproducible bake wrapper.
#
#  Targets:
#    deps     -- print the apt/dnf one-liner the CLI's preflight would.
#    build    -- bake the ISO (default URL: http://bty-server/boot,
#                override with ``make build URL=...``).
#    format   -- pre-commit run --all-files (auto-fixes; non-zero
#                exit if anything changed). Single source of truth
#                for lint + format -- no separate ``lint`` target.
#    test     -- pytest.
#    ci       -- format + test (the gate every commit must pass;
#                same command CI runs).
#    smoke    -- boot the freshly-built ISO in QEMU under BOTH
#                BIOS and UEFI firmware, assert iPXE loads + runs
#                the embedded script + falls into the shell on the
#                expected chain failure. Release-gate validation.
#    smoke-bios / smoke-uefi -- the same, split for debugging.
#    clean    -- remove ./boots-from.iso + cijoe-output/.
#    distclean -- also remove the iPXE source cache (forces re-clone).
#    help     -- this list.

URL ?= http://bty-server/boot

.PHONY: help deps build format test ci smoke smoke-bios smoke-uefi clean distclean

help:
	@awk 'BEGIN{FS=":.*##"} /^[a-zA-Z][a-zA-Z0-9_-]*:.*##/ {printf "  %-10s %s\n", $$1, $$2}' $(MAKEFILE_LIST)

deps: ## print apt/dnf install line (run yourself; needs sudo)
	@uv run python -c 'from boots_from.build import detect_host_family, install_hint; print(install_hint(detect_host_family()))'

build: ## bake the ISO (URL=http://... to override the chain URL)
	uv run boots-from $(URL)

format: ## run every pre-commit hook on every file (auto-fixes in place; non-zero exit if anything changed)
	uv run pre-commit run --all-files --show-diff-on-failure

test: ## pytest
	uv run pytest

ci: format test ## format-check + test (the pre-merge gate; same command CI runs)

smoke: smoke-bios smoke-uefi ## boot the ISO in QEMU under BIOS + UEFI (release-gate)

smoke-bios: boots-from.iso ## QEMU smoke-test the BIOS boot path
	cijoe tasks/smoke-bios.yaml -c configs/smoke.toml --output cijoe-output/bios --monitor

smoke-uefi: boots-from.iso ## QEMU smoke-test the UEFI boot path
	cijoe tasks/smoke-uefi.yaml -c configs/smoke.toml --output cijoe-output/uefi --monitor

boots-from.iso:
	$(MAKE) build

clean: ## rm ./boots-from.iso + cijoe-output/
	rm -f boots-from.iso
	rm -rf cijoe-output cijoe-archive

distclean: clean ## also remove ~/.cache/boots-from
	rm -rf $${HOME}/.cache/boots-from
