# ─────────────────────────────────────────────────────────────────────────────
# Arcline OS — top-level build orchestration
#
# The Makefile is a thin, readable wrapper around the scripts/ pipeline. It
# exists so that "make iso-server" is the one command that takes a host with
# the right build deps to a bootable Arcline OS ISO.
#
# Everything that has real logic lives in scripts/ — this file only wires
# editions to targets and passes through environment.
#
# Quick reference:
#   make help                 → this message
#   make deps                 → install host build dependencies
#   make check                → validate manifests + shell syntax (fast, safe)
#   make rootfs-<edition>     → build just the rootfs for an edition
#   make iso-<edition>        → build a bootable ISO for an edition
#   make iso                  → build all editions
#   make toolchain            → build the 11 Arcline Go tools into .deb
#   make test                 → run the smoke tests against a built image
#   make clean                → wipe build/
# ─────────────────────────────────────────────────────────────────────────────

SHELL := /bin/bash
.DEFAULT_GOAL := help

include versions.mk

# Pass edition + env through to the scripts.
export DISTRO_NAME DISTRO_ID VERSION RELEASE_NAME
export DEBIAN_SUITE DEBIAN_MIRROR SECURITY_MIRROR ARCH
export KERNEL_PACKAGE KERNEL_VERSION BOOT
export BUILD_DIR ROOTFS_DIR IMAGE_DIR DEB_DIR LOG_DIR ARTIFACT_DIR
export TOOLCHAIN_REPO

ROOT := $(CURDIR)
EDITION := $(filter-out $@,$(MAKECMDGOALS))

.PHONY: help deps check toolchain vendor test clean

# ── help ────────────────────────────────────────────────────────────────────
help:
	@echo "Arcline OS build targets"
	@echo "───────────────────────"
	@echo "  make deps                 Install host build dependencies"
	@echo "  make check                Validate manifests + shell syntax"
	@echo "  make rootfs-<edition>     Build rootfs  (server|workstation|cloud)"
	@echo "  make iso-<edition>        Build ISO     (server|workstation|cloud)"
	@echo "  make image-<edition>      Build disk image (qcow2; cloud edition)"
	@echo "  make iso                   Build ISO for every edition"
	@echo "  make iso-<edition>-minimal Build ISO WITHOUT the Arcline toolchain"
	@echo "  make toolchain            Build the 11 Go tools into .deb"
	@echo "  make vendor               Build grafana/loki/promtail .debs"
	@echo "  make test                 Run smoke tests against a built image"
	@echo "  make clean                Remove build/ artifacts"
	@echo
	@echo "Configuration (see versions.mk):"
	@echo "  VERSION=$(VERSION)  DEBIAN_SUITE=$(DEBIAN_SUITE)  ARCH=$(ARCH)"
	@echo "  BOOT=$(BOOT)  ARCLINE_TOOLCHAIN=auto|skip|require  (bootloader / toolchain in image builds)"

# ── host deps ────────────────────────────────────────────────────────────────
deps:
	@./scripts/check-host-deps.sh

# ── validation (fast, no network, safe on any machine) ──────────────────────
check:
	@./scripts/validate.sh

# ── edition builds ──────────────────────────────────────────────────────────
# ARCLINE_TOOLCHAIN controls whether the 11 Go tools are included:
#   auto (default)  install if build/debs/ has .deb files, else skip
#   skip            never install (minimal image)
#   require         fail if no .deb files are available
# The *-minimal targets are a shortcut for ARCLINE_TOOLCHAIN=skip.
define ISO_RULE
.PHONY: rootfs-$(1) iso-$(1) image-$(1) clean-$(1) rootfs-$(1)-minimal iso-$(1)-minimal image-$(1)-minimal
rootfs-$(1):
	@./scripts/build-edition.sh $(1) rootfs

iso-$(1):
	@./scripts/build-edition.sh $(1) iso

image-$(1):
	@./scripts/build-edition.sh $(1) image

rootfs-$(1)-minimal:
	@ARCLINE_TOOLCHAIN=skip ./scripts/build-edition.sh $(1) rootfs

iso-$(1)-minimal:
	@ARCLINE_TOOLCHAIN=skip ./scripts/build-edition.sh $(1) iso

image-$(1)-minimal:
	@ARCLINE_TOOLCHAIN=skip ./scripts/build-edition.sh $(1) image

clean-$(1):
	@rm -rf "$(ROOTFS_DIR)/$(1)" "$(IMAGE_DIR)/$(1)"
endef
$(foreach e,$(EDITIONS),$(eval $(call ISO_RULE,$(e))))

iso: $(foreach e,$(EDITIONS),iso-$(e))

# ── toolchain ───────────────────────────────────────────────────────────────
toolchain:
	@./toolchain/build-tools.sh

# ── vendor packaging (grafana / loki / promtail) ────────────────────────────
vendor:
	@./toolchain/build-vendor.sh

# ── tests ───────────────────────────────────────────────────────────────────
test:
	@./tests/run-tests.sh

# ── clean ───────────────────────────────────────────────────────────────────
clean:
	@rm -rf "$(BUILD_DIR)"
	@echo "removed $(BUILD_DIR)/"
