HIPERISO_VERSION := 1.0.0
BUILD_DIR  := build
STAGING    := $(CURDIR)/$(BUILD_DIR)/staging
PAYLOAD    := $(CURDIR)/$(BUILD_DIR)/payload
SCRIPTS    := scripts

.PHONY: all grub2 kernel qemu ovmf busybox initramfs hiperiso-log gui package dist clean distclean help

help:
	@echo "hiperiso build system"
	@echo ""
	@echo "Targets:"
	@echo "  all         Build everything (staging artifacts)"
	@echo "  grub2       Build GRUB2 2.04 with hiperiso module"
	@echo "  kernel      Build host kernel (KVM built-in)"
	@echo "  qemu        Build stripped QEMU system-x86_64"
	@echo "  ovmf        Acquire OVMF UEFI firmware"
	@echo "  busybox     Build static busybox for initramfs"
	@echo "  initramfs   Pack initramfs.cpio.gz"
	@echo "  hiperiso-log  Build log analysis tool"
	@echo "  gui         Build all GUI/web tools (Qt5, GTK3, WebUI, Plugson)"
	@echo "  package     Assemble release payload + ESP disk image (depends on gui)"
	@echo "  dist        Alias for 'package'"
	@echo "  download    Download source tarballs"
	@echo "  clean       Remove build/"
	@echo "  distclean   Remove build/ + payload/"

all: kernel qemu ovmf grub2 busybox initramfs hiperiso-log
	@echo "=== All staging artifacts built ==="
	@echo "Run 'make package' to build GUI tools and assemble release payload"

dist: package

download:
	@echo "=== Downloading sources ==="
	sh $(SCRIPTS)/download_sources.sh $(BUILD_DIR)

grub2:
	@echo "=== Building GRUB2 2.04 with hiperiso module ==="
	sh $(SCRIPTS)/build_grub2_204.sh

gui:
	@echo "=== Building all GUI/web tools ==="
	sh $(SCRIPTS)/build_gui_all.sh

kernel:
	@echo "=== Building host kernel ==="
	cd $(BUILD_DIR)/linux && \
	  cp ../../host/kernel/hiperiso_defconfig .config && \
	  make olddefconfig && \
	  make -j$$(nproc) bzImage && \
	  mkdir -p $(STAGING)/efi && \
	  cp arch/x86/boot/bzImage $(STAGING)/efi/vmlinuz

qemu:
	@echo "=== Building QEMU ==="
	cd $(BUILD_DIR)/qemu && \
	  ../../scripts/configure_qemu.sh && \
	  make -j$$(nproc) qemu-system-x86_64 && \
	  cp build/qemu-system-x86_64 $(STAGING)/qemu-system-x86_64 && \
	  strip $(STAGING)/qemu-system-x86_64

ovmf:
	@echo "=== Acquiring OVMF firmware ==="
	@if [ -f /usr/share/edk2/x64/OVMF.4m.fd ]; then \
	  cp /usr/share/edk2/x64/OVMF.4m.fd $(STAGING)/OVMF.fd; \
	elif [ -f /usr/share/OVMF/OVMF.fd ]; then \
	  cp /usr/share/OVMF/OVMF.fd $(STAGING)/OVMF.fd; \
	else \
	  echo "ERROR: No OVMF available. Install edk2-ovmf."; \
	  exit 1; \
	fi

busybox:
	@echo "=== Building busybox (static) ==="
	cd $(BUILD_DIR)/busybox && \
	  make defconfig && \
	  sed -i 's/# CONFIG_STATIC is not set/CONFIG_STATIC=y/' .config && \
	  yes "" | make oldconfig >/dev/null 2>&1 && \
	  make -j$$(nproc) && \
	  cp busybox $(STAGING)/busybox

initramfs:
	@echo "=== Building initramfs ==="
	sh scripts/build_initramfs.sh $(STAGING)

hiperiso-log:
	@echo "=== Building hiperiso-log tool ==="
	cd logging/hiperiso-log && make && \
	  cp hiperiso-log $(STAGING)/hiperiso-log

package: gui
	@echo "=== Assembling release payload + ESP disk image ==="
	sh $(SCRIPTS)/package_release.sh

clean:
	rm -rf $(BUILD_DIR)

distclean: clean
	rm -rf $(PAYLOAD)
