
# Include Makefile.local  if any
#include Makefile.local

# The main directory of logidee-tools
DIR = ..

# The default language (fallback)
DEFAULT_LANG = en

# Other default values
ifndef SELECTION
SELECTION = none
endif
ifndef TRAINER
TRAINER = false
endif
ifndef CHARTE
CHARTE = default
endif
ifndef CYCLE
CYCLE = false
endif
ifndef LANG
LANG = $(DEFAULT_LANG)
endif
ifndef SLIDE
SLIDE = true
endif
ifndef DESC
DESC = true
endif

# Try to use a supported language
ifeq (en,$(findstring en,$(LANG)))
override LANG = en
else
ifeq (fr,$(findstring fr,$(LANG)))
override LANG = fr
else
ifeq (de,$(findstring de,$(LANG)))
override LANG = de
else
# the language may not be supported
# switch to the language by default
override LANG = $(DEFAULT_LANG)
endif
endif
endif

export TEXINPUTS=$(shell kpsepath tex):./charte/$(CHARTE)//:../charte/$(CHARTE)//:../../charte/$(CHARTE)//

# Values for libxslt/xsltproc
XMLVALID = xmllint --xinclude --noout --valid
XMLOUT = 
XSLTPROC  = xsltproc --xinclude
XSLTPARAM = --stringparam
XSLTXSL =
XSLTIN =
XSLTOUT = >

# Values for xerces-c/xalan-c
# XMLVALID = DOMPrint -v=always
# XMLOUT = > /dev/null
# XSLTPROC="testXSLT"
# XSLTPARAM="-param"
# XSLTXSL="-xsl"
# XSLTIN = "-in"
# XSLTOUT="-out"

XSLT = $(XSLTPROC) $(XSLTPARAM) selection "$(SELECTION)" $(XSLTPARAM) cycle "$(CYCLE)" $(XSLTPARAM) charte "$(CHARTE)" $(XSLTPARAM) trainer "$(TRAINER)" $(XSLTPARAM) lang "$(LANG)" $(XSLTPARAM) slide "$(SLIDE)" $(XSLTPARAM) description "$(DESC)"

NAMES=$(shell find . -name "*.xml" -exec basename {} .xml \;)
CEXTS=.bak .toc .log .aux .dvi .tex
RCEXTS=.ps .pdf .html .txt

help:
	@printf "Usage: \n"
	@printf "make formations:\tbuilds PDF (a4) for all formations \n\t\t\t(formation*.xml) in the directory ;\n"
	@printf "make formations_2pp:\tbuilds PS (a4) with 2 pages per \n\t\t\tsheet for all formations (formation*.xml) ;\n"
	@printf "make formations_show:\tbuilds all slide shows (PDF) for all formations ;\n"
	@printf "\n"
	@printf "The rules available for « formations » are also available for « themes ».\n"
	@printf "\n"
	@printf "make tex:\t builds all TeX document ;\n"
	@printf "make toto:\t builds toto.ps using toto.xml as input ;\n"
	@printf "make check:\t check the validity of all XML documents.\n"


# Generic rules
tex: $(patsubst %.xml,%.tex,$(wildcard *.xml))
html: $(patsubst %.xml,%.html,$(wildcard *.xml))
ps: $(patsubst %.xml,%.ps,$(wildcard *.xml))
2pp: $(patsubst %.xml,%_2pp.ps,$(wildcard *.xml))
pdf: $(patsubst %.xml,%.pdf,$(wildcard *.xml)) $(patsubst %.xml,%_slideshow.pdf,$(wildcard *.xml))
show: slideshow
slide: slideshow
slideshow: $(patsubst %.xml,%_slideshow.pdf,$(wildcard *.xml))
txt: $(patsubst %.xml,%.txt,$(wildcard *.xml))

define mktex
	@echo "include Makefile" >makefile.tmp
	@echo $@: $(shell $(DIR)/verif_dep.sh $(@:%$(2)=%.xml)) >>makefile.tmp
	@echo "	$(XSLT) $(XSLTXSL) $(DIR)/xsl/$(1) $(XSLTIN) $(@:%$(2)=%.xml) $(XSLTOUT) $@" >>makefile.tmp
	make -f makefile.tmp $@
	rm makefile.tmp
endef

FORCE:

