NAME    := demo
OSCAR64 := oscar64/bin/oscar64
SRC     := src/main.c
SRCS    := $(wildcard src/*.c src/*.h)
PRG     := build/$(NAME).prg
EMU     := x64sc

UNAME_S := $(shell uname -s)

all: build

deps:
ifeq ($(UNAME_S),Darwin)
	@command -v brew >/dev/null 2>&1 || { echo "Homebrew is required. Install it: https://brew.sh"; exit 1; }
	brew install vice
else
	@echo "The 'deps' target is only automated on macOS (Darwin). Detected OS: $(UNAME_S)"
	@echo "Install the VICE emulator (x64sc) manually. Oscar64 is vendored and builds itself."
	@exit 1
endif

$(OSCAR64):
	$(MAKE) -C oscar64/make compiler

build: $(PRG)

$(PRG): $(SRCS) $(OSCAR64)
	@mkdir -p build
	$(OSCAR64) -n -O2 -o=$(PRG) $(SRC)

run: $(PRG)
	$(EMU) $(PRG)

clean:
	rm -rf build

rebuild: clean build

rebuildrun: clean build run

.PHONY: all deps build run clean rebuild rebuildrun
