# Makefile for automatic latex generation
# Chan Bernard Ki Hong (2003)

# This is a crude makefile. Messy and don't learn by reading it.

######################################################################
# Document-specific prefixes
######################################################################
name = perltut

######################################################################
# Path prefixes and filenames
######################################################################
prefix = 					# Should be empty (deprecated)
images_prefix = images/		# Where to find images
pdfviewer = acroread
#pdfviewer = kghostview
dvi = $(name).dvi
ps = $(name).ps
pdf = $(name).pdf
######################################################################

png = $(wildcard $(strip $(images_prefix))*.png)
eps = $(patsubst %.png,%.eps,$(png)) # $(wildcard $(strip $(images_prefix))*.eps)
tex = $(wildcard *.tex)

aux = $(wildcard *.aux *.out)

objects = $(dvi) # $(eps)		# objects to be dynamically generated

all : $(objects) $(ps) $(pdf)

view : $(objects) $(ps) $(pdf)
	$(pdfviewer) $(pdf) >/dev/null 2>&1 &

$(ps) : $(objects)
	dvips $(dvi)

$(pdf) : $(ps)
	ps2pdf -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -dPDFSETTINGS=/printer $(ps)

$(eps): $(png)				# generate the eps'es from the png's
							# BUGS TODO: need to make into a loop
	convert $? $(patsubst %.png,%.eps,$?)

$(dvi) : $(tex) # $(eps)			# generate the latex DVI file
	latex $(name)			# May need to run latex two times to update references and sectioning
	latex $(name)
	makeindex $(name).idx
	latex $(name)

debug :						# For debugging of makefiles only. Don't use.
	echo $(aux)

clean :	
	-rm $(ps) $(pdf) $(aux) $(dvi) $(name).log
