#
# Compilation du roman Autonomie : la liberté d'expression
#
# Génère les sorties PDF, HTML, archives tgz, tbz2 et zip.
#
# Écrit par Thomas Tempé et T.Pierron
#

LATEX=pdflatex 
LATEX2HTML=rubber -d
LATEX2HTML=latex2html
CHAPITRES=$(wildcard chap*.tex)
WIKI_IN=$(patsubst %.tex,%.wiki-in,$(CHAPITRES)) 

.PHONY: wiki all help clean latex book info aide apropos archives autonomy_tex
.PHONY: pdf html arch update $(wildcard chap*.wiki-in) latex

all: autonomy.pdf html autonomy_html.zip autonomy_latex.zip


help aide apropos info RTFM:
	@echo "Les principales cibles disponibles sont les suivantes:"
	@echo ""
	@echo "- [1mmake book[0m : génère le livre à partir des chapitres tex  au format pdf et HTML."
	@echo "  [1mmake pdf[0m  : génère le livre uniquement au format pdf."
	@echo "  [1mmake html[0m : format HTML uniquement."
	@echo ""
	@echo "- [1mmake archives[0m : génère les archives de ce package, ainsi que la sortie HTML."
	@echo ""
	@echo "- [1mmake all[0m : génère le livre, plus les archives correspondantes."
	@echo ""
#	@echo "- [1mmake chapXX.wiki-in[0m : télécharge le chapitre XX à partir du wiki, et le convertit en LaTeX."
#	@echo ""
#	@echo "- [1mmake chapXX.wiki-out[0m : met à jour le wiki à partir du fichier chapXX.tex"
#	@echo ""
#	@echo "- [1mmake update[0m : récupère la dernière version de chaque chapitre à partir du Wiki"
#	@echo "Les fichiers chapXX.wiki-in doivent ensuite être renommés manuellement en chapXX.tex"
#	@echo ""
	@echo "Nécessite les commandes suivantes: pdflatex ou rubber, latex2html, python"
	@echo ""

book: latex pdf html

# Le chapitre 0 est déjà inclut dans le template
CHAPITRES_N0=$(patsubst chap00.tex,,$(CHAPITRES))

#autonomy.tex: $(CHAPITRES_N0) autonomy_template.tex
#	awk '/__SED_REPLACE__/ { print "$(patsubst %,\\input{%}\n,$(CHAPITRES_N0))" } \
#	    !/__SED_REPLACE__/ { print }' autonomy_template.tex > autonomy.tex
#'

html autonomy/index.html: autonomy.tex livre.cls licence.tex $(CHAPITRES)
	$(LATEX2HTML) autonomy

#chap%.wiki-out: chap%.tex
#	./scripts/latex2wiki.py $@
#	./scripts/upload.py $@
#
#chap%.wiki-in:
#	./scripts/download.py $@
#	./scripts/wiki2latex.py $@

pdf autonomy.pdf: autonomy.tex livre.cls licence.tex $(CHAPITRES)
	$(LATEX) autonomy.tex
	$(LATEX) autonomy.tex

#
# Génération des archives
#
autonomy_html.zip autonomy_html.tar.gz autonomy_html.tar.bz2: html
	zip -r autonomy_html.zip autonomy
	tar -jcvf autonomy_html.tar.bz2 autonomy
	tar -zcvf autonomy_html.tar.gz autonomy

autonomy_latex.zip autonomy_latex.tar.gz autonomy_latex.tar.bz2: autonomy_template.tex livre.cls licence.tex README.txt $(CHAPITRES)
	-mkdir autonomy_latex
	cp -r autonomy_template.tex Makefile livre.cls scripts README.txt $(CHAPITRES) autonomy_latex
	zip -r autonomy_latex.zip autonomy_latex
	tar -zcvf autonomy_latex.tar.gz autonomy_latex
	tar -jcvf autonomy_latex.tar.bz2 autonomy_latex
	-rm -rf autonomy_latex

autonomy.pdb: html
	-plucker-build file:///home/thomas/public_html/autonomy/autonomy/index.html > autonomy.pdb

archives arch: autonomy_html.zip autonomy_latex.zip

update: $(WIKI_IN) $(CHAPITRES)


clean:
	-rm autonomy.pdf autonomy.dvi *.wiki-out *.wiki-in *~ *.pdb
#rubber --clean
#-find . -name \*~ -o -name \#\* |xargs rm
