# Some basic recursive rules for building, installing, and cleaning up
# a source tree
#
subdirs:
	@sub="$(SUBDIRS)"; \
	for i in $$sub; do if test -n "$$i"; then \
	      (cd $$i; make); \
	fi; done;

clean:  localclean
	@sub="$(SUBDIRS)"; \
	for i in $$sub; do if test -n "$$i"; then \
	      (cd $$i; make $@); \
	fi; done;

localclean:
	$(RM) $(DIRT)

clobber: localclean
	@sub="$(SUBDIRS)";						      \
	for i in $$sub; do if test -n "$$i"; then			      \
	      (cd $$i; make $@);					      \
	fi; done;
	$(RM) $(TARGETS) $(GENERATED)

install: default localinstall
	@sub="$(SUBDIRS)";						      \
	for i in $$sub; do if test -n "$$i"; then			      \
	      (cd $$i; make $@);					      \
	fi; done;

incdepend: .dependtime
depend: .dependtime

.dependtime: $(SOURCES)
	touch temp
	$(MKDEPEND) $(MKDEPC++FLAGS) -f temp $(C++FILES)
	touch $(MKDEPFILE)
	$(MKDEPEND) $(MKDEPCFLAGS) -f $(MKDEPFILE) $(CFILES)
	cat temp >> $(MKDEPFILE)
	$(RM) temp
	touch $@

# Rules for TeX files
#
# The line that runs LATEX needs to run inside a subshell to turn off
# "make"'s status checking. It blows away .aux because the .aux file
# created is bad and also breaks bibtex. It touches .stamp to force a
# rebuild.
.tex.dvi:
	@if test -r "$*.aux";						      \
	   then cp "$(BUILDD)$*.aux" "$(BUILDD)$*.auxprev";		      \
	   else echo > "$(BUILDD)$*.auxprev";				      \
	fi
	@echo "$(LATEX) $<"
	@sh -c "$(LATEX) $< || ($(RM) $*.aux; sleep 1; touch \"$(BUILDD)$*.stamp\"; exit 1)"
	@-cmp -s "$(BUILDD)$*.aux" "$(BUILDD)$*.auxprev";		      \
	if test $$? = 1;						      \
	then								      \
	    sleep 1;							      \
	    echo "==> Rebuilding $@";					      \
	    touch "$(BUILDD)$*.stamp";					      \
	    $(MAKE) $@;							      \
	else								      \
	    echo "==> No need to rebuild $@";				      \
	fi
	@$(RM) "$(BUILDD)$*.auxprev"

.tex.pdf:
	@if test -r "$(BUILDD)$*.aux";					      \
	   then cp "$(BUILDD)$*.aux" "$(BUILDD)$*.auxprev";		      \
	   else echo > "$(BUILDD)$*.auxprev";				      \
	fi
	@echo "$(PDFLATEX) $<"
	@sh -c "$(PDFLATEX) $< || ($(RM) $*.aux; sleep 1; touch \"$(BUILDD)$*.stamp\"; exit 1)"
	@-cmp -s "$(BUILDD)$*.aux" "$(BUILDD)$*.auxprev";		      \
	if test $$? = 1;						      \
	then								      \
	    sleep 1;							      \
	    echo "==> Rebuilding $@";					      \
	    touch "$(BUILDD)$*.stamp";					      \
	    $(MAKE) $@;							      \
	else								      \
	    echo "==> No need to rebuild $@";				      \
	fi
	@$(RM) "$(BUILDD)$*.auxprev"

# must add dependencies like
#    foo.ind: foo.stamp
#    foo.dvi: foo.ind
# to use this rule properly.
.tex.ind:
	@if [ -f $*.idx ]; then						      \
	   echo makeindex $*.idx;					      \
	   makeindex $*.idx;						      \
	else								      \
	   echo touch $@;						      \
	   touch $@;							      \
        fi

# must add dependencies like
#    foo.bbl: foo.bib
#    foo.dvi: foo.bbl
# to use this rule properly.
.tex.bbl:
	@if [ -r $*.aux ]; then echo $(BIBTEX) $*; sh -c "$(BIBTEX) $* || echo"; else echo; fi

.tex.stamp: # backward compatibility
	touch $@

.dvi.ps:
	$(DVIPS) $(DVIPSOPTS) $< -o $@
.eps.pdf:
	epstopdf $(EPSTOPDFOPTS) $<

#
# Rules for C++ and .i files
#

.C.i:
	$(C++F) -E $< > $@

.c.i:
	$(CCF) -E $< > $@

.C.o:
	$(C++F) -c $<

# include $(M3SU)    # old Modula-3 rules

# Paper versions
markup:
	@DOC=markup MAKEFLAGS=e $(MAKE)
draft:
	@DOC=draft MAKEFLAGS=e $(MAKE)
tr:
	@DOC=tr MAKEFLAGS=e $(MAKE)
trdraft:
	@DOC=trdraft MAKEFLAGS=e $(MAKE)
local:
	@DOC=local MAKEFLAGS=e $(MAKE)
submission:
	@DOC=submission MAKEFLAGS=e $(MAKE)
final:
	@DOC=final MAKEFLAGS=e $(MAKE)
finaldraft:
	@DOC=finaldraft MAKEFLAGS=e $(MAKE)
blindtr:
	@DOC=blindtr MAKEFLAGS=e $(MAKE)
web:
	@DOC=web MAKEFLAGS=e $(MAKE)
