# Portal panel firmware — build and USB deploy
#
# Usage:
#   make              # build only
#   make deploy       # build + flash over USB (picotool)
#   make clean
#
# Options (passed to cmake on configure/reconfigure):
#   make deploy PANEL_ID=0
#   make deploy NO_DHCP=1
#   make deploy WS2812_PIN=26 WS2812_PIN1=27   # override defaults GP28 / GP27
#
# Environment:
#   PICO_SDK_PATH     default: ~/pico/pico-sdk
#   SERIAL_PORT       default: first /dev/ttyACM* or /dev/ttyACM0

PICO_SDK_PATH ?= $(HOME)/pico/pico-sdk
BUILD_DIR     := build
UF2           := $(BUILD_DIR)/panel.uf2
CMAKE_STAMP   := $(BUILD_DIR)/.cmake_stamp
JOBS          ?= $(shell nproc 2>/dev/null || echo 4)
SERIAL_PORT   ?= $(shell ls /dev/ttyACM* 2>/dev/null | head -1)
ifeq ($(SERIAL_PORT),)
SERIAL_PORT   := /dev/ttyACM0
endif
SERIAL_BAUD   ?= 115200

CMAKE_ARGS :=
ifdef PANEL_ID
CMAKE_ARGS += -DPANEL_ID_BUILD=$(PANEL_ID)
endif
ifdef WS2812_PIN
CMAKE_ARGS += -DWS2812_PIN_BUILD=$(WS2812_PIN)
endif
ifdef WS2812_PIN1
CMAKE_ARGS += -DWS2812_PIN1_BUILD=$(WS2812_PIN1)
endif
ifeq ($(NO_DHCP),0)
CMAKE_ARGS += -DPORTAL_USE_DHCP=ON
else
CMAKE_ARGS += -DPORTAL_USE_DHCP=OFF
endif

CMAKE_STAMP_BODY := $(strip $(CMAKE_ARGS))

SOURCES := $(wildcard *.c *.cpp *.h) WS2812.pio CMakeLists.txt pico_sdk_import.cmake

.PHONY: all build configure reconfigure deploy flash upload reset monitor clean help

all: build

help:
	@echo "Targets:"
	@echo "  make build      Build panel.uf2"
	@echo "  make deploy     Build and flash over USB"
	@echo "  make flash      Alias for deploy"
	@echo "  make reset      USB reboot Pico (picotool) — run before monitor for boot log"
	@echo "  make monitor    USB serial console (picocom, Ctrl+A Ctrl+X to quit)"
	@echo "  make clean      Remove build directory"
	@echo "  make reconfigure  Re-run cmake (e.g. after changing PANEL_ID)"
	@echo ""
	@echo "Typical:  make reset && make monitor"

build: configure
	@echo "==> Building panel firmware"
	PICO_SDK_PATH="$(PICO_SDK_PATH)" cmake --build $(BUILD_DIR) -j$(JOBS)
	@test -f "$(UF2)" || (echo "Error: build did not produce panel.uf2" && exit 1)
	@echo "==> Build OK: $(UF2)"

configure:
	@test -d "$(PICO_SDK_PATH)" || (echo "Error: pico-sdk not found at $(PICO_SDK_PATH) (set PICO_SDK_PATH)" && exit 1)
	@command -v cmake >/dev/null || (echo "Error: cmake is required" && exit 1)
	@mkdir -p $(BUILD_DIR)
	@STAMP='$(CMAKE_STAMP_BODY)'; \
	if [ ! -f "$(BUILD_DIR)/CMakeCache.txt" ] || [ ! -f "$(CMAKE_STAMP)" ] || [ "$$(cat $(CMAKE_STAMP))" != "$$STAMP" ]; then \
		echo "==> Configuring cmake in $(BUILD_DIR) $(CMAKE_ARGS)"; \
		PICO_SDK_PATH="$(PICO_SDK_PATH)" cmake -S . -B $(BUILD_DIR) $(CMAKE_ARGS); \
		echo "$$STAMP" > "$(CMAKE_STAMP)"; \
	fi

reconfigure:
	@test -d "$(PICO_SDK_PATH)" || (echo "Error: pico-sdk not found at $(PICO_SDK_PATH) (set PICO_SDK_PATH)" && exit 1)
	@mkdir -p $(BUILD_DIR)
	@echo "==> Reconfiguring cmake in $(BUILD_DIR) $(CMAKE_ARGS)"
	PICO_SDK_PATH="$(PICO_SDK_PATH)" cmake -S . -B $(BUILD_DIR) $(CMAKE_ARGS)
	@echo '$(CMAKE_STAMP_BODY)' > $(CMAKE_STAMP)

deploy: build
	@command -v picotool >/dev/null || (echo "Error: picotool not found (https://github.com/raspberrypi/picotool)" && exit 1)
	@picotool version 2>&1 | grep -q 'without USB support' && \
		(echo "Error: picotool was built without USB support" && exit 1) || true
	@echo "==> Uploading $(UF2) over USB"
	@picotool load -x -f "$(UF2)"

flash: deploy
upload: deploy

reset:
	@command -v picotool >/dev/null || (echo "Error: picotool not found (https://github.com/raspberrypi/picotool)" && exit 1)
	@echo "==> Resetting Pico over USB"
	@picotool reboot -f
	@echo "==> Waiting for USB serial..."
	@i=0; \
	while [ $$i -lt 40 ]; do \
		if ls /dev/ttyACM* >/dev/null 2>&1; then \
			echo "==> Ready: $$(ls /dev/ttyACM* | head -1)"; \
			exit 0; \
		fi; \
		i=$$((i + 1)); \
		sleep 0.25; \
	done; \
	echo "Warning: no /dev/ttyACM* yet — try make monitor anyway"; \
	exit 0

monitor:
	@command -v picocom >/dev/null || (echo "Error: picocom not found (sudo apt install picocom)" && exit 1)
	@SERIAL="$(SERIAL_PORT)"; \
	if [ ! -e "$$SERIAL" ]; then \
		SERIAL=$$(ls /dev/ttyACM* 2>/dev/null | head -1); \
	fi; \
	test -n "$$SERIAL" && test -e "$$SERIAL" || (echo "Error: no /dev/ttyACM* (plug in Pico USB, or set SERIAL_PORT=...)" && exit 1); \
	echo "==> Serial monitor on $$SERIAL ($(SERIAL_BAUD) baud, Ctrl+A Ctrl+X to quit)"; \
	picocom --baud $(SERIAL_BAUD) --flow n --echo "$$SERIAL"

clean:
	rm -rf $(BUILD_DIR)
