PYTHON ?= python3
PIO ?= pio
UPLOAD_PORT ?=
VENV := .venv
DEPS_STAMP := $(VENV)/.requirements-installed

.DEFAULT_GOAL := all
.PHONY: all deps test convert build flash

all: test convert build

deps: $(DEPS_STAMP)

$(DEPS_STAMP): book-processing/requirements.txt
	$(PYTHON) -m venv $(VENV)
	$(VENV)/bin/python -m pip install -r $<
	touch $@

test: deps
	. $(VENV)/bin/activate && cd book-processing && python -m unittest discover -v

convert: deps
	@output_dir=$$(mktemp -d /tmp/tds3p-timing-sample.XXXXXX); \
		echo "Converting sample EPUB into $$output_dir"; \
		. $(VENV)/bin/activate && cd book-processing && python epub_to_chapters.py examples/rsvp-timing-sample.epub "$$output_dir"

build:
	cd firmware && $(PIO) run

flash:
	cd firmware && $(PIO) run -t upload $(if $(UPLOAD_PORT),--upload-port $(UPLOAD_PORT))
