.PHONY: test lint check build clean help version version-set version-sync

# ── Paths ──────────────────────────────────────────────────────────────
REPO_ROOT      := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
PROFILE_DIR    := $(REPO_ROOT)/packaging/archiso
SCRIPTS_DIR    := $(REPO_ROOT)/packaging/scripts
TESTS_DIR      := $(REPO_ROOT)/tests
VERSION_FILE   := $(REPO_ROOT)/VERSION

# Shell scripts to lint with shellcheck
SHELL_SCRIPTS  := \
	$(PROFILE_DIR)/profiledef.sh \
	$(PROFILE_DIR)/build.sh \
	$(PROFILE_DIR)/airootfs/usr/local/bin/samios \
	$(PROFILE_DIR)/airootfs/usr/local/bin/choose-mirror \
	$(PROFILE_DIR)/airootfs/root/.automated_script.sh \
	$(SCRIPTS_DIR)/samios-desktop-setup.sh \
	$(SCRIPTS_DIR)/samios-installer.sh \
	$(TESTS_DIR)/test_helper.sh \
	$(TESTS_DIR)/test_profiledef.sh \
	$(TESTS_DIR)/test_packages.sh \
	$(TESTS_DIR)/test_pacman_conf.sh \
	$(TESTS_DIR)/test_samios_cli.sh \
	$(TESTS_DIR)/test_font_policy.sh \
	$(TESTS_DIR)/test_version.sh \
	$(TESTS_DIR)/test_installer.sh \
	$(TESTS_DIR)/test_voice_bridge.sh \
	$(TESTS_DIR)/run_tests.sh

SHELLCHECK     ?= shellcheck
SHELLCHECK_OPTS := --severity=warning
# SC2034 (unused var) is expected in profile definitions and CLI stubs
SHELLCHECK_EXCLUDE := --exclude=SC2034

# ── Targets ────────────────────────────────────────────────────────────

help: ## Show this help
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
		awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-12s\033[0m %s\n", $$1, $$2}'

test: ## Run the test suite
	@echo "Running SamiOS test suite..."
	@bash $(TESTS_DIR)/run_tests.sh --verbose

lint: ## Run shellcheck on all shell scripts
	@echo "Running shellcheck..."
	@for script in $(SHELL_SCRIPTS); do \
		if [ -f "$$script" ]; then \
			echo "  shellcheck $$script"; \
			$(SHELLCHECK) $(SHELLCHECK_OPTS) $(SHELLCHECK_EXCLUDE) "$$script" || exit 1; \
		fi \
	done
	@echo "shellcheck passed ✓"

check: lint test ## Run lint + test (CI entry point)

build: ## Build the SamiOS ISO (requires archiso + root)
	@echo "Building SamiOS ISO..."
	@cd $(PROFILE_DIR) && sudo ./build.sh

clean: ## Remove build artifacts
	@echo "Cleaning build artifacts..."
	@rm -rf /tmp/samios-build
	@rm -rf $(PROFILE_DIR)/out
	@echo "Clean complete"

# ── Version management ─────────────────────────────────────────────────
# VERSION is the single source of truth. Bumping it is a one-line edit
# to $(VERSION_FILE); version-sync mirrors it into the airootfs overlay.

version: ## Print the current SamiOS version
	@cat $(VERSION_FILE)

version-sync: ## Mirror VERSION into the airootfs overlay
	@if [ ! -f "$(VERSION_FILE)" ]; then \
		echo "ERROR: $(VERSION_FILE) does not exist"; exit 1; \
	fi
	@install -d -m 755 $(PROFILE_DIR)/airootfs/etc
	@install -m 644 $(VERSION_FILE) $(PROFILE_DIR)/airootfs/etc/samios-version
	@echo "Synced $(VERSION_FILE) → $(PROFILE_DIR)/airootfs/etc/samios-version"
	@echo "  Content: $$(cat $(VERSION_FILE))"

version-set: ## Set VERSION to an explicit value (usage: make version-set NEW=X.Y.Z)
	@if [ -z "$(NEW)" ]; then \
		echo "Usage: make version-set NEW=X.Y.Z"; exit 1; \
	fi
	@if ! echo "$(NEW)" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*)?$$'; then \
		echo "ERROR: NEW must be semver-ish (X.Y.Z or X.Y.Z-suffix; suffix is dot-separated alphanumerics)"; exit 1; \
	fi
	@echo "$(NEW)" > $(VERSION_FILE)
	@$(MAKE) --no-print-directory version-sync
	@echo ""
	@echo "Set to $(NEW). Run 'make check' to verify, then commit."
