# Thin wrapper over scripts/ — all real logic lives in scripts/*.sh, with
# shared config and helpers in scripts/lib.sh.  `make help` lists targets.
#
# NOTE: $(VAR) in this file is MAKE syntax, not shell substitution.

SYSTEM_IMG ?= /tmp/system.img

.PHONY: all build run run-headless debug system system-fresh inspect \
	test test-disk test-fault test-gfx test-mouse test-msg test-usb fmt clean help

# ---- build ----------------------------------------------------------------

all: build

build:
	./scripts/build.sh

# ---- boot -----------------------------------------------------------------

# Build + stage the image + boot QEMU (window + serial on stdio).
run:
	./scripts/run.sh

# Serial console only, no window.
run-headless:
	./scripts/run.sh --headless

# GDB stub on :1234, CPU paused (attach: gdb -x scripts/debug-kernel.py).
debug:
	./scripts/run.sh --debug

# ---- system image ---------------------------------------------------------

# Stage the image if it is stale (runtime files survive).
system:
	./scripts/mksystem.sh

# Re-create the image from scratch (wipes runtime data).
system-fresh:
	./scripts/mksystem.sh --force

# List the image contents (needs mtools: sudo apt install mtools).
inspect:
	@mdir -i "$(SYSTEM_IMG)" ::

# ---- tests ----------------------------------------------------------------

test: test-disk test-gfx test-mouse test-msg test-fault test-usb

test-disk:
	./scripts/tests/test-disk.sh

test-fault:
	./scripts/tests/test-fault.sh

test-gfx:
	./scripts/tests/test-gfx.sh

test-mouse:
	./scripts/tests/test-mouse.sh

test-msg:
	./scripts/tests/test-msg.sh

test-usb:
	./scripts/tests/test-usb.sh

# ---- utilities ------------------------------------------------------------

fmt:
	cargo fmt

clean:
	cargo clean

help:
	@echo "raisernics — make targets"
	@echo ""
	@echo "  make                build kernel + /bin binaries (release)"
	@echo "  make run            build + stage image + boot QEMU (window)"
	@echo "  make run-headless   same, serial console only"
	@echo "  make debug          same, GDB stub on :1234 (CPU paused)"
	@echo "  make system         stage the system image if stale"
	@echo "  make system-fresh   re-create the system image (wipes data)"
	@echo "  make inspect        list image contents (mtools)"
	@echo "  make test           run every automated test"
	@echo "  make test-disk      disk round trip (build -> boot -> fsck)"
	@echo "  make test-gfx       graphics self-test"
	@echo "  make test-mouse     PS/2 mouse + cursor tracking"
	@echo "  make test-msg       scheduler message queues"
	@echo "  make test-fault     fault dump (screen + serial)"
	@echo "  make test-usb       USB HID enumeration (EHCI + xHCI)"
	@echo "  make fmt            format Rust source"
	@echo "  make clean          remove build artifacts"
	@echo ""
	@echo "Two-command flow: ./scripts/build.sh && ./scripts/run.sh"
	@echo "Scripts: build.sh  run.sh  mksystem.sh  lib.sh  tests/"
