# Void Installer — XPS 17 (xps9700)
#
# Targets:
#   make iso         build the auto-installing ISO (uses docker)
#   make live        build the full Cinnamon live desktop ISO
#   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/

PROJECT_DIR := $(CURDIR)
OUT         := $(PROJECT_DIR)/out
SECRETS     := $(PROJECT_DIR)/secrets.env

.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-iso.sh

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

# 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 $(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)
	$(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-iso.sh \
	    $(PROJECT_DIR)/iso/_inner-build.sh \
	    $(PROJECT_DIR)/iso/_inner-build-live.sh \
	    $(PROJECT_DIR)/iso/build-live-iso.sh \
	    $(PROJECT_DIR)/tests/*.sh \
	    $(PROJECT_DIR)/tests/lib/*.sh

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

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