# --------------------------------------------------------------------------- #
# Dave Richerby's wonderful LaTeX makefile                                    #
# --------------------------------------------------------------------------- #

# This makefile is in the public domain.  Do whatever you want with it
# but be aware that it is provided ``as is'' without any express or
# implied warranty.
#
# Please mail comments or suggestions to davidr@chiark.greenend.org.uk
#
# Examples of usage:
#   ``make foo.ps'':    compile foo.tex to PostScript
#   ``make foo'':       compile foo.tex to PostScript and view it
#   ``make foo.ps.gz'': compile foo.tex to PostScript and compress it
#
# Warning: don't cut and paste this file from your browser as it will
# probably convert all the tabs to spaces.  The make program needs
# them to be tabs.
#
# Changes:
#
# 2000-10-18: Added PDF output (thanks to chrisw@chiark.greenend.org.uk)
# 2000-10-12: Original version
# 2003-12-08: Nick Urbanik, for snmp-v3.

#
# Set this to your PostScript viewer
#
VIEWER=gv

#
# If you want a default action, put it here.  E.g. ``default: foo.ps''
# will cause make to produce foo.ps when run without arguments
#

DOCUMENT=snmp-v3

default: $(DOCUMENT)-4up.ps $(DOCUMENT)-8up.ps $(DOCUMENT).pdf  $(DOCUMENT)-4up.pdf $(DOCUMENT)-8up.pdf

# $(DOCUMENT)-extra-4up.ps

#
# If you have any documents which include other files, list them here, e.g.
#
#   sample.dvi: sample.tex diagram.eps
#
# If you need to continue onto a second line, end the first with a '\'
# character
#

figures = snmp-context-example.fig vacm-diagram.tex is-allowed.tex

epsfigs = $(figures:.fig=.eps) $(figures:.tex=.eps)
pdffigs = $(figures:.fig=.pdf)

$(DOCUMENT).dvi: $(epsfigs)

$(DOCUMENT).pdf: $(epsfigs)


vacm-diagram.eps: vacm-diagram.dvi
	dvips -E -o $@ $<

is-allowed.eps: is-allowed.dvi
	dvips -E -o $@ $<

# extra_pages_to_print = "32,34,35,38-42,44-47,54-56,58,59-63,78-80,84,118-120"

# --------------------------------------------------------------------------- #
# You shouldn't need to change anything below this point unless you're        #
# extending the functionality of the makefile.                                #
# --------------------------------------------------------------------------- #

clean:
	rm -f *.log *.dvi *.ps *.toc *.aux *.eepic *.bak *~ *.out

# Remove everything that can be built with make:
veryclean: clean
	rm -f *.eps *.pdf *.dep *.cfg $(DOCUMENT)-snapshot*

squeakyclean: veryclean

reallyclean: veryclean

%.eps: %.dvi
	dvips -E -o $@ $<

bundle: ${DOCUMENT}-snapshot.tar.bz2

zipbundle: ${DOCUMENT}-snapshot.zip

bundles: bundle zipbundle

${DOCUMENT}-snapshot.zip: ${DOCUMENT}.tex bundledoc-zip.cfg
	@echo
	@echo \*
	@echo \*
	snapshot=$(subst .tex,-snapshot.tex,$<)
	sed 1i'\\RequirePackage{snapshot}       % Needed by bundledoc' $< > \
	$(subst .tex,-snapshot.tex,$<)
	latex $(subst .tex,-snapshot.tex,$<)
	ls -l $(subst .tex,-snapshot.dep,$<)
	latex $(subst .tex,-snapshot.tex,$<)
	ls -l $(subst .tex,-snapshot.dep,$<)
	mv -f $@ $@.bak
	bundledoc --config=bundledoc-zip.cfg \
	$(subst .tex,-snapshot.dep,$<)
	zip -g9r $@ ${figures} Makefile ${DOCUMENT}.tex README
	zip -d $@ ${DOCUMENT}-snapshot/ict-logo-smaller.eps
	rm -f $(subst .tex,-snapshot.tex,$<)

${DOCUMENT}-snapshot.tar.bz2: ${DOCUMENT}.tex bundledoc-tar.cfg
	@echo
	@echo \*
	@echo \*
	snapshot=$(subst .tex,-snapshot.tex,$<)
	sed 1i'\\RequirePackage{snapshot}       % Needed by bundledoc' $< > \
	$(subst .tex,-snapshot.tex,$<)
	latex $(subst .tex,-snapshot.tex,$<)
	ls -l $(subst .tex,-snapshot.dep,$<)
	latex $(subst .tex,-snapshot.tex,$<)
	ls -l $(subst .tex,-snapshot.dep,$<)
	bundledoc --config=bundledoc-tar.cfg \
	$(subst .tex,-snapshot.dep,$<)
	tar -rv -f $(subst .tex,-snapshot.tar,$<) ${figures} \
	Makefile ${DOCUMENT}.tex README
	bzip2 --best --force $(subst .tex,-snapshot.tar,$<)
	rm -f $(subst .tex,-snapshot.tex,$<)

