# Void Installer — XPS 17 (xps9700)
#
# Profiles: stable-cinnamon | stable-niri | mainline-cinnamon | mainline-niri
#   kernel: stable = Void's linux (k6),  mainline = linux-mainline (k7)
#   DE:     cinnamon = Cinnamon/X11,     niri = niri+noctalia-shell/Wayland
#
# Targets:
#   make iso         build the auto-installing ISO   (PROFILE=stable-cinnamon)
#   make live        build the full desktop live ISO  (PROFILE=stable-cinnamon)
#   make test-disk   create a fresh QEMU test disk that mimics XPS 17 layout
#   make test        full automated QEMU smoke test
#   make test-iso    rebuild only the TEST ISO variant
#   make qemu        launch QEMU interactively with the latest ISO
#   make shellcheck  lint all installer/build shell scripts
#   make clean       remove build/, out/  (cache stays)
#   make distclean   also remove cache/
#
# Examples:
#   make live PROFILE=mainline-niri
#   make iso  PROFILE=stable-cinnamon
#   make test PROFILE=mainline-cinnamon

PROJECT_DIR := $(CURDIR)
OUT         := $(PROJECT_DIR)/out
SECRETS     := $(PROJECT_DIR)/secrets.env
PROFILE     ?= stable-cinnamon

.PHONY: all iso live live-qemu test test-disk test-iso qemu shellcheck clean distclean check-secrets check-docker

all: iso

check-secrets:
	@test -r $(SECRETS) || { echo "missing $(SECRETS) — copy from template"; exit 1; }

check-docker:
	@command -v docker >/dev/null || { echo "ERROR: docker not installed"; exit 1; }
	@docker info >/dev/null 2>&1 || { echo "ERROR: docker daemon unreachable (in 'docker' group? systemctl start docker?)"; exit 1; }

iso: check-secrets check-docker
	$(PROJECT_DIR)/iso/build.sh --profile $(PROFILE) --type installer

live: check-secrets check-docker
	$(PROJECT_DIR)/iso/build.sh --profile $(PROFILE) --type live

# Launch the live ISO in QEMU with 12 GB RAM so nix packages fit in the tmpfs.
# The live session is a pure-RAM tmpfs overlay; Cinnamon + nix need ~7-8 GB total.
live-qemu:
	$(PROJECT_DIR)/tests/launch-live-qemu.sh

test-iso: check-secrets check-docker
	REBUILD_ISO=1 PROFILE=$(PROFILE) $(PROJECT_DIR)/tests/run-qemu-test.sh

test-disk:
	$(PROJECT_DIR)/tests/make-test-disk.sh $(OUT)/test-disk.img

test: check-secrets check-docker
	@mkdir -p $(OUT)
	PROFILE=$(PROFILE) $(PROJECT_DIR)/tests/run-qemu-test.sh

qemu:
	$(PROJECT_DIR)/tests/interactive-qemu.sh

shellcheck:
	@command -v shellcheck >/dev/null || { echo "shellcheck not installed"; exit 1; }
	shellcheck -x \
	    $(PROJECT_DIR)/installer/install.sh \
	    $(PROJECT_DIR)/installer/lib/*.sh \
	    $(PROJECT_DIR)/iso/build.sh \
	    $(PROJECT_DIR)/iso/_inner-build-unified.sh \
	    $(PROJECT_DIR)/tests/*.sh \
	    $(PROJECT_DIR)/tests/lib/*.sh

clean:
	rm -rf $(PROJECT_DIR)/build $(OUT)

distclean: clean
	rm -rf $(PROJECT_DIR)/cache
