#
# Makefile for converting svg files
# to something else: png, pdf, eps
#
# There would be absolutely no reason, at all, to produce EPS neither PNG of this covers. Anyway you can do it it you want to.
#
# 10-Oct-2012 - Philippe Chauvat / Bacula Systems
# 27-jul-2017 - Philippe Chauvat / Bacula Systems : Improvements with date and version management
#               Work is based on svg files named *.template which contain __BVERSION__ and __BDATE__
#		These "variables" are going to be seded to the current version and the current date.
# 

AT=@   # Make this empty to get command line echoed.

INKSCAPE=inkscape
INKSCAPE_FLAGS=-z
SVG_TO_PDF=-A
SVG_TO_EPS=-E
PDFDIR=../pdf
EPSDIR=../eps
TEMPLATES=$(wildcard *.template)
SVGS=$(TEMPLATES:.template=.svg)
PDFS=$(SVGS:.svg=.pdf)
EPSS=$(SVGS:.svg=.eps)
VERSIONFILE=$(PWD)/../../latex/version.tex
BDATE := $(shell date +%Y-%m-%d)

vpath %.eps $(EPSDIR)

all: svg pdf

.SUFFIXES:
.PHONY:
.DONTCARE:

#
# PDF images creation
pdf: $(TEMPLATES) $(PDFS)

$(TEMPLATES):

svg: 
%.svg: %.template
	$(AT)echo "$< :   Handling version number and date"
	$(AT)sed "s/__BVERSION__/`awk '{print $$1}' $(VERSIONFILE)`/g" $< > $@
	$(AT)sed -i 's/__BDATE__/$(BDATE)/g' $@


$(PDFS): | $(PDFDIR)
$(PDFDIR):
	$(AT)echo "Creating PDF images directory..."
	$(AT)mkdir $(PDFDIR)
	$(AT)echo "Done"



%.pdf: %.svg
	$(AT)echo "$< :   Converting to PDF"
	$(AT)${INKSCAPE} ${INKSCAPE_FLAGS} ${SVG_TO_PDF} $(PDFDIR)/$@ $<
#
# EPS images creation
vpath %.pdf $(PDFDIR)
eps: $(EPSS)
$(EPSS): | $(EPSDIR)
$(EPSDIR):
	$(AT)echo "Creating EPS images directory..."
	$(AT)mkdir $(EPSDIR)
	$(AT)echo "Done"
%.eps: %.svg
	${INKSCAPE} ${INKSCAPE_FLAGS} ${SVG_TO_EPS} $(EPSDIR)/$@ $<

clean:
	$(AT)(if [ -e $(PDFDIR) ]; then cd $(PDFDIR) ; rm -f $(PDFS); fi)
	$(AT)(if [ -e $(EPSDIR) ]; then cd $(EPSDIR) ; rm -f $(EPSS); fi)
	$(AT)(if [ -e $(PNGDIR) ]; then cd $(PNGDIR) ; rm -f $(PNGS); fi)
	$(AT)rm -f *.svg
	$(AT)rm -f *~

distclean: clean
