RST = python tools/rst.py

TEXDOCUMENTS  = $(basename $(wildcard *.tex))
RSTDOCUMENTS  = $(basename $(wildcard *.txt))

SOURCES       = $(addsuffix .tex,$(TEXDOCUMENTS))
DVI           = $(addsuffix .dvi,$(TEXDOCUMENTS))
POSTSCRIPT    = $(addsuffix .ps,$(TEXDOCUMENTS))
POSTSCRIPT_GZ = $(addsuffix .ps.gz,$(TEXDOCUMENTS))
PDF           = $(addsuffix .pdf,$(TEXDOCUMENTS))
RSTSOURCES    = $(addsuffix .txt,$(RSTDOCUMENTS))
HTML          = $(TEXDOCUMENTS) $(addsuffix .html,$(RSTDOCUMENTS))

all: dvi ps ps.gz html
dvi: $(DVI)
ps: $(POSTSCRIPT)
ps.gz: $(POSTSCRIPT_GZ)
pdf: $(PDF)
html: $(HTML)


${DVI}: %.dvi: %.tex
	latex $<
	latex $<

${POSTSCRIPT}: %.ps: %.dvi
	dvips -o $@ $<

${POSTSCRIPT_GZ}: %.ps.gz: %.ps
	gzip -c $< > $@

${PDF}: %.pdf: %.tex
	rm -f $*.out $*.log $*.aux
	xelatex $<
	rm -f $*.out $*.log $*.aux

%: %.tex %.dvi
	latex2html -local_icons $<
	touch $@

%.html: %.txt
	$(RST) $< > $@


clean:
	rm -f -- *~ *.log *.aux *.out *.nav *.snm *.toc ${DVI} ${POSTSCRIPT} ${POSTSCRIPT_GZ} ${PDF}
	rm -fr ${HTML}