# shellcheck shell=sh disable=SC2046,SC2091,SC2086,SC2155
# NOTE: $(VAR) in this file is MAKE syntax, not shell substitution.
export TARGET=x86_64-unknown-uefi
export PROFILE=debug
export OUT_DIR=target/$(TARGET)/$(PROFILE)
export ESP_DIR=esp
export KERNEL=$(OUT_DIR)/rsdos.efi
export BZIMAGE=$(ESP_DIR)/bzImage
export INITRD=$(ESP_DIR)/initramfs.cpio.gz
export BOOTLOADER=rsboot/rsboot.efi
export OVMF=/usr/share/edk2/x64/OVMF.4m.fd
export BOOT_EFI=$(ESP_DIR)/EFI/BOOT/BOOTX64.EFI

.PHONY: all run run-gui clean image debug release fmt bootloader boot-image

# ---- Build ---------------------------------------------------------------

all: $(BZIMAGE) $(INITRD)

release: PROFILE=release
release: export PROFILE=release
release: export OUT_DIR=target/$(TARGET)/release
release: CARGO_FLAGS=--release
release: all

# shellcheck disable=SC2034
CARGO_FLAGS=

$(OUT_DIR)/rsdos.efi: $(shell find rsnix shell rsdos -name '*.rs') Cargo.toml rsnix/Cargo.toml shell/Cargo.toml rsdos/Cargo.toml
	cargo build --package rsdos --target $(TARGET) $(CARGO_FLAGS)

$(BZIMAGE): $(KERNEL) | $(ESP_DIR)
	cp "$<" "$@"

$(ESP_DIR):
	mkdir -p $(ESP_DIR)

$(INITRD): | $(ESP_DIR)
	printf '' | gzip > "$@"

# ---- QEMU runners --------------------------------------------------------

run: all
	qemu-system-x86_64 \
	  -bios $(OVMF) \
	  -drive file=fat:rw:$(ESP_DIR),format=raw \
	  -serial stdio \
	  -m 256M \
	  -machine q35 \
	  -cpu qemu64 \
	  -no-reboot \
	  -display none

run-gui: all
	qemu-system-x86_64 \
	  -bios $(OVMF) \
	  -drive file=fat:rw:$(ESP_DIR),format=raw \
	  -serial stdio \
	  -m 256M \
	  -machine q35 \
	  -cpu qemu64 \
	  -no-reboot \
	  -vga std

# GDB stub on :1234
debug: all
	qemu-system-x86_64 \
	  -bios $(OVMF) \
	  -drive file=fat:rw:$(ESP_DIR),format=raw \
	  -serial stdio \
	  -m 256M \
	  -machine q35 \
	  -cpu qemu64 \
	  -no-reboot \
	  -display none \
	  -s -S

# Boot from a raw disk image (delegates to boot-image.sh which builds the
# full chain if the image doesn't exist yet)
run-image: all
	./scripts/boot-image.sh rs-dos.img

# ---- Full boot chain -----------------------------------------------------

# Build bootloader only (rsboot submodule)
bootloader:
	./scripts/build-bootloader.sh

# Build the full boot chain disk image (kernel + bootloader)
image: all
	./scripts/build-image.sh rs-dos.img

# Boot the full chain disk image (builds it first if needed)
boot-image: all
	./scripts/boot-image.sh rs-dos.img

# ---- Utilities -----------------------------------------------------------

fmt:
	cargo fmt

clean:
	cargo clean
	rm -rf $(ESP_DIR) rs-dos.img

help:
	@echo "rs-dos — Make targets"
	@echo ""
	@echo "  make                build kernel + ESP (default)"
	@echo "  make release        build with --release (smaller binary)"
	@echo "  make bootloader     build rsboot from source"
	@echo "  make image          build full chain disk image (bootloader + kernel)"
	@echo "  make boot-image     build + boot full chain via QEMU"
	@echo "  make run            QEMU serial console (direct boot, no bootloader)"
	@echo "  make run-gui        QEMU with graphical window (GOP/PS2)"
	@echo "  make debug          QEMU + GDB stub on :1234"
	@echo "  make clean          remove build artifacts"
	@echo "  make fmt            format Rust source"
	@echo ""
	@echo "Scripts:"
	@echo "  ./scripts/boot.sh              quick direct boot (dev loop)"
	@echo "  ./scripts/boot-image.sh        boot full chain (builds if needed)"
	@echo "  ./scripts/build-image.sh       build full chain disk image"
	@echo "  ./scripts/build-bootloader.sh  build rsboot from source"
	@echo "  ./scripts/qemu.sh              flexible QEMU launcher"
	@echo "  ./scripts/debug-kernel.py      GDB plugin (attach after 'make debug')"
