######################################################################
# $Id: Makefile 923 2008-09-13 16:45:14Z zalila $
######################################################################

BASE     = these_main
BIBFILES = bibliographie.bib
EPSFILES = # Mettre ici la liste des figures

CODE = # Mettre ici la liste des fichiers sources si des listings sont
       # inclus dans le mmoire.

# All source files
SOURCES = $(CODE)

# List all .tex files to be taken into account in build
TEXFILES = $(BASE).tex $(wildcard chapter_*.tex)

# List all custom .sty, .cls, .bst, .eps files to be taken into
# account.
STYLES = \
	TE/thenst.cls \
	TE/named.sty \
	TE/named.bst \
	TE/enst.eps \
	TE/edite.eps

# Since we have multiple directories, we use TEXINPUTS to tell latex
# where to find files. The ":" at the end is important since it
# appends the values to the default latex one. Otherwise we cannot
# find the standard latex files. It is also important for the shell to
# be a bourne compatible shell.
SHELL         = bash
PWD           = ${shell pwd}
SET_TEXINPUTS = \
	TEXINPUTS="$(PWD):$(PWD)/figures:$(PWD)/TL:$(PWD)/TE:" \
	BSTINPUTS="$(PWD):$(PWD)/TL:$(PWD)/TE:"

# Commands
LATEX    = latex
BIBTEX   = bibtex
DVI2PDF  = dvipdf
DVI2PS   = dvips -j0 -Pdownload35 -D 1200 -Z -t a4 
# Force A4 for dvips scince the default is US Letter on some platforms
# (Darwin...). We also include all fonts and compress bitmappeb ones.
PS2PDF   = ps2pdf -dEmbedAllFonts=true
# Embedd all fonts with ps2pdf
PDF2TEXT = pdftotext
PDFTK    = pdftk
PDFINFO  = pdfinfo
XDVI     = xdvi

all: pdf

pdf: $(BASE).pdf

ps: $(BASE).ps

dvi: $(BASE).dvi

encrypted: $(BASE)_final.pdf

$(BASE)_final.pdf: $(BASE).pdf
	$(PDFTK) $(BASE).pdf output $(BASE)_final.pdf owner_pw PROMPT \
	  allow Printing CopyContents

# For word-count, convert to text and calculate with wc
wc: $(BASE).pdf
	$(PDF2TEXT) $(BASE).pdf
	wc -w $(BASE).txt
	-rm -f $(BASE).txt

$(BASE).pdf: $(BASE).ps
	$(PS2PDF) $(BASE).ps $(BASE).pdf
	$(PDFINFO) $(BASE).pdf

$(BASE).ps: $(BASE).dvi
	$(SET_TEXINPUTS) $(DVI2PS) -o $(BASE).ps $(BASE).dvi

$(BASE).dvi: $(STYLES) $(TEXFILES) $(EPSFILES) $(SOURCES) $(BIBFILES) Makefile
	$(SET_TEXINPUTS) $(LATEX) $(BASE).tex
	$(SET_TEXINPUTS) $(BIBTEX) $(BASE)
	$(SET_TEXINPUTS) $(LATEX) $(BASE).tex
	$(SET_TEXINPUTS) $(LATEX) $(BASE).tex
	$(SET_TEXINPUTS) $(LATEX) $(BASE).tex
	$(MAKE) check

# After the last run of $(LATEX), we ensure there is no duplicated
# labels, missing references or citations.

DUP_LABEL = grep 'LaTeX Warning: Label'
UNDEF_REF = grep 'LaTeX Warning: Reference'
UNDEF_CIT = grep 'LaTeX Warning: Citation'

check:
	@$(DUP_LABEL) $(BASE).log && exit 1 || echo "Labels     are fine"
	@$(UNDEF_REF) $(BASE).log && exit 1 || echo "References are fine"
	@$(UNDEF_CIT) $(BASE).log && exit 1 || echo "Citations  are fine"

view:
	@$(SET_TEXINPUTS) $(XDVI) $(BASE).dvi 2>/dev/null &

filter-citations: $(BASE).dvi distclean
	@$(SET_TEXINPUTS) $(LATEX) $(BASE).tex
	@cat $(BASE).log | $(UNDEF_CIT) | cut -d'`' -f2 \
	  | cut -d"'" -f1 | sort | uniq > used_citations
	@cat $(BIBFILES) | grep '@.*{' | cut -d'{' -f2 \
	  | cut -d',' -f1 | sort | uniq > available_citations
	@diff -u used_citations available_citations \
	  | grep '^+' | cut -d'+' -f2
	@rm -f used_citations available_citations

ARCHIVE_B = thenst
DIST_DEPS := ${shell cat MANIFEST| sed -e 's,$(ARCHIVE_B)/,,' | tr '\n' ' '}

dist: $(ARCHIVE_B).tar.gz

$(ARCHIVE_B).tar.gz: $(DIST_DEPS)
	cd ..; \
	tar -cvvf $(ARCHIVE_B)/$(ARCHIVE_B).tar -T $(ARCHIVE_B)/MANIFEST; \
	gzip -f --best $(ARCHIVE_B)/$(ARCHIVE_B).tar

softclean:
	rm -f *.aux *.log *.bbl *.blg *.flg *.glo *.bmt *.out *.toc *.lof \
	      *.lol *.lot *.maf *.idx *.rul *.mtc*
	rm -f *~ *.bak *.backup

clean: softclean
	rm -f $(BASE).dvi

allclean distclean realclean: clean
	rm -f $(BASE).ps $(BASE).pdf $(BASE).pdf.* $(BASE)_opt.pdf $(BASE)_final.pdf
