#
#  Makefile for the Harvest source tree
# 
#  Makefile,v 1.62 1995/10/06 07:23:23 duane Exp
#
#  --------------------------------------------------------------------
#  
#  You can change the default installation by changing the prefix line.
#
#  The software will install into $prefix/{bin,lib,man}.  Do NOT use a 
#  common directory (like /usr/local) as the prefix, since Harvest will 
#  install about 75 (small) programs.   For example, to install Harvest
#  into a home directory:
#
#	prefix = /home/joe/harvest
#
prefix = /usr/local/harvest

SUBDIRS	= common gatherer broker ../components

all:	ckconfigure
	@echo Making all in $(SUBDIRS)
	@for dir in $(SUBDIRS); do \
		echo Making $@ in $$dir; \
		(cd $$dir; $(MAKE) $(MFLAGS) prefix="$(prefix)" $@); \
	done
	@echo "Done building the core Harvest software."

reconfigure:	rmconfig ckconfigure

install:	install-mkdir install-harvest-bin
	@for dir in $(SUBDIRS); do \
		echo Making $@ in $$dir; \
		(cd $$dir; $(MAKE) $(MFLAGS) prefix="$(prefix)" $@); \
	done
	/bin/sh make-symlinks.sh "$(prefix)"

clean:
	@for dir in $(SUBDIRS); do \
		echo Making $@ in $$dir; \
		(cd $$dir; $(MAKE) $(MFLAGS) prefix="$(prefix)" $@); \
	done

realclean:
	-rm -f config.status config.log config.cache
	@for dir in $(SUBDIRS); do \
		echo Making $@ in $$dir; \
		(cd $$dir; $(MAKE) $(MFLAGS) prefix="$(prefix)" $@); \
	done


#  ----------------------------------------------------------------------
#  Support for building and installing single components
#  
#  - Verifies that ./configure has been run; otherwise, runs it.
#  - Builds the software for that component.
#  - Installs the software for that component.
#  

#  gatherer is our Harvest Gatherer
gatherer:	do-gatherer

do-gatherer:	ckconfigure
	@for dir in common gatherer ../components/gatherer; do \
		if test -d "$$dir"; then \
			echo Making all in $$dir; \
			(cd $$dir; $(MAKE) $(MFLAGS) all); \
		fi; \
	done

install-gatherer:	install-mkdir
	@for dir in common gatherer ../components/gatherer; do \
		if test -d "$$dir"; then \
			echo Making install in $$dir; \
			(cd $$dir; $(MAKE) $(MFLAGS) install); \
		fi; \
	done

#  broker is our Harvest Broker
broker:	do-broker

do-broker:	ckconfigure
	@for dir in common broker ../components/broker; do \
		if test -d "$$dir"; then \
			echo Making all in $$dir; \
			(cd $$dir; $(MAKE) $(MFLAGS) all); \
		fi; \
	done

install-broker:	install-mkdir
	@for dir in common broker ../components/broker; do \
		if test -d "$$dir"; then \
			echo Making install in $$dir; \
			(cd $$dir; $(MAKE) $(MFLAGS) install); \
		fi; \
	done

#  replicator is the Harvest Replicator (floodd and mirrord)
replicator:	do-replicator

do-replicator:	ckconfigure
	@for dir in replicator ../components/replicator; do \
		if test -d "$$dir"; then \
			echo Making all in $$dir; \
			(cd $$dir; $(MAKE) $(MFLAGS) all); \
		fi; \
	done

install-replicator:	install-mkdir
	@for dir in replicator ../components/replicator; do \
		if test -d "$$dir"; then \
			echo Making install in $$dir; \
			(cd $$dir; $(MAKE) $(MFLAGS) install); \
		fi; \
	done

# binary is the binary-only distribution, same as all/install
binary:	do-binary

do-binary:	all

install-binary:	install

# install the fluffy Harvest binaries
install-harvest-bin:
	cp Harvest $(prefix)/bin/Harvest
	chmod 755 $(prefix)/bin/Harvest
	cp RunHarvest $(prefix)/RunHarvest
	chmod 755 $(prefix)/RunHarvest

#  ckconfigure runs the configure command
ckconfigure:
	@if test ! -r ./config.status; then \
		echo ./configure --prefix="$(prefix)" --srcdir="`pwd`"; \
		./configure --prefix="$(prefix)" --srcdir="`pwd`"; \
		chmod 755 ./Harvest; \
		chmod 755 ./RunHarvest; \
	else \
		echo "Configuration is up-to-date.  Continuing build..."; \
	fi;

#  rmconfig deletes the previous configuration output
rmconfig:
	-rm -f config.status config.cache config.log

#  install-mkdir will verify that the install directories work
install-mkdir:
	@for dir in $(prefix)/bin $(prefix)/lib $(prefix)/lib/gatherer $(prefix)/lib/broker $(prefix)/cgi-bin; do \
		if test ! -d $$dir; then \
			echo mkdir $$dir; \
			mkdir $$dir; \
			echo chmod a+x $$dir; \
			chmod a+x $$dir; \
		fi \
	done
