# Amirine Cosplay Lights — Build & Upload
# SPDX-License-Identifier: BSD-2-Clause
#
# Usage:
#   make shows             — convert all .txt shows and regenerate shows.h
#   make build             — compile the sketch
#   make upload            — convert shows, compile, and upload (auto-detects port)
#   make upload PORT=/dev/ttyUSB1  — upload to a specific port
#   make port              — list connected Arduino-compatible devices
#   make clean             — remove build artifacts

SKETCH    := arduino/cosplay_lights
FQBN      := arduino:avr:uno
BUILD_DIR := build

# Auto-detect the first connected Arduino Uno port.
# Override on the command line: make upload PORT=/dev/ttyUSB1
PORT ?= $(shell arduino-cli board list 2>/dev/null | awk '/arduino:avr:uno/{print $$1}' | head -1)

# ---- Targets -----------------------------------------------------------

.PHONY: all shows simulate build upload port clean

all: build

## Convert all .txt show files and regenerate shows.h.
shows:
	python converter/convert_all.py

## Preview shows in the terminal without hardware.
simulate:
	python converter/simulate.py

## Compile the sketch.
build:
	arduino-cli compile --fqbn $(FQBN) --build-path $(BUILD_DIR) $(SKETCH)

## Convert shows, compile, and upload to the Arduino.
upload: shows build
	@if [ -z "$(PORT)" ]; then \
		echo ""; \
		echo "  No Arduino found. Plug in the USB cable and try again,"; \
		echo "  or specify the port manually: make upload PORT=/dev/ttyUSB0"; \
		echo ""; \
		exit 1; \
	fi
	arduino-cli upload --fqbn $(FQBN) --port $(PORT) --input-dir $(BUILD_DIR) $(SKETCH)
	@echo ""
	@echo "  Uploaded to $(PORT). The show starts immediately."

## List connected Arduino-compatible devices.
port:
	@arduino-cli board list

## Remove build artifacts.
clean:
	rm -rf $(BUILD_DIR)