# Rules to generate various files
formation%_slideshow.tex: FORCE
	$(call mktex,formation-ptex.xsl,_slideshow.tex)

formation%.tex: FORCE
	$(call mktex,formation-tex.xsl,.tex)

theme%_slideshow.tex: FORCE
	$(call mktex,theme-ptex.xsl,_slideshow.tex)

theme%.tex:  FORCE
	$(call mktex,theme-tex.xsl,.tex)

formations: $(patsubst %.xml,%.pdf,$(wildcard formation*.xml))
formations_show: $(patsubst %.xml,%_slideshow.pdf,$(wildcard formation*.xml))
formations_2pp: $(patsubst %.xml,%_2pp.ps,$(wildcard formation*.xml))

themes: $(patsubst %.xml,%.pdf,$(wildcard theme*.xml))
themes_show: $(patsubst %.xml,%_slideshow.pdf,$(wildcard theme*.xml))
themes_2pp: $(patsubst %.xml,%_2pp.ps,$(wildcard theme*.xml))

%_slideshow.tex: FORCE
	$(call mktex,module-ptex.xsl,_slideshow.tex)

%.tex: FORCE 
	$(call mktex,module-tex.xsl,.tex)

%.txt: %.xml
	$(XSLT) $(XSLTXSL) $(DIR)/xsl/module-txt.xsl $(XSLTIN) $< $(XSLTOUT) $@

formation%.html: formation%.xml 
	@echo "Formations and themes cannot be converted to HTML."
theme%.html: theme%.xml
	@echo "Formations and themes cannot be converted to HTML."

%.html: %.xml
	test -d $@ || mkdir $@
	cp $(DIR)/charte/$(CHARTE)/default.css $@/default.css
	cp $(DIR)/charte/$(CHARTE)/icones/*.png $@/
	cp $(DIR)/charte/$(CHARTE)/html/$(LANG)/*.html $@/
	$(XSLT) $(XSLTPARAM) dir "$(CURDIR)/$@" $(XSLTXSL) $(DIR)/xsl/module-html.xsl $(XSLTIN) $< $(XSLTOUT) $@/index.html
	for i in `cat $@/images`; do \
	    convert $$i $@/`basename $${i%%.eps}.png`; \
	done
	rm $@/images
	touch $@

%_slideshow.dvi: %_slideshow.tex $(wildcard *.eps) $(DIR)/charte/$(CHARTE)/tex/logidoc.cls
	latex $<

%.dvi: %.tex $(wildcard *.eps) $(DIR)/charte/$(CHARTE)/tex/logidoc.cls
	# Three times to get cross-references right (argh)
	latex $< && latex $< && latex $<

%_slideshow.ps: %_slideshow.dvi
	dvips -t a4 -t landscape -f $< > $@
	#dvips -T 31.0cm,21.1cm -f $< > $@

%_slideshow.pdf: %_slideshow.ps
	ps2pdf $< $@

%.pdf: %.ps
	ps2pdf $< $@

%.ps: %.dvi
	dvips -f $< > $@

%_2pp.ps: %.ps
	ps2ps $< tmp.ps
	psnup -2 tmp.ps > $@
	rm tmp.ps

%_slideshow: %_slideshow.pdf

%: %.xml
	make $@.pdf $@_slideshow.pdf

# Rules for checking the validity of XML files
check: $(patsubst %.xml, %.xml-check, $(wildcard *.xml))

%.xml-check: %.xml $(DIR)/dtd/module.dtd
	@echo "*** Validation of $<"
	$(XMLVALID) $< $(XMLOUT)

# Rules for cleaning the directories
define files
`for EXT in $(1); do \
     for FILEBASE in $(NAMES); do \
         echo $$FILEBASE*$$EXT; \
     done; \
done`
endef

clean::
	rm -rf $(call files, $(CEXTS))

realclean::
	rm -rf $(call files, $(CEXTS) $(RCEXTS))
	rm -rf .log texput.log images/.gscache*

rclean: realclean

# Meta information about target & rules for make
.PHONY: formations formations_show formations_2pp themes themes_show themes_2pp clean help tex txt ps pdf 2pp slide show slideshow check %.xml-check 
.PRECIOUS: %.ps %.pdf %_slideshow.pdf %_slideshow.ps
#.SECONDARY: %.dvi %_slideshow.dvi %.tex %_slideshow.tex
