# 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

## wheels-windows-mcp — Download Windows-MCP wheels for offline install
wheels-windows-mcp:
	@printf "$(BOLD)Downloading Windows-MCP wheels...$(NC)\n"
	@mkdir -p $(OUTPUT_DIR)/bin/wheels-windows-mcp
	@pip3 download --platform win_amd64 --python-version 3.13 --only-binary=:all: \
		--dest $(OUTPUT_DIR)/bin/wheels-windows-mcp --quiet windows-mcp 2>&1
	@printf "$(GREEN)✓$(NC) Downloaded $$(ls $(OUTPUT_DIR)/bin/wheels-windows-mcp/ | wc -l) wheels\n"
	@du -sh $(OUTPUT_DIR)/bin/wheels-windows-mcp/

## wheels-hermes — Download Hermes Agent wheels for offline Windows install
wheels-hermes:
	@printf "$(BOLD)Downloading Hermes Agent wheels...$(NC)\n"
	@mkdir -p $(OUTPUT_DIR)/bin/wheels
	@pip3 download --platform win_amd64 --python-version 3.11 --only-binary=:all: \
		--dest $(OUTPUT_DIR)/bin/wheels --quiet hermes-agent zeroconf ifaddr 2>&1
	@printf "$(GREEN)✓$(NC) Downloaded $$(ls $(OUTPUT_DIR)/bin/wheels/ | wc -l) wheels\n"
	@du -sh $(OUTPUT_DIR)/bin/wheels/

## get-pip — Download get-pip.py bootstrapper
get-pip:
	@printf "$(BOLD)Downloading get-pip.py...$(NC)\n"
	@curl -sL https://bootstrap.pypa.io/get-pip.py -o $(OUTPUT_DIR)/bin/get-pip.py
	@printf "$(GREEN)✓$(NC) Downloaded get-pip.py ($$(wc -c < $(OUTPUT_DIR)/bin/get-pip.py) bytes)\n"

## python-portable — Download portable Python 3.13 for Windows
python-portable:
	@printf "$(BOLD)Downloading portable Python 3.13 for Windows...$(NC)\n"
	@mkdir -p /tmp/python-portable
	@cd /tmp/python-portable && curl -sL "https://www.python.org/ftp/python/3.13.4/python-3.13.4-embed-amd64.zip" -o python-embed.zip
	@mkdir -p $(OUTPUT_DIR)/python
	@cd /tmp/python-portable && unzip -q -o python-embed.zip -d $(OUTPUT_DIR)/python/
	@rm -rf /tmp/python-portable
	@# Remove the _pth file so pip can be installed
	@rm -f $(OUTPUT_DIR)/python/python*._pth
	@printf "$(GREEN)✓$(NC) Portable Python 3.13 extracted to $(OUTPUT_DIR)/python/\n"
	@du -sh $(OUTPUT_DIR)/python/

## deps — Download all dependencies for offline USB build
deps: get-pip wheels-hermes wheels-windows-mcp
	@printf "$(GREEN)✓$(NC) All dependencies downloaded.\n"

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"
