#############################################################
# Makefile to latex files                                   #
# Thanks to http://www.light.utoronto.ca/stok/linux.html    #
#                                                           #
# $Source: /home/cvsd/var/lib/cvs/fiaif/fiaif/doc/fiaif/Makefile,v $  #
# $Revision: 1.12 $                                          #
# $Author: afu $                                            #
#############################################################

COMPILER = latex
.SUFFIXES: .dia .fig .eps .latex .gnuplot .pdf .pstex .ps .tex .bbl .ind .aux
.PHONY: xdvi pdf ps gv print dvi spell clean view html dvihelper conffiles

TARGET = fiaif

TEXFILES = preface introduction layout iptables \
	   main rules firewall conclusion related conffiles usage \
	   configuration 

FIGS = fig/zones

BIBFILE = main

conffiles: 
	rm -fr conf
	mkdir conf
	cp ../../conf/private_networks conf/private-networks
	cp ../../conf/reserved_networks conf/reserved-networks
	cp ../../conf/type_of_services conf/type-of-services


$(TARGET).dvi: psheader.tex $(addsuffix .eps, $(FIGS)) \
	       $(addsuffix .tex, $(TEXFILES)) ${MISC_FILES} $(BIBFILE).bib \
		conffiles
	cat psheader.tex main.tex > $(TARGET).tex 
	make dvihelper
	rm -fr conf


$(TARGET).pdf: pdfheader.tex $(addsuffix .eps, $(FIGS)) \
	       $(addsuffix .tex, $(TEXFILES))  ${MISC_FILES} $(BIBFILE).bib \
		conffiles
	cat pdfheader.tex main.tex > $(TARGET).tex 
	make dvihelper
	dvipdft -e -p a5 -r 1200 -o $(TARGET).pdf $(TARGET).dvi

dvihelper: $(addsuffix .eps, $(FIGS)) $(addsuffix .tex, $(TEXFILES)) \
	   ${MISC_FILES} fiaif.tex
	${COMPILER} $(TARGET).tex
	bibtex $(TARGET)
	makeindex $(TARGET)
	${COMPILER} $(TARGET).tex 
	if grep "Rerun" $(TARGET).log > /dev/null || \
	   grep "undefined references" $(TARGET).log; then \
		${COMPILER} $(TARGET).tex; \
	fi


html: $(TARGET).dvi
	latex2html -split +2 -scalable_fonts -info "" \
                   -local_icons -html_version 4.0,math \
                   -dir $(HTML_DIR) $(TARGET).tex > /dev/null

xdvi: $(TARGET).dvi
	xdvi $(TARGET)&

pdf: $(TARGET).pdf
	acroread $(TARGET).pdf & 

ps: $(TARGET).ps

print: $(TARGET).dvi
	dvips -f $(TARGET) | psbook | pstops "2:0L(1h,0)+1L(1h,.5h)" |lpr

view: gv

gv: $(TARGET).ps
	gv $(TARGET).ps

dvi: $(TARGET).dvi


.tex.aux: $(basename $<).tex
	${COMPILER} $<

.bib.bbl: $(basename $<).aux
	bibtex $<

.idx.ind: $(basename $<)
	makeindex $<

.dvi.ps: 
	dvips $< -o $@

.fig.latex:
	fig2dev -L latex $< $@

.fig.eps:
	fig2dev -L eps $< $@

.dia.eps:
	dia  --nosplash --export=$@ $< 

.gnuplot.latex:
	gnuplot $<

.gnuplot.eps:
	gnuplot $<

.eps.pdf:
	epstopdf $<

.fig.pstex:
	fig2dev -L pstex $< $@; \
	fig2dev -L pstex_t -p $(notdir $(basename $<)).pstex $< $(notdir $(basename $@)).pstex_t

.gnuplot.pstex:
	gnuplot $< ; \
	mv $(notdir $(basename $<)).ps $(notdir $(basename $<)).pstex ; \
	sed -e "s/psfile=\(.*\)ps/psfile=\1pstex/" \
		$(notdir $(basename $<)).pstex_t > temp.pstex_t ; \
	mv temp.pstex_t $(notdir $(basename $<)).pstex_t

.pstex.pdf:
	epstopdf $<; \
	sed -e "s/\\special{psfile=\(.*\)pstex/\\pdfimage{\1pdf/" \
		$(notdir $(basename $<)).pstex_t > temp.pstex_t;\
	mv temp.pstex_t $(notdir $(basename $<)).pstex_t

.gnuplot.pdf:
	gnuplot $< ; \
	epstopdf $(notdir $(basename $<)).ps ; \
	grep "psfile=" $(notdir $(basename $<)).pstex_t | \
	sed -e '1i\' -e '\\begin{picture}(0,0)' \
		-e '1a\' -e '\\end{picture}' \
		-e "s/\\special{psfile=\(.*\)ps.*}/\\pdfimage{\1pdf}/" \
		> temp1 ;\
	sed -e "/psfile=/d"  $(notdir $(basename $<)).pstex_t > temp2 ;\
	cat temp1 temp2 > $(notdir $(basename $<)).pstex_t ;\
	rm temp1 temp2

.PHONY: clean

clean:
	rm -f *.dvi *.log *.bak *.aux *.bbl *.blg *.ps *.pdf *.latex \
	*.pstex *.pstex_t *.toc fiaif.ps *.idx *.ind *.ilg
	rm -fr conf
	find . -name \*~ | xargs rm -f
	find . -name \*.eps | xargs rm -f
