# Hermes Portable Rescue — Build System
# =========================================
#
# Targets:
#   all          — Full build: ISO + disk image (default)
#   iso          — Build rescue ISO only
#   image        — Build full disk image (requires ISO first)
#   check        — Validate all upstream artifacts are present
#   clean        — Remove all build artifacts
#   distclean    — Clean + remove downloaded tool binaries
#   write        — Write image to USB device (run: make write DEV=/dev/sdX)
#   ventoy       — Install Ventoy to USB device (run: make ventoy DEV=/dev/sdX)
#   help         — Show this message
#
# Quick Start:
#   1. make check        — See what's missing
#   2. make all          — Build everything that's available
#   3. make write DEV=/dev/sdb  — Write to USB (CAUTION!)
#

BUILD_DIR   ?= build
HERMES_SRC  ?= src/hermes
DIAG_SRC    ?= src/diagnostics
TOOLS_SRC   ?= src/tools
OUTPUT_DIR  ?= $(BUILD_DIR)

IMAGE_SCRIPT := $(BUILD_DIR)/usb-image.sh
OUTPUT_IMAGE := $(OUTPUT_DIR)/hermes-rescue.img
OUTPUT_ISO   := $(OUTPUT_DIR)/hermes-rescue.iso

DEVICE       ?= /dev/sdb

# Colors
BOLD := \033[1m
GREEN := \033[0;32m
YELLOW := \033[1;33m
RED := \033[0;31m
NC := \033[0m

.PHONY: all iso image check clean distclean write ventoy help dirs

all: check iso image
	@echo ""
	@printf "$(GREEN)✓ Build complete!$(NC)\n"
	@echo "  ISO:  $(OUTPUT_ISO)"
	@echo "  IMG:  $(OUTPUT_IMAGE)"

iso: dirs
	@printf "$(BOLD)Building rescue ISO...$(NC)\n"
	@bash $(IMAGE_SCRIPT) --iso-only

image: dirs iso
	@printf "$(BOLD)Building disk image...$(NC)\n"
	@bash $(IMAGE_SCRIPT)

dirs:
	@mkdir -p $(BUILD_DIR)

check:
	@printf "$(BOLD)Checking upstream artifacts...$(NC)\n"
	@bash $(IMAGE_SCRIPT) --skip-validate 2>&1 | head -30 || true
	@echo ""
	@printf "Expected artifact paths:\n"
	@for f in \
		"$(HERMES_SRC)/agent/hermes" \
		"$(HERMES_SRC)/config.yaml" \
		"$(HERMES_SRC)/autorun.sh" \
		"$(DIAG_SRC)/hardware.py" \
		"$(DIAG_SRC)/bsod.py" \
		"$(DIAG_SRC)/drivers.py" \
		"$(DIAG_SRC)/backup.py" \
		"$(DIAG_SRC)/stress.py" \
	; do \
		if [ -f "$$f" ] || [ -d "$$f" ]; then \
			printf "  $(GREEN)✓$(NC) %s\n" "$$f"; \
		else \
			printf "  $(YELLOW)✗$(NC) %s\n" "$$f"; \
		fi; \
	done

clean:
	@printf "$(BOLD)Cleaning build artifacts...$(NC)\n"
	@rm -rf $(BUILD_DIR)/usb-staging/
	@rm -f $(OUTPUT_IMAGE) $(OUTPUT_ISO)
	@printf "$(GREEN)✓ Cleaned.$(NC)\n"

distclean: clean
	@printf "$(BOLD)Deep cleaning...$(NC)\n"
	@rm -rf $(TOOLS_SRC)/*
	@printf "$(GREEN)✓ Deep clean complete.$(NC)\n"

write:
	@if [ ! -b "$(DEVICE)" ]; then \
		printf "$(RED)Error: $(DEVICE) is not a block device. Specify DEV=/dev/sdX.$(NC)\n"; \
		exit 1; \
	fi
	@printf "$(YELLOW)⚠️  About to write to $(DEVICE). ALL DATA WILL BE DESTROYED!$(NC)\n"
	@printf "Type the device path to confirm: " && read confirm && [ "$$confirm" = "$(DEVICE)" ]
	@printf "$(BOLD)Writing $(OUTPUT_IMAGE) to $(DEVICE)...$(NC)\n"
	@sudo dd if=$(OUTPUT_IMAGE) of=$(DEVICE) bs=4M status=progress conv=fsync
	@sync
	@printf "$(GREEN)✓ Written to $(DEVICE).$(NC)\n"

ventoy:
	@if [ ! -b "$(DEVICE)" ]; then \
		printf "$(RED)Error: $(DEVICE) is not a block device. Specify DEV=/dev/sdX.$(NC)\n"; \
		exit 1; \
	fi
	@if ! command -v Ventoy2Disk.sh &>/dev/null; then \
		printf "$(RED)Ventoy2Disk.sh not found. Install Ventoy first.$(NC)\n"; \
		exit 1; \
	fi
	@printf "$(YELLOW)⚠️  About to install Ventoy to $(DEVICE). ALL DATA WILL BE DESTROYED!$(NC)\n"
	@printf "Type the device path to confirm: " && read confirm && [ "$$confirm" = "$(DEVICE)" ]
	@sudo Ventoy2Disk.sh -i -g "$(DEVICE)"
	@printf "$(GREEN)✓ Ventoy installed to $(DEVICE). Copy ISOs to the data partition.$(NC)\n"

help:
	@printf "$(BOLD)Hermes Portable Rescue — Build Targets$(NC)\n"
	@echo ""
	@sed -n 's/^# //p; s/^#//p' $(MAKEFILE_LIST) | head -20
	@echo ""
	@echo "Usage: make <target> [VAR=value]"
	@echo ""
	@echo "Variables:"
	@echo "  DEVICE=/dev/sdX     — Target USB device (for write/ventoy targets)"
	@echo "  BUILD_DIR=build     — Output directory"
	@echo "  HERMES_SRC=src/hermes — Hermes runtime source"
	@echo "  DIAG_SRC=src/diagnostics — Diagnostic scripts source"
	@echo "  TOOLS_SRC=src/tools — Diagnostic tools source"
	@echo ""
	@echo "Examples:"
	@echo "  make                 — Full build (check → iso → image)"
	@echo "  make iso             — Build ISO only"
	@echo "  make write DEV=/dev/sdb  — Write image to USB"
