OPENSCAD := openscad
BUILD := build
PARTS := plate ink glow
LINE ?= K
POSE ?= waiting_watch
POSES := waiting_watch waiting

.PHONY: test preview qr stls site-assets kit clean

test:
	mkdir -p $(BUILD)
	$(OPENSCAD) -o $(BUILD)/test.stl tests/test_model.scad

preview:
	mkdir -p $(BUILD)
	$(OPENSCAD) --imgsize=700,700 --projection=o \
	  --camera=70,70,0,0,0,0,160 --viewall \
	  -D 'POSE="$(POSE)"' -D 'LINE="$(LINE)"' \
	  -o $(BUILD)/preview-$(POSE).png piece.scad

qr:
	../.venv/bin/python ../scripts/gen_qr.py

stls:
	mkdir -p $(BUILD)/stl/$(POSE)
	for p in $(PARTS); do \
	  $(OPENSCAD) -D 'PART="'$$p'"' -D 'LINE="$(LINE)"' -D 'POSE="$(POSE)"' \
	    -o $(BUILD)/stl/$(POSE)/$$p.stl piece.scad || exit 1; \
	done
	@ls -la $(BUILD)/stl/$(POSE)

# Publish printable assets into the website (served at /downloads/...).
# One STL set serves every line: glow color is assigned in the slicer.
SITE_DL := ../site/public/downloads

site-assets: stls
	mkdir -p $(SITE_DL)
	cp $(BUILD)/stl/$(POSE)/plate.stl $(SITE_DL)/la-espera-$(POSE)-plate.stl
	cp $(BUILD)/stl/$(POSE)/ink.stl   $(SITE_DL)/la-espera-$(POSE)-ink.stl
	cp $(BUILD)/stl/$(POSE)/glow.stl  $(SITE_DL)/la-espera-$(POSE)-glow.stl
	cd .. && rm -f site/public/downloads/la-espera-source.zip && \
	  zip -q -r site/public/downloads/la-espera-source.zip \
	  model scripts README.md -x "model/build/*" -x "*.DS_Store"
	@ls -la $(SITE_DL)

# One zip with every pose's STLs plus the source: the "print kit" on /print.
kit:
	rm -rf $(BUILD)/kit && mkdir -p $(BUILD)/kit/la-espera-print-kit
	for pose in $(POSES); do \
	  $(MAKE) stls POSE=$$pose > /dev/null && \
	  mkdir -p $(BUILD)/kit/la-espera-print-kit/$$pose && \
	  cp $(BUILD)/stl/$$pose/*.stl $(BUILD)/kit/la-espera-print-kit/$$pose/ || exit 1; \
	done
	mkdir -p $(BUILD)/kit/la-espera-print-kit/source
	cd .. && cp -R model scripts README.md model/$(BUILD)/kit/la-espera-print-kit/source/ && \
	  rm -rf model/$(BUILD)/kit/la-espera-print-kit/source/model/build && \
	  find model/$(BUILD)/kit -name .DS_Store -delete
	printf 'LA ESPERA print kit\n\nEach pose folder has three STLs: plate, ink, glow.\nImport all three as one object with multiple parts, one filament each.\nPETG or ASA. Four 10x3mm neodymium magnets in the back pockets.\nFull instructions: https://laespera.org/print\n' \
	  > $(BUILD)/kit/la-espera-print-kit/README.txt
	mkdir -p $(SITE_DL)
	rm -f $(SITE_DL)/la-espera-print-kit.zip
	cd $(BUILD)/kit && zip -q -r ../../$(SITE_DL)/la-espera-print-kit.zip la-espera-print-kit
	@ls -la $(SITE_DL)/la-espera-print-kit.zip

clean:
	rm -rf $(BUILD)
