# -*- mode: makefile; coding: latin-9-unix -*-
#% Time-stamp: <2005-11-09 20:49:14 alain> %
#% $Id: Makefile,v 1.3 2006/04/19 18:06:17 cvsalain Exp $ %
#*******************************************************************************
# Makefile for LaTeX documents
#
# Copyright 2003 by Alain Jaffre <jack.r [at] free.fr>
# Provided under the terms of
# GNU General Public License <http://www.gnu.org/licenses/gpl.html>
#
# It was build for my personal use, but could interest some other people.
# It try to be as generic as possible
#*******************************************************************************

#*******************************************************************************
# Document base name that we want to build
# If there is, it must contain \printindex and/or \printglossary
DOC = excel_vba

#*******************************************************************************
# Directory where are stored all used images .eps and .png format
IMAGEDIR =img

#*******************************************************************************
# Final directory where we want to save the final results
FINALDIR = final

#*******************************************************************************
# Language (fr, gb)
# If set, html will use the corresponding language
# If set and run final, files will be in the corresponding language directory
LNG = fr




#*******************************************************************************
# NO CHANGE NEEDED BELOW THAT LINE
#*******************************************************************************




#*******************************************************************************
# Processor
LATEX = latex
MAKEINDEX = makeindex
DVIPS = dvips
PDF = pdflatex
HTML = hevea
HACHA = hacha

#*******************************************************************************
# Error message
BADREF = "get cross-references right"

#*******************************************************************************
# Default values
MODE = --interaction batchmode
FINALPATH = $(FINALDIR)/
HTMLPARAM = 

#*******************************************************************************
# Language adaptation
ifdef LNG
FINALPATH:=$(FINALPATH)$(LNG)/
endif
ifeq ($(findstring fr,$(LNG)),fr)
HTMLPARAM = -francais
endif

#*******************************************************************************
# Check what we have to do
INDEX = $(shell cat $(DOC).tex | grep \printindex)
GLOSSARY = $(shell cat $(DOC).tex | grep \printglossary)
ifdef IMAGEDIR
IMAGES = $(shell ls -l -d $(IMAGEDIR) | grep d)
endif

.PHONY: help usage dvi ps pdf html cuthtml text clean proper all final

help: usage

usage:
	@echo ""
	@echo "Makefile for latex document"
	@echo "USAGE: "
	@echo ""
	@echo "help     - That usage information"
	@echo "usage    - That usage information"
	@echo "dvi      - Build the dvi file"
	@echo "ps       - Build the postscript file to be print"
	@echo "pdf      - Build the pdf file"
	@echo "html     - Build a single html file"
	@echo "cuthtml  - Build multiple html files. Main one is index.html"
	@echo "text     - Build a simple text file"
	@echo "clean    - Remove all intermediate files"
	@echo "proper   - Remove all files except source ones"
	@echo "all      - Build all output files (dvi, ps, pdf, html, cuthtml, text)"
	@echo "final    - Build all output files (dvi, ps, pdf, html, cuthtml, text) and move them to the final directory"
	@echo ""

dvi: $(DOC).tex
	make clean
	while ( \
		$(LATEX) $(MODE) $(DOC).tex ; \
		grep $(BADREF) $(DOC).log > /dev/null ) do true ; \
	done
	@echo "$(INDEX)"
