# project file name (use for schematic and board layout)
NAME ?= rdm_splitter
SUBSHEET ?= 
# path to KiCad CLI
KICAD = kicad-cli
# path to qeda
QEDA := qeda
# path to KiBOM
KIBOM := ./KiBOM_CLI.py
# path to InteractiveHtmlBom
IBOMGEN := ~/.local/share/kicad/7.0/3rdparty/plugins/org_openscopeproject_InteractiveHtmlBom/generate_interactive_bom.py

# read project version
VERSION := $(shell cat version)
# current date for stamping output
DATE = $(shell date +%Y-%m-%d)
# revision based on number of changes on schematic or board layout
REVISION := $(shell git log --pretty=oneline "${NAME}.kicad_sch" "${NAME}.kicad_pcb" | wc -l)

# generate file with version information
VERSIONED_EXT = kicad_sch kicad_pcb kicad_pro json
define version_rule
%.versioned.$1: %.$1
	cp $$< $$@
	sed --in-place 's/\$$$$version\$$$$/${VERSION}/g' $$@
	sed --in-place 's/\$$$$date\$$$$/${DATE}/g' $$@
	sed --in-place 's/\$$$$revision\$$$$/${REVISION}/g' $$@
	sed --in-place 's/\.kicad_sch/.versioned.kicad_sch/g' $$@
endef
$(foreach EXT,$(VERSIONED_EXT),$(eval $(call version_rule,$(EXT))))
VERSIONED_SHEET = $(foreach SHEET,$(NAME) $(SUBSHEET),$(SHEET).versioned.kicad_sch)

FABRICATION_DIR := fabrication
IBOM := ${FABRICATION_DIR}/ibom.html

all: $(VERSIONED_SHEET) ${NAME}.sch.pdf ${NAME}.brd-top.png ${NAME}.brd-bot.png ${NAME}.bom.csv fab

fab: ${FABRICATION_DIR} ${IBOM}

# generate fabrication files (gerbers/drill/BoM/PnP)
${FABRICATION_DIR}: ${NAME}.versioned.kicad_sch ${NAME}.versioned.kicad_pcb
	kikit fab jlcpcb --assembly --no-drc --field JLCPCB,LCSC --schematic $^ $@

# generate fabrication files (gerbers/drill/uncorrected PnP)
#${FABRICATION_DIR}: ${NAME}.versioned.kicad_pcb
#	mkdir -p ${FABRICATION_DIR}
#	$(KICAD) pcb export gerbers --output ${FABRICATION_DIR} $<
#	$(KICAD) pcb export drill --output ${FABRICATION_DIR}/ $<
#	$(KICAD) pcb export pos --output ${FABRICATION_DIR}/${NAME}.versioned.pos $<

# generate interactive BoM
${IBOM}: ${NAME}.versioned.kicad_pcb
	python $(IBOMGEN) --no-browser --dest-dir `dirname $@` --name-format `basename $@ ".html"` --show-fields "Value" $< &>/dev/null

# generate symbols and footprints from parts
lib:
	$(QEDA) generate qeda

# generate printable version (PDF) of schematic
%.sch.pdf: %.versioned.kicad_sch %.versioned.kicad_pro
	$(KICAD) sch export pdf --output $@ $<

# generate render from layout (top side)
%.brd-top.png: %.versioned.kicad_pcb
	pcbdraw plot --silent $< --dpi 600 --side front $@

# generate render from layout (bottom side)
%.brd-bot.png: %.versioned.kicad_pcb
	pcbdraw plot --silent $< --dpi 600 --side back $@

# export Bill of Material (as CSV)
%.bom.xml: %.versioned.kicad_sch %.versioned.kicad_pro
	$(KICAD) sch export python-bom --output $@ $<

# export Bill of Material (as CSV)
%.bom.csv: %.bom.xml
	python $(KIBOM) $< $@

# generate panel
PANEL_DIR := panel
panel: panel.kicad_pcb
panel.kicad_pcb: ${NAME}.versioned.kicad_pcb ${NAME}.versioned.kicad_pro ${NAME}.versioned.kicad_sch ${NAME}.versioned.json
	kikit panelize -p ${NAME}.versioned.json ${NAME}.versioned.kicad_pcb $@
	sed --in-place 's/\"missing_courtyard\": \"warning\"/\"missing_courtyard\": \"ignore\"/g' $(patsubst %.kicad_pcb,%.kicad_pro,$@) # the mouse bites don't have a courtyard
	kikit fab jlcpcb --assembly --missingError --field JLCPCB,LCSC --schematic ${NAME}.versioned.kicad_sch $@ ${PANEL_DIR}
	pcbdraw --silent $@ --dpi 600 panel.brd-top.png
	pcbdraw --silent $@ --dpi 600 --back panel.brd-bot.png

clean:
	rm -f $(foreach EXT,$(VERSIONED_EXT),${NAME}.versioned.$(EXT))
	rm -f ${NAME}.sch.pdf ${NAME}.brd-top.png ${NAME}.brd-bot.png ${NAME}.versioned.xml ${NAME}.bom.csv
	rm -f ${NAME}.versioned.kicad_prl ${NAME}.versioned.kicad_pro-bak ${NAME}.versioned.xml ${NAME}.versioned.csv
	rm -f ${IBOM}
	rm -rf ${FABRICATION_DIR}
	rm -f panel.kicad_pcb panel.kicad_pro 
	rm -rf ${PANEL_DIR}
