#
# Makefile.kicad - Makefile template for KiCAD projects
#
# Written 2011 by Werner Almesberger
# Copyright 2011 Werner Almesberger
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#

#
# Define before including this template:
#
# NAME		project name
# VERSION	"release" version number
#

ifndef TOP
TOP = ../..
endif

PLOT_BRD = pcbnew --plot=ps --plot-fill-all-zones
CPTX = $(TOP)/eda-tools/mlztx/cptx
GMERGE = $(TOP)/eda-tools/fab/gmerge
DRL2GERBER = $(TOP)/eda-tools/fab/drl2gerber
PRETTYGERBV = $(TOP)/eda-tools/fab/prettygerbv
POS2FAB = $(TOP)/eda-tools/fab/pos2fab
FPDOC = PATH=$$PATH:$(TOP)/eda-tools/fab:$(TOP)/eda-tools/fpd2pdf fpdoc

DIR = $(shell pwd | sed 's|.*/||')

SCH=$(shell pwd)/$(NAME).sch
BRD=$(shell pwd)/$(NAME).brd


.PHONY:		all gen generate sch brd xpdf front back clean
.PHONY:		gerber gerbv fab fab-pcb fab-smt overview

all:
		@echo "make what ? target: gen sch brd xpdf front back clean"
		@exit 1

gen generate:
		eeschema --plot=ps $(SCH)
		# need scripts

sch:
		eeschema $(SCH)

brd:
		pcbnew $(BRD)

xpdf:
		xpdf $(NAME).pdf

front:		$(NAME)-Front.ps
		lpr $<

back:		$(NAME)-Back.ps
		lpr $<

# --- DIY production (toner transfer) -----------------------------------------

#
# Postscript for production of front/back layer, using the toner transfer
# method. Note that other artwork transfer methods may require different
# mirror settings.
#
# We use  --ps-pads-drill-opt=none  to avoid having any hole before drilling,
# which yields the best results with a CNC drill. For manual drilling, "real"
# would be preferrable. Do not use "small", for this created holes that are
# larger (!) than designed.
#

%-Front.ps:	%.brd
		$(PLOT_BRD) --layers=Front --plot-mirror $<

%-Back.ps:	%.brd
		$(PLOT_BRD) --layers=Back $<

# --- Industrial production ---------------------------------------------------

GERBERS = $(NAME)-SilkS_Front.gto $(NAME)-Mask_Front.gts \
	  $(NAME)-Front.gtl $(NAME)-Back.gbl $(NAME)-Mask_Back.gbs \
	  $(NAME)-PCB_Edges.gbr $(NAME)-SoldP_Front.gtp

PCB_FILES = README-PCB $(NAME)-front.png $(NAME)-back.png \
	    $(NAME)-PCB_Edges.dxf $(NAME).drl $(GERBERS)

SMT_FILES = README-SMT $(NAME)-front.png $(NAME)-bom.csv $(NAME)-pos.csv \
	    $(NAME)-footprints.pdf

gerber:		$(GERBERS)

$(GERBERS):	$(NAME).brd Makefile
		pcbnew --plot=gerber --plot-aux-origin \
		  --layers=`pcbnew --list-layers $(BRD) | tr '\012' ,` \
		  --plot-fill-all-zones --plot-exclude-edge \
		  $(BRD)
		$(GMERGE) $(NAME)-SilkS_Front.gto $(NAME)-Comments.gbr >_tmp \
		  || { rm -rf _tmp; exit 1; }
		mv _tmp $(NAME)-SilkS_Front.gto

fab:		fab-pcb fab-smt

$(NAME)-PCB_Edges.dxf: $(NAME).brd
		pcbnew --plot=dxf --plot-aux-origin --layers=PCB_Edges $(BRD)

$(NAME).drl:	$(NAME).brd
		pcbnew --drill --drill-aux-origin $(BRD)

fab-pcb:	$(PCB_FILES)
		mkdir -p fab
		tar Ccfz .. fab/$(NAME)-pcb-$(VERSION).tar.gz \
		  $(PCB_FILES:%=$(DIR)/%)
		cd ..; zip -l $(DIR)/fab/$(NAME)-pcb-$(VERSION).zip \
		  $(PCB_FILES:%=$(DIR)/%)

