TEX=xelatex
SOURCE=.

SRC  =$(wildcard *.rst)
PDFS = $(SRC:.rst=.pdf)

all: tmp/description.rst $(PDFS)

DIR = $(sort $(shell find $(SOURCE) -name '*.rst' -print))
SOURCES = $(sort $(wildcard resources/*.tex))

null  :=
space := $(null) #
comma := ,
TEX_STYLE := $(subst $(space),$(comma),$(strip $(SOURCES)))


# Generate the latex file from rst
tmp/%.tex: %.rst $(SOURCES) tmp/description.rst | tmp
	rst2latex --no-section-subtitles --no-section-numbering --documentclass=article --documentoption=12pt,A4 --stylesheet=$(TEX_STYLE) $< $@

tmp/description.rst: script.sh | tmp
	./script.sh

# Generate the pdf with latex
tmp/%.pdf: tmp/%.tex | tmp
	$(TEX) -output-directory tmp $<
	while grep 'Rerun to get ' tmp/$*.log ; do $(TEX) -output-directory tmp $< ; done

# Put the pdf in the right place
%.pdf: tmp/%.pdf | tmp
	cp $< $@
	rm -r tmp

clean:
	rm -r tmp

tmp:
	mkdir tmp

.PHONY: all clean
