##############################################################################
# Assume that whoever is including us defined $(TOP) to be a relative path
# from that directory to the top of the build tree

# Top of the source tree
TOPSRC = $(srcdir)/$(TOP)

##############################################################################
# Common definitions for Makefiles
#

RM = rm -f
ECHO = echo
TOUCH = touch

##############################################################################
# Variables passed down via makes

CUR = .

##############################################################################
# Cleaning

DIRT = *.o core *.bak .depend.bak *.i *.[ib]lg doc.dvi doc.ps \
       $(GENERATED) $(LDIRT)
LDIRT =

CLOBBER = Makefile .depend $(LCLOBBER)
LCLOBBER =

##############################################################################
# Default rule checks for reconfiguration and then runs builds "all"

reconfig: Makefile $(TOP)/config/Makedefs
	@if test -r .depend; then				\
		$(MAKE) CUR=$(CUR) -f Makefile -f .depend all;	\
	else							\
		$(MAKE) CUR=$(CUR) -f Makefile all;		\
	fi

all: subdirs

##############################################################################
# Rules

subdirs:
	@sub="$(SUBDIRS)";				\
	for d in $$sub; do if test -d $$d; then		\
		(cd $$d; $(MAKE) CUR=$(CUR)/$$d);	\
	else $(ECHO) "WARNING: subdirectory $$d is absent."; fi; done
	@$(ECHO) making in $(CUR)

clobber:
	@sub="$(SUBDIRS)";					\
	for d in $$sub; do if test -d $$d; then			\
		(cd $$d; $(MAKE) CUR=$(CUR)/$$d clobber);	\
	else $(ECHO) "WARNING: subdirectory $$d is absent."; fi; done
	@$(ECHO) clobbering in $(CUR)
	$(RM) $(TARGETS) $(DIRT) $(CLOBBER)

clean:
	@sub="$(SUBDIRS)";				\
	for d in $$sub; do if test -d $$d; then		\
		(cd $$d; $(MAKE) CUR=$(CUR)/$$d clean);	\
	else $(ECHO) "WARNING: subdirectory $$d is absent."; fi; done
	@$(ECHO) cleaning in $(CUR)
	$(RM) $(DIRT)

doc:
	grind -o doc.dvi -style ttquot -two-column -para $(DOCFILES)
	dvips doc.dvi -o doc.ps

preview:
	grind -o tmp.dvi -style ttquot -one-column -para $(DOCFILES)
	xdvi tmp.dvi
	$(RM) tmp.dvi

##############################################################################
# Rules for automatic reconfiguration

Makefile: $(srcdir)/Makefile.in $(TOP)/config.status
	cd $(TOP); config.status

$(TOP)/config/config.h: $(TOPSRC)/config/config.h.in
	$(RM) $(TOP)/config/config.h
	cd $(TOP); $(MAKE) config.status
	cd $(TOP); config.status

$(TOP)/config/Makedefs: $(TOPSRC)/config/Makedefs.in $(TOP)/config.status
	cd $(TOP); config.status

$(TOP)/config.status: $(TOPSRC)/configure
	cd $(TOP); $(MAKE) config.status
