ALL: adi3new.ps tcpadi3.ps adi3new.pdf 
SHELL = /bin/sh

# Set MPICH2 to the location of the MPICH source tree.  If this is
# built from within the MPICH2 distribution, then a relative path of 
# ../.. may be used.  This *must* be a relative path.
MPICH2 = ../..

#
# Use doctext in /home/gropp/bin/$ARCH/doctext 
DOCTEXT = doctext
# Give the directory with the Latex style files associated with doctext
DOCTEXTSTYLE = /home/gropp/data/share

.SUFFIXES: .ps .pdf .dvi .tex .eps .fig .idx .ind

adi3.dvi: adi3.tex
paper.dvi: paper.tex

.dvi.ps:
	dvips $*
.dvi.pdf:
	(unset TEXMF ; dvipdfm $* )
.tex.dvi:
	-latex $* </dev/null 2>&1 >/dev/null
	-bibtex $*
	-latex $* </dev/null 2>&1 >/dev/null
	-latex $*

.idx.ind:
	rm -f tmp.idx tmp.ind
	sed -e 's/{\\tt *\\char *`\\_ *}/_/g' \
	    -e 's/MPID_\([A-Za-z0-9]*\)/\1_MPID/g' \
	    -e 's/MPIU_\([A-Za-z0-9]*\)/\1_MPIU/g' \
		$< > tmp.idx
	makeindex tmp
	sed -e 's/\([A-Za-z0-9]*\)_MPID/MPID_\1/g' \
	    -e 's/\([A-Za-z0-9]*\)_MPIU/MPIU_\1/g' \
	    -e 's/_/\\texttt{\\char`\\_}/g' tmp.ind > $*.ind
	rm -f tmp.idx tmp.ind

ADI3FILES = adi3man.tex adi3.c adi3.h Makefile

# This list of files gets us the old versions first and the new
# versions last, so that the newest versions will override the old versions
# Eventually, the old versions will be removed.
ADI3IMPLFILES = adi3.c adi3.h $(MPICH2)/src/include/mpiimpl.h 
adi3man/MPID_Init.tex: $(ADI3IMPLFILES) Makefile
	-if [ ! -d adi3man ] ; then mkdir adi3man ; fi
	-rm -f adi3man/*.tex adi3man/tmp.*
	for file in $(ADI3IMPLFILES) ; do \
	( cd adi3man ; \
	${DOCTEXT} -latex -nolocation -quotefmt ../$$file ; \
	perl ../manextract ../$$file ) ; done

refman.sty: $(DOCTEXTSTYLE)/refman.sty
	cp $(DOCTEXTSTYLE)/refman.sty .
#	cp $(DOCTEXTSTYLE)/tpage.sty .

adi3man.dvi: adi3man/MPID_Init.c $(ADI3FILES) refman.sty
	${MAKE} lists
	-cp adi3man.tex adi3man/adi3man.tex
	(cd adi3man ;
	latex adi3man < /dev/null 2>&1 >/dev/null ;\
	bibtex adi3man ;\
	make -f ../Makefile adi3man.ind ; \
	latex adi3man < /dev/null )
	mv adi3man/adi3man.dvi .

# This isn't ready yet.  We'd like to use doctext to generate the 
# HTML for the 
adi3man/adi3man.htm: adi3man.tex adi3.c adi3.h Makefile
	-rm -rf adi3man
	tohtml -default adi3man.tex
	tohtml -default adi3man.tex

#adi3new/adi3new.htm: adi3new.tex adi3.c adi3.h Makefile
#	-rm -rf adi3new
#	tohtml -default adi3new.tex
#	tohtml -default adi3new.tex

ADI3NEWFILES = adi3new.tex adi3.c adi3.h adi-ov.eps blockdiag.eps layers.eps mpiddir.eps Makefile adi3man/MPID_Init.tex refman.sty
adi3new.ps: $(ADI3NEWFILES)
	touch adi3new.ind
	latex adi3new < /dev/null 2>&1 >/dev/null
	-bibtex adi3new
	make adi3new.ind 
	latex adi3new < /dev/null
	dvips adi3new

adi3new.pdf: $(ADI3NEWFILES) adi3man/MPID_Init.tex
	touch adi3new.ind
	rm -f adi3newpdf.tex
	sed -e 's/documentclass{article}/documentclass[dvipdfm]{article}/' \
	    -e 's/%hyperref .*/\\usepackage[dvipdfm]{hyperref}/' \
	    -e '/usepackage{.*url}/d' \
		adi3new.tex > adi3newpdf.tex
	latex adi3newpdf < /dev/null 2>&1 >/dev/null
	-bibtex adi3newpdf
	make adi3newpdf.ind
	latex adi3newpdf < /dev/null
	dvipdfm adi3newpdf
	rm -f adi3new.pdf
	mv -f adi3newpdf.pdf adi3new.pdf

