# Declarations
LaTeX = pdflatex      # Latex program
BibTeX = bibtex       # Bibtex program
reflib = bibtexReferenceFileName
target := doc1 doc2 doc3 # doc#num = name of LaTeX documents to build.
#endnot = >/dev/null  # uncomment this if you don't want to see LaTeX compiling messages

.PHONY: default all clean

default: all

# Defines some variables
PDF := $(foreach targ,$(target),$(targ).pdf)
BBL := $(foreach targ,$(target),$(targ).bbl)
MYT := $(foreach targ,$(target),$(targ).myt)

#all: $(target).pdf
all: 
	make -s $(PDF)

# Patern rules
# Syntax is
# targets ...: target-pattern: dep-patterns ...
#	commands
#	...

$(MYT): %.myt: %.tex
	if [ ! -e $*.myt ]; then touch $@; $(LaTeX) $< $(endnot); echo "$(LaTeX) $<"; fi

$(BBL): %.bbl: %.myt $(reflib).bib
	$(BibTeX)  $* $(endnot)
	echo "$(BibTeX)  $*"

$(PDF): %.pdf: %.tex %.bbl
	if [ ! $*.aux -nt $*.bbl ]; then $(LaTeX) $*.tex $(endnot); echo "$(LaTeX) $<"; fi
	$(LaTeX) $*.tex $(endnot)
	echo "$(LaTeX) $*.tex"

clean:
	rm -vf *.aux *.bbl *.blg *.log *.toc
	#rm -vf $(target).dvi $(target).ps $(target).pdf $(target).myt
	rm -vf $(foreach targ,$(target),$(targ).dvi) $(foreach targ,$(target),$(targ).myt)
	rm -vf $(foreach targ,$(target),$(targ).ps) $(foreach targ,$(target),$(targ).pdf)
	rm -vf *~

# For the implicit rules to work we must declare the suffixes used here
.SUFFIXES: .tex .aux .toc .bbl .blg .log .dvi .ps .pdf .myt
