# Regenerate Kidsbox enclosure STLs and preview images from the OpenSCAD source.
#
#   make stl     export all printable STLs into stl/
#   make img     render preview PNGs into img/   (needs a display or xvfb)
#   make all     both
#   make clean   remove generated STLs and images
#
# Override the renderer if openscad isn't on PATH:  make OPENSCAD=/path/to/openscad

OPENSCAD ?= openscad
SRC       = kidsbox.scad params.scad lib.scad
PARTS     = body lid speaker_adapter grill

STL_FILES = $(addprefix stl/,$(addsuffix .stl,$(PARTS)))

# Software GL so it works headless under xvfb on a server.
RENDER_ENV = LIBGL_ALWAYS_SOFTWARE=1 GALLIUM_DRIVER=llvmpipe
XVFB       = xvfb-run -a -s "-screen 0 1280x960x24"
IMGOPTS    = --viewall --autocenter --imgsize=1280,960 --colorscheme=Tomorrow

.PHONY: all stl img clean
all: stl img

stl: $(STL_FILES)

stl/%.stl: $(SRC)
	@mkdir -p stl
	$(OPENSCAD) -D 'part="$*"' -o $@ kidsbox.scad

img: $(SRC)
	@mkdir -p img
	$(RENDER_ENV) $(XVFB) $(OPENSCAD) -D 'part="assembly"' -D 'explode=0' \
		--camera=0,0,0,64,0,28,0 $(IMGOPTS) -o img/hero.png kidsbox.scad
	$(RENDER_ENV) $(XVFB) $(OPENSCAD) -D 'part="assembly"' \
		--camera=0,0,0,62,0,28,0 $(IMGOPTS) -o img/assembly.png kidsbox.scad
	$(RENDER_ENV) $(XVFB) $(OPENSCAD) -D 'part="body"' \
		--camera=0,0,0,60,0,25,0 $(IMGOPTS) -o img/body.png kidsbox.scad
	$(RENDER_ENV) $(XVFB) $(OPENSCAD) -D 'part="lid"' \
		--camera=0,0,0,60,0,200,0 $(IMGOPTS) -o img/lid_top.png kidsbox.scad
	$(RENDER_ENV) $(XVFB) $(OPENSCAD) -D 'part="speaker_adapter"' \
		--camera=0,0,0,60,0,20,0 $(IMGOPTS) -o img/speaker_adapter.png kidsbox.scad
	$(RENDER_ENV) $(XVFB) $(OPENSCAD) -D 'part="grill"' \
		--camera=0,0,0,55,0,20,0 $(IMGOPTS) -o img/grill.png kidsbox.scad

clean:
	rm -f stl/*.stl img/*.png