# This isn't ready yet.  We'd like to use doctext to generate the 
# HTML for the adi document
adi3new/adi3new.htm: adi3new.tex adi3.c adi3.h Makefile
	tohtml -default -dosnl adi3new.tex
	tohtml -default -dosnl adi3new.tex

core.dvi: core.tex adi3.c adi3.h
	touch core-list.ind
	-latex core </dev/null 2>&1 >/dev/null
	-latex core

tcpadi3.pdf: tcpadi3.tex
	rm -f tcpadi3pdf.tex
	sed -e 's/documentclass{article}/documentclass[dvipdfm]{article}/' \
	    -e 's/%hyperref .*/\\usepackage[dvipdfm]{hyperref}/' \
		tcpadi3.tex > tcpadi3pdf.tex
	make tcpadi3pdf.dvi
	dvipdfm tcpadi3pdf
	-mv tcpadi3pdf.pdf tcpadi3.pdf
	rm -f tcpadi3pdf.tex tcpadi3pdf.dvi 

# This uses the full-featured date program in Linux
WEBDIR = /mcs/www-unix/mpi/mpich/adi3
install-web: adi3new.ps adi3new.pdf adi3new/adi3new.htm tcpadi3.ps tcpadi3.pdf
	-rm -f ${WEBDIR}/adi3.[ch]
	cp adi3.[ch] ${WEBDIR}
	cp adi3new.ps ${WEBDIR}/adi3man.ps
	cp adi3new.pdf ${WEBDIR}/adi3man.pdf
	cp tcpadi3.ps ${WEBDIR}/tcpadi3.ps
	cp tcpadi3.pdf ${WEBDIR}/tcpadi3.pdf
	rm -rf ${WEBDIR}/adi3man
	cp -r adi3new ${WEBDIR}/adi3man
	mv ${WEBDIR}/adi3man/adi3new.htm ${WEBDIR}/adi3man/adi3man.htm
	cp ${WEBDIR}/index.html .foo
	sed "s/Last updated .*$$/Last updated on `date '+%B %e, %Y'`./" \
		.foo > ${WEBDIR}/index.html
	rm -f .foo
install-web-old: adi3man.ps adi3man.pdf
	-rm -f ${WEBDIR}/adi3.[ch]
	cp adi3.[ch] ${WEBDIR}
	cp adi3man.ps ${WEBDIR}
	cp adi3man.pdf ${WEBDIR}
	cp ${WEBDIR}/index.html .foo
	sed "s/Last updated .*$$/Last updated on `date '+%B %e, %Y'`./" \
		.foo > ${WEBDIR}/index.html
	rm -f .foo

clean:
	rm -f *.log *.aux *.dvi adi3.ps *.bbl paper.ps findmod adi3man.ps
	rm -f *.ilg adi3man.ind adi3new.ind *.blg *.toc *.out 
	rm -f adi3newpdf.idx adi3newpdf.ind adi3new.idx 
distclean: clean
	rm -f adi3man/*.tex
	rm -f adi3new.ps tcpadi3.ps

#
# The following are helpers for assembling the reference manual
findmod: adi3.c adi3.h Makefile
	rm -f .findmod
	@(cd adi3man ; \
	for file in *.tex ; do \
	if [ $$file = "adi3man.tex" ] ; then continue ; fi ; \
	awk '{if (pnext == 1) { printf("%s",$$0); pnext=2; exit;} };\
	/\{Module\}/ {pnext = 1;}; \
	END{if (pnext != 2) printf("None");}' $$file | \
	sed 's/{.*\\_}/_/g' >> ../.findmod ;\
	echo " $$file" >> ../.findmod;\
	done )
	sort .findmod > findmod
	rm -f .findmod
lists: findmod
	cd adi3man ; \
	awk 'BEGIN{module=""};{if ($$1 != module) { \
	file=$$1 "-list.tex"; module=$$1; };\
	print "\\input " $$2>>file;}' ../findmod

# Handle older fig2dev programs
.fig.eps:
	-fig2dev -L eps -l landscape $*.fig > $*.eps
	if [ ! -s $*.eps ] ; then \
		fig2dev -L ps -l landscape $*.fig > $*.eps ; fi

goals.dvi: goals.tex adi-ov.eps
