#                                                   -*- makefile -*-
# Time-stamp: <Tue Aug 28 2001 15:55:14 Stardate: [-30]7163.10 hwloidl>
# $Id: Makefile,v 1.1.1.1 2001/08/07 16:47:11 hwloidl Exp $
#	
# Makefile for PFP-Journal paper.
# This is a generic Makefile for running LaTeX.
# You should only have to set the name of the .ps file you want to produce.
#
# Make targets:
#  all      ... build the entire paper
#  show     ... buidl and show the paper via ghostview
#  chk-cvs  ... check CVS status of all .tex files 
#  count    ... count the number of words in all .tex files
#  update   ... do a CVS update
#  prgs     ... type-set all programs in discussion/ (not in src/)
#  clean    ... delete all intermediate files used by LaTeX
#
# Structure of this directory:
#  ./ contains the top-level and general .tex files
#  discussion/ contains tex files discussing individual programs (1 dir per prg)
#  results/ contains the results for all prgs
#  src/ contains the prg code for all prgs as used for the measurements
#  bin/ contains aux tools used for the paper (measurement, pretty printing)
#
#########################################################################

# ---------------------------------------------------------------------------
# User tunable vars
# ---------------------------------------------------------------------------

# example programs for the refereed papers
main_TARGET = paper-ref.ps paper-foo.ps paper-bar.ps paper-08.ps
# example programs for the draft proceedings
#main_TARGET = paper-draft.ps bar-sfp.ps 08.ps

# ---------------------------------------------------------------------------
# Don't change anything beyond this line
# ---------------------------------------------------------------------------

# Default setting for the name of the .ps file to produce
ifeq "$(main_TARGET)" ""
main_TARGET =   main.ps
endif

# Default command settings
LATEX =		latex
BIBTEX = 	bibtex
MAKEINDEX =     makeindex
DVIPS =		dvips
FIG2DEV = 	fig2dev
GV =            ghostview
PRETTY_PRINT =  ./bin/hs2ps
DETEX =         detex
AWK =           awk
WC =            wc

# all eps figures (generated from xfig)
#main_FIG =      $(shell ls *.xfig)
main_EPS =      $(subst xfig,eps,$(shell ls discussion/*/*.xfig))
# all bibtex files
main_BIB =      $(shell ls *.bib)
main_BBL =      $(subst ps,bbl,$(main_TARGET))
# all source files
main_SRCS =     $(shell ls *.tex)
# all prg files; that actually might include much more than used in the text!!
# src_dirs =      $(shell find . \( -name src -a -type d \) -print)
main_PRG =      $(subst hs,ps,$(shell ls discussion/*/src/*.hs)) \
	        $(subst sml,ps,$(shell ls discussion/*/src/*.sml)) \
                $(subst hs,ps,$(shell ls discussion/src/*.hs)) \
	        $(subst sml,ps,$(shell ls discussion/src/*.sml))


main_DVI =      $(subst ps,dvi,$(main_TARGET))
main_LOG =      $(subst ps,log,$(main_TARGET))
main_AUX =      $(subst ps,aux,$(main_TARGET))

# where to search for scripts, additionally to the existing path
#export PATH=$(shell "${PATH}:./bin")
#export ENSCRIPT_LIBRARY = "share/enscript"

.PHONY: all force prgs show set-env chk-cvs count clean


# Targets

all::
	@echo "Srcs: |$(main_SRCS)|; Prgs: |$(main_PRG)|; Figs: |$(main_EPS)|"

all:: set-env

all:: $(main_TARGET)

# force:
# 	$(LATEX) $(subst .ps,,$(main_TARGET))
# 	$(LATEX) $(subst .ps,,$(main_TARGET))
# 	$(BIBTEX) $(subst .ps,,$(main_TARGET))
# 	$(LATEX) $(subst .ps,,$(main_TARGET))
# 	$(LATEX) $(subst .ps,,$(main_TARGET))

# force: $(main_TARGET) $(main_EPS)

show:: $(main_TARGET)
	$(GV) -portrait -magstep 0 $<

# pretty print prg code
%.ps:	%.hs
	$(PRETTY_PRINT) -s 0.8 $^

%.ps:	%.ml
	$(PRETTY_PRINT) -m -s 0.8 $^

%.ps:	%.sml
	$(PRETTY_PRINT) -m -s 0.8 $^

chk-cvs:
	@for i in $(main_SRCS) ; do cvs status $$i | egrep "Status|Sticky Tag" ; done

count: 
	@echo "Total number of words: "
	@cat $(subst ps,tex,$(main_TARGET)) | \
        sed '/\\begin{document}/,/\\end{document}/!d' | \
        $(DETEX) | $(WC) -w 

update:
	cvs update -d .

set-env::
	@export ENSCRIPT="StatesConfigFile: share/enscript/enscript.st"

prgs:: set-env
	@find . -name "*.hs" -exec touch \{\} \;
	@find . -name "*.sml" -exec touch \{\} \;
	@echo "ENSCRIPT is |${ENSCRIPT}|"
	@echo "PATH is |${PATH}|"

prgs:: $(main_PRG)

%.ps:	%.dvi
	$(DVIPS) -o $@ $<

%.dvi: %.bbl

%.dvi %.aux %.toc %.idx: %.tex count
	$(LATEX) $<
	if [ -n "$(shell grep 'undefined references' $(main_LOG))" ]; then $(LATEX) $< ; fi
	if [ -n "$(shell grep 'undefined references' $(main_LOG))" ]; then $(LATEX) $< ; fi
	if [ -n "$(shell grep changed $(main_LOG))" ]; then $(LATEX) $< ; fi

%.bbl: %.tex
	$(BIBTEX) $(subst .tex,,$<)

%.ind:	%.idx
	$(MAKEINDEX) $<

%.eps:	%.xfig
	$(FIG2DEV) -L eps $< $@

# dependencies
$(main_DVI):	$(main_PRG) $(main_SRCS) $(main_EPS) $(main_BBL)
$(main_BBL):    $(main_AUX)

clean: 
	-rm $(main_TARGET) $(subst .ps,.dvi,$(main_TARGET)) *.aux *.log *.bbl *.eps
