#
#  Makefile for the Harvest source tree
# 
#  Makefile,v 1.4.4.3 1995/12/19 06:54:45 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

LIBDIRS	= lib regex url # GNUmalloc
SUBDIRS	= server scripts announce

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

reconfigure:	rmconfig ckconfigure

install:	install-mkdir install-cached-conf
	@for dir in $(LIBDIRS) $(SUBDIRS); do \
		echo Making $@ in $$dir; \
		(cd $$dir; $(MAKE) $(MFLAGS) prefix="$(prefix)" $@); \
	done

install-cached-conf:
	cp server/cached.conf $(prefix)/lib/cached.conf.default
	chmod 644 $(prefix)/lib/cached.conf.default
	@if test ! -f $(prefix)/lib/cached.conf ; then \
		cp server/cached.conf $(prefix)/lib/cached.conf; \
		chmod 644 $(prefix)/lib/cached.conf; \
	fi


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

realclean:
	-rm -f config.status config.cache config.log
	@for dir in $(LIBDIRS) $(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.
#  

#  cache is the Harvest Cache and the cachemanager
cache:	do-cache

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

install-cache:	install-mkdir install-cached-conf
	@for dir in common cache; do \
		if test -d "$$dir"; then \
			echo Making install in $$dir; \
			(cd $$dir; $(MAKE) $(MFLAGS) install); \
		fi; \
	done

#  ckconfigure runs the configure command
ckconfigure:
	@if test ! -r ./config.status; then \
		echo ./configure --prefix="$(prefix)" --srcdir="`pwd`"; \
		./configure --prefix="$(prefix)" --srcdir="`pwd`"; \
	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)/cgi-bin; do \
		if test ! -d $$dir; then \
			echo mkdir $$dir; \
			mkdir $$dir; \
		fi \
	done
	@if test -d $(prefix)/lib/cache; then \
		echo mv $(prefix)/lib/cache $(prefix)/lib/cache-NOT-USED; \
		mv $(prefix)/lib/cache $(prefix)/lib/cache-NOT-USED; \
	fi


mk-libdir:
	@test -d lib || mkdir lib