bundledoc-zip.cfg: Makefile
	( echo '# bundledoc configuration file for teTeX (kpathsea + Unix)'; \
	echo '# By Scott Pakin <pakin@uiuc.edu>'; \
	echo 'bundle: zip -9r $$BDBASE.zip $$BDINPUTS -x $$BDBASE/ict-logo-smaller.eps'; \
	echo 'sink:   > /dev/null 2>&1'; \
	echo 'find:   kpsewhich -progname=latex $$BDINPUTS' ) > $@

bundledoc-tar.cfg: Makefile
	( echo '# bundledoc configuration file for teTeX (kpathsea + Unix)'; \
	echo '# By Scott Pakin <pakin@uiuc.edu>'; \
	echo 'bundle: tar cvf $$BDBASE.tar --exclude=ict-logo-smaller.eps $$BDINPUTS'; \
	echo 'sink:   > /dev/null 2>&1'; \
	echo 'find:   kpsewhich -progname=latex $$BDINPUTS' ) > $@

%.dvi: %.tex 
	@echo
	@echo \*
	@echo \* Compiling $<
	@echo \*
	latex $<
	@while ( grep "Rerun to get cross-references"                   \
			$(subst .tex,.log,$<) >/dev/null ); do          \
		echo '** Re-running LaTeX **';                          \
		latex $<;                                               \
	done

# See http://freshmeat.net/articles/view/667/
#    1.  latex file.tex
#    2. dvips -Ppdf -G0 file.dvi -o file.ps
#    3. ps2pdf -dPDFsettings=/prepress file.ps file.pdf 

# Two points to note:

#     * The -G0 parameter passed to dvips is used to get around a
#       bug in GhostScript which converts the "f" character to a
#       pound sign in the final PDF.

#     * The -dPDFsettings parameter for ps2pdf is used to prevent
#       downsampling of EPS images when they are converted to PDF.
#       Without this switch, EPS graphics in the final PDF look
#       very fuzzy, especially when viewed with a projector. 


%.ps: %.dvi 
	@echo
	@echo \*
	@echo \* Converting $< to PostScript
	@echo \*
	dvips -Ppdf -G0 -o $@ $<

%.pdf: %.ps 
	@echo
	@echo \*
	@echo \* Converting $< to PDF
	@echo \*
	ps2pdf -dPDFsettings=/prepress $< $@

%-extra.ps: %.ps
	@echo
	@echo \*
	@echo \* Extracting extra pages from $< to $@
	@echo \*
	psselect -p$(extra_pages_to_print)  $< $@

%-4up.ps: %.ps
	@echo
	@echo \*
	@echo \* Converting $< to 4up, ready for printing
	@echo \*
	psnup -m12mm -b-2mm -pa4 -4 -l $< > $@

%-8up.ps: %.ps
	@echo
	@echo \*
	@echo \* Converting $< to 8up, ready for printing
	@echo \*
	psnup -m10mm -b-2mm -pa4 -8 -l $< > $@

lpr: $(DOCUMENT)-4up.ps	
	@echo
	@echo \*
	@echo \* Printing $<
	@echo \*
	lpr $<

# %.pdf: %.dvi
# 	@echo
# 	@echo \*
# 	@echo \* Converting $< to PDF
# 	@echo \*
# 	dvips -o - -Ppdf $< | ps2pdf - $@

%: %-4up.ps
	@echo
	@echo \*
	@echo \* Spawning viewer for $<
	@echo \*
	$(VIEWER) -landscape $<

%.tex: %.c
	@echo
	@echo \*
	@echo \* Formatting C program $< to $@
	@echo \*
	c2lt $< > $@

%.gz: %
	@echo
	@echo \*
	@echo \* Compressing $<
	@echo \*
	gzip $<

%.eps: %.obj
	@echo
	@echo \*
	@echo \* Producing Encapsulated PostScript for $<
	@echo \*
	tgif -print -eps -color $<

%.pdf: %.eps
	@echo
	@echo \*
	@echo \* Producing Encapsulated PostScript for $<
	@echo \*
	epstopdf $<

%.eps: %.fig
	@echo
	@echo \*
	@echo \* Producing $@ from $<
	@echo \*
	fig2dev -L eps $< $@

%.eepic: %.fig
	@echo
	@echo \*
	@echo \* Producing EEPIC for $<
	@echo \*
	fig2dev -L eepic $< $@

# Stop GNU make from overzealous deletion of intermediate files
.PRECIOUS: %.ps %.dvi %.eps
