CXX ?= g++
CXXFLAGS ?= -std=c++17 -O0 -g -Wall -Wextra -Wno-unused-parameter
INCLUDES = -I../components/grub_boot_switch

SRCS = test_disk.cpp \
       ../components/grub_boot_switch/fat12.cpp \
       ../components/grub_boot_switch/msc_disk.cpp

BIN = test_disk
IMG = disk.img

.PHONY: all build run verify clean

all: verify

build: $(BIN)

$(BIN): $(SRCS)
	$(CXX) $(CXXFLAGS) $(INCLUDES) -o $@ $(SRCS)

# Generate a disk image (target=0 by default; pass T=N to override)
$(IMG): $(BIN)
	./$(BIN) $(IMG) $(or $(T),0)

# Run the verifier suite. Requires mtools and `file`.
verify: $(IMG)
	@echo "=== file(1) on disk image ==="
	@file $(IMG)
	@echo
	@echo "=== mdir listing ==="
	@MTOOLS_SKIP_CHECK=1 mdir -i $(IMG) ::
	@echo
	@echo "=== switch_position contents ==="
	@MTOOLS_SKIP_CHECK=1 mtype -i $(IMG) ::switch_position
	@echo
	@echo "=== switch_position_grub.cfg contents ==="
	@MTOOLS_SKIP_CHECK=1 mtype -i $(IMG) ::switch_position_grub.cfg
	@echo
	@echo "=== volume serial ==="
	@MTOOLS_SKIP_CHECK=1 minfo -i $(IMG) :: | grep -i serial

# Cycle through several target values to prove the file content tracks state
sweep: $(BIN)
	@for t in 0 1 2 3 9 ; do \
	    ./$(BIN) sweep_$$t.img $$t >/dev/null ; \
	    echo "target=$$t:" ; \
	    MTOOLS_SKIP_CHECK=1 mtype -i sweep_$$t.img ::switch_position_grub.cfg ; \
	done ; \
	rm -f sweep_*.img

clean:
	rm -f $(BIN) $(IMG) sweep_*.img
