# variable initialization
DOCUMENT = tgc05

.PHONY: all clean tmp_tgz

# The following variable contains the names of other `tex' files
# except for $(DOCUMENT).tex
#
# All of these files are considered to be $(DOCUMENT).tex prerequisits
OTHER_TEX_FILES = $(subst $(DOCUMENT).tex,,$(shell ls *.tex))

all: $(DOCUMENT).ps $(DOCUMENT).pdf

FIGURES_DIA = $(shell ls Figures/Dia/*.dia 2>/dev/null)
FIGURES_DIA_EPS = $(subst dia,eps,$(FIGURES_DIA))
FIGURES_DIA_PDF = $(subst dia,pdf,$(FIGURES_DIA))

FIGURES_EPS = $(shell ls Figures/Eps/*.eps 2>/dev/null)
FIGURES_EPS_PDF = $(subst eps,pdf,$(FIGURES_EPS))

FIGURES_FIG = $(shell ls Figures/Fig/*.fig 2>/dev/null)
FIGURES_FIG_EPS = $(subst fig,eps,$(FIGURES_FIG))
FIGURES_FIG_PDF = $(subst fig,pdf,$(FIGURES_FIG))

# all EPS Figures
EPS_FIGURES = $(FIGURES_DIA_EPS) $(FIGURES_EPS) $(FIGURES_FIG_EPS)

# all PDF Figures
PDF_FIGURES = $(FIGURES_DIA_PDF) $(FIGURES_EPS_PDF) $(FIGURES_FIG_PDF)

# Figures, which are originally in `eps' format
# FIGURES.EPS

# main targets

$(DOCUMENT).dvi: $(DOCUMENT).tex common.bib $(EPS_FIGURES) $(OTHER_TEX_FILES)
	touch $(subst tex,ind,$<) && \
	latex "$<" && \
	bibtex "$(subst .tex,,$<)" && \
	latex "$<" && \
	latex "$<"
	@# The first run of the `latex' produces certain files which
	@# are required by `bibtex' and `makeindex' programs.
	@# The second run of the `latex' produces corrent `dvi' file
	@# with respect to what `bibtex' and `makeindex' programs
	@# produced.
	@# The third latex is invoked because the second run complains that
	@# the labels may have changed.

$(DOCUMENT).ps: $(DOCUMENT).dvi
	dvips -f "$<" > "$@"

$(DOCUMENT).pdf: $(DOCUMENT).ps common.bib $(PDF_FIGURES) $(OTHER_TEX_FILES)
	ps2pdf $(DOCUMENT).ps || eps2pdf $(DOCUMENT).ps

# building of Figures

%.eps: %.fig
	@echo "fig -> eps $<"
	@fig2dev -L eps "$<" "$@"

%.eps: %.dia
	@echo "dia -> eps $<"
	@dia --nosplash -e "$@" "$<"

%.pdf: %.eps
	@echo "eps -> pdf $<"
	@epstopdf "$<"

common.html: common.bib
	bibtex2html common.bib
	# the next command:
	# 1. adds to the HTML page one META tag which determines
	#    the used encoding (Latin-2)
	# 2. Removes the bottom "banner" referring to the bibtex2html homepage
	# 3. Change the `.html' text of the link into the `html'
	#    (The `.html' link text is generated by bibtex2html in the case when
	#    the original bibtex file contains an URL link ending with `.html'.
	#    If the URL link ends with a name of the directory, then bibtex2html
	#    generates `html' link text).
	# 4. Change the `http' link text into `html'
	cat common.html | \
		sed -e 's%</title>%</title><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-2">%' | \
		sed 's%<hr><p><em>This file has been generated by%%' | \
		sed 's%<a href="http://www.lri.fr/~filliatr/bibtex2html/">bibtex2html</a> 1.75</em></p>%%g' | \
		sed 's%>.html<%>html<%' | \
		sed 's%>http<%>html<%' > /tmp/$$
	mv /tmp/$$ common.html
	cat common_bib.html | \
		sed -e 's#</title>#</title><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-2">#' | \
		sed -e 's%<hr><p><em>This file has been generated by%%g' | \
		sed -e 's%<a href="http://www.lri.fr/~filliatr/bibtex2html/">bibtex2html</a> 1.75</em></p>%%g' > /tmp/$$
	mv /tmp/$$ common_bib.html

# other useful targets

# MarkM: Stopped cleaning *.html
clean:
	rm -r -f *.out *.log *.aux *.dvi *.ps *.pdf *.toc *.gz $(DOCUMENT) *.lot *.bak *.mtc* *.lof out *.bbl *.blg *.bbl *~ .*~ Figures/dia/*~ Figures/Fig/*.bak $(DOCUMENT) $(FIGURES_DIA_EPS) $(FIGURES_DIA_PDF) $(FIGURES_EPS_PDF) $(FIGURES_FIG_EPS) $(FIGURES_FIG_PDF) *.idx *.ilg *.ind 

install_bib: common.html
	scp common.html common_bib.html kosik@altair.dcs.elf.stuba.sk:/home/kosik/public_html

install: $(DOCUMENT).pdf
	scp -r $(DOCUMENT).pdf kosik@altair.dcs.elf.stuba.sk:~/public_html

altair: $(DOCUMENT).pdf
	scp $(DOCUMENT).pdf kosik@altair.dcs.elf.stuba.sk:~/public_html

# NOTE: not a general-purpose target
backup:
	bin/backup.sh $(DOCUMENT)

# Auxiliary target. It creates a tar.gz archive of the document sources

tmp_tgz:
	rm -r -f /tmp/$(DOCUMENT) && mkdir /tmp/$(DOCUMENT) && cp -r . /tmp/$(DOCUMENT) && cd /tmp/$(DOCUMENT) && grep -v '^%' $(DOCUMENT).tex > tmp.tex && mv tmp.tex $(DOCUMENT).tex && make clean && cd /tmp && tar chvzf $(DOCUMENT).tar.gz $(DOCUMENT)