ifeq ($(findstring \printindex,$(INDEX)),\printindex)
# We have an index
	@for idxfile in ./*.idx ; do \
		$(MAKEINDEX) $$idxfile ; \
	done
	while ( \
		$(LATEX) $(MODE) $(DOC).tex ; \
		grep $(BADREF) $(DOC).log > /dev/null ) do true ; \
	done
endif
ifeq ($(findstring \printglossary,$(GLOSSARY)),\printglossary)
# We have a glossary
	$(MAKEINDEX) -s nomencl.ist -o $(DOC).gls $(DOC).glo
	while ( \
		$(LATEX) $(MODE) $(DOC).tex ; \
		grep $(BADREF) $(DOC).log > /dev/null ) do true ; \
	done
endif

ps: $(DOC).tex
	make dvi
	$(DVIPS) -o $(DOC$).ps $(DOC).dvi

pdf: $(DOC).tex
	make dvi
	while ( \
		$(PDF) $(MODE) $(DOC).tex ; \
		grep $(BADREF) $(DOC).log > /dev/null ) do true ; \
	done

html: $(DOC).tex
	make dvi
	while ( \
		$(HTML) $(HTMLPARAM) $(DOC).tex 2> $(DOC).log ; \
		grep $(BADREF) $(DOC).log > /dev/null ) do true ; \
	done

cuthtml: $(DOC).tex
	make html
	$(HACHA) -o index.html $(DOC).html

text: $(DOC).tex
	make dvi
	while ( \
		$(HTML) -text $(DOC).tex 2> $(DOC).log ; \
		grep $(BADREF) $(DOC).log > /dev/null ) do true ; \
	done

clean:
	rm -f *~
	rm -f *.aux
	rm -f *.glo
	rm -f *.gls
	rm -f *.idx
	rm -f *.ilg
	rm -f *.ind
	rm -f *.lof
	rm -f *.log
	rm -f *.lot
	rm -f *.out
	rm -f *.toc
	rm -f $(DOC).haux
	rm -f $(DOC).hind
	rm -f $(DOC).htoc
	rm -f $(DOC).lol
	rm -f $(DOC).image.tex

proper:
	make clean
	rm -f $(DOC).dvi
	rm -f $(DOC)*.ps
	rm -f $(DOC).pdf
	rm -f $(DOC).html
	rm -f $(DOC)[0-9][0-9][0-9].html
	rm -f $(DOC).txt
	rm -f *.gif
	rm -f index.html

all: $(DOC).tex
# dvi
	while ( \
		$(LATEX) $(MODE) $(DOC).tex ; \
		grep $(BADREF) $(DOC).log > /dev/null ) do true ; \
	done
ifeq ($(findstring \printindex,$(INDEX)),\printindex)
# We have an index
	$(MAKEINDEX) $(DOC)
	while ( \
		$(LATEX) $(MODE) $(DOC).tex ; \
		grep $(BADREF) $(DOC).log > /dev/null ) do true ; \
	done
endif
ifeq ($(findstring \printglossary,$(GLOSSARY)),\printglossary)
# We have a glossary
	$(MAKEINDEX) -s nomencl.ist -o $(DOC).gls $(DOC).glo
	while ( \
		$(LATEX) $(MODE) $(DOC).tex ; \
		grep $(BADREF) $(DOC).log > /dev/null ) do true ; \
	done
endif
# ps
	$(DVIPS) -o $(DOC$).ps $(DOC).dvi
# pdf
	while ( \
		$(PDF) $(MODE) $(DOC).tex ; \
		grep $(BADREF) $(DOC).log > /dev/null ) do true ; \
	done
# html
	while ( \
		$(HTML) $(HTMLPARAM) $(DOC).tex 2> $(DOC).log ; \
		grep $(BADREF) $(DOC).log > /dev/null ) do true ; \
	done
# cuthtml
	$(HACHA) -o index.html $(DOC).html
# text
	$(HTML) -text $(DOC).tex 2> $(DOC).log

final: $(DOC).tex
ifdef IMAGEDIR
ifeq ($(findstring d,$(IMAGES)),d)
# We really have an images directory so copy it
	mkdir -p $(FINALPATH)$(IMAGEDIR)
	cp -f img/*.png $(FINALPATH)$(IMAGEDIR)/
endif
endif
	make all
	mv $(DOC).dvi $(FINALPATH)
	mv $(DOC).ps $(FINALPATH)
	mv $(DOC).pdf $(FINALPATH)
	mv $(DOC).html $(FINALPATH)
	mv $(DOC).txt $(FINALPATH)
	mv index.html $(FINALPATH)
	mv $(DOC)[0-9][0-9][0-9].html $(FINALPATH)
	mv *.gif $(FINALPATH)
	make clean