gerbv:
		gerbv $(NAME)-SilkS_Front.gto \
		  $(NAME)-SoldP_Front.gtp \
		  $(NAME)-Front.gtl \
		  $(NAME)-Mask_Front.gts \
		  $(NAME)-Mask_Back.gbs \
		  $(NAME)-Back.gbl \
		  $(NAME)-PCB_Edges.gbr

$(NAME)Front.pos:
		pcbnew --pos $(BRD)

%-pos.csv:	%Front.pos %.cmp
		$(POS2FAB) $^ >$@ || { rm -f "$@"; exit 1; }

../bom/$(NAME)-bom.csv:
		$(MAKE) -C ../bom $(NAME)-bom.csv

%-bom.csv:	../bom/%-bom.csv
		mv $< $@

%-footprints.pdf: %.pro %.cmp %Front.pos
		$(FPDOC) -a FIDUCIAL $< "$@" || { rm -f "$@"; exit 1; }

fab-smt:	$(SMT_FILES)
		mkdir -p fab
		tar Ccfz .. fab/$(NAME)-smt-$(VERSION).tar.gz \
		  $(SMT_FILES:%=$(DIR)/%)
		cd ..; zip -l $(DIR)/fab/$(NAME)-smt-$(VERSION).zip \
		  $(SMT_FILES:%=$(DIR)/%)

upload:
		qippl \
		  fab/$(NAME)-pcb-$(VERSION).tar.gz \
		  fab/$(NAME)-pcb-$(VERSION).zip \
		  fab/$(NAME)-smt-$(VERSION).tar.gz \
		  fab/$(NAME)-smt-$(VERSION).zip \
		  wpan/fab

# --- Overview images ---------------------------------------------------------

%-Drill.gbr:	%.drl
		$(DRL2GERBER) $^ >$@ || { rm -f "$@"; exit 1; }

$(NAME)-front.png: gerber $(NAME)-Drill.gbr
		$(PRETTYGERBV) -d $(NAME)-Drill.gbr $(NAME) front \
		  $(NAME)-front.png

$(NAME)-back.png: gerber $(NAME)-Drill.gbr
		$(PRETTYGERBV) -d $(NAME)-Drill.gbr $(NAME) back \
		  $(NAME)-back.png

$(NAME)-overview.png: gerber $(NAME)-Drill.gbr
		$(PRETTYGERBV) -d $(NAME)-Drill.gbr $(NAME) all \
		  $(NAME)-overview.png

overview:	$(NAME)-overview.png
		display $(NAME)-overview.png

uoverview:	$(NAME)-overview.png
		qippl $(NAME)-overview.png \
		  wpan/tmp/$(NAME)-$(VERSION)-overview.png

# --- Cleanup -----------------------------------------------------------------

clean::
		rm -f $(NAME)-Front.ps $(NAME)-Back.ps
		rm -f $(NAME).drl $(NAME)-Drill.gbr
		rm -f $(NAME)-PCB_Edges.gbr $(NAME)-PCB_Edges.dxf
		rm -f $(NAME)-Front.gtl $(NAME)-Mask_Front.gts
		rm -f $(NAME)-SilkS_Front.gto $(NAME)-SoldP_Front.gtp
		rm -f $(NAME)-Back.gbl $(NAME)-Mask_Back.gbs
		rm -f $(NAME)-SilkS_Back.gbo $(NAME)-SoldP_Back.gbp
		rm -f $(NAME)-Comments.gbr $(NAME)-Drawings.gbr
		rm -f $(NAME)-overview.png $(NAME)-front.png $(NAME)-back.png
		rm -f $(NAME)Front.pos $(NAME)-pos.csv
		rm -f $(NAME)-bom.csv $(NAME)-footprints.pdf

spotless::	clean
		rm -f '$$'savepcb.000 '$$'savepcb.brd
		rm -f $(NAME)-cache.lib $(NAME)-cache.bak
		rm -f $(NAME).000
		rm -f $(NAME).net