#
# Makefile for NWS. 
#
# $Id: Makefile,v 1.75 2004/12/06 20:29:25 graziano Exp $

# common variables
include Makedefs

# define the various subdirs we'll use
SUBDIRS		=	Common \
			Include \
			Doc \
			Sensor/ExpCPU \
                        Sensor/ExpDisk \
			Sensor/ExpConnect \
			Sensor/ExpMemory \
			Sensor/ExpTcpBw \
			Sensor/ExpFS \
                        Sensor/ExpAvail \
			Sensor/ExpMemorySpeed \
			Sensor/ExpStart \
			Sensor \
			JavaNwsProtocol
SDKDIRS		= 	Include JavaNwsProtocol
SERVERDIRS	= 	Server Server/Nwslapd
CLIENTDIR	=	Interface


# we create libnws.a here
LIBRARY		=	$(buildlibdir)/$(LIBNWSNAME)

# stuff related to create a tarball
DIST_FILES	=	ChangeLog \
			Makedefs.in \
			Makefile \
			README \
			VERSION \
			config.guess \
			config.sub \
			filelist \
			filelist.in \
			pkg_data_src.gpt \
			install-sh \
			configure

# these are needed to rebuild the old libnws.a
PORTABILITY_OBJS	=	$(buildobjdir)/diagnostic.o \
				$(buildobjdir)/dnsutil.o \
				$(buildobjdir)/formatutil.o \
				$(buildobjdir)/osutil.o \
				$(buildobjdir)/messages.o \
				$(buildobjdir)/strutil.o \
				$(buildobjdir)/timeouts.o \
				$(buildobjdir)/protocol.o
FORECAST_OBJS		=	$(buildobjdir)/exp_smooth.o \
				$(buildobjdir)/fbuff.o \
				$(buildobjdir)/forc.o \
				$(buildobjdir)/nws_forecast_api.o \
				$(buildobjdir)/last_value.o \
				$(buildobjdir)/median.o \
				$(buildobjdir)/mse_forc.o \
				$(buildobjdir)/run_mean.o
NWS_OBJS		=	$(buildobjdir)/nws_api.o \
				$(buildobjdir)/clique_protocol.o\
				$(buildobjdir)/register.o \
				$(buildobjdir)/host_protocol.o \
				$(buildobjdir)/periodic.o \
				$(buildobjdir)/experiments.o \
				$(buildobjdir)/skills.o \
				$(buildobjdir)/sensor.o

.PHONY: all common clients servers help
help:
	@echo "Possible targets for NWS:"
	@echo "   all                  - build everything"
	@echo "   install              - install everything"
	@echo "   clients              - build the clients (contains sensors)"
	@echo "   servers              - build the servers"
	@echo "   install-clients      - install the clients"
	@echo "   install-servers      - install the servers"
	@echo "   install-sdk          - install include file and static libraries"
	@echo "   clean                - remove objects files"
	@echo "   distclean            - remove build directory and configuration"
	@echo "   dist                 - creates a tar file of nws"

all: common clients servers $(LIBRARY)

# the common target: we need to build these for both clients and servers
common: precondition portability 
	@for subdir in $(SUBDIRS); do \
		(cd $$subdir && $(MAKE) all) || exit $$? ; done

# the target for building the clients
clients: common $(LIBRARY)
	@for subdir in $(CLIENTDIR); do \
		(cd $$subdir && $(MAKE) all) || exit $$? ; done

# the target for building the servers
servers: common $(LIBRARY)
	@for subdir in $(SERVERDIRS); do \
		(cd $$subdir && $(MAKE) all) || exit $$? ; done

# this is a temporary cheat: we'll fix when the libraries will be better
# defined. We recreate here the old libnws.a
$(PORTABILITY_OBJS): portability
$(FORECAST_OBJS): portability

$(NWS_OBJS): common

$(LIBRARY): $(SENSORS_OBJS) $(PORTABILITY_OBJS) $(FORECAST_OBJS) $(NWS_OBJS)
	$(AR) cr $(LIBRARY) $(NWS_OBJS) $(SENSORS_OBJS) $(PORTABILITY_OBJS) \
			$(FORECAST_OBJS)
	$(RANLIB) $(LIBRARY)

.PHONY: install install-common install-servers install-clients 
install: install-servers install-sdk install-clients

install-common: common
	@test -w $(prefix) || $(INSTALL) -d $(prefix)
	@test -w $(exec_prefix) || $(INSTALL) -d $(exec_prefix)
	@test -w $(bindir) || $(INSTALL) -d $(bindir)
	@test -w $(libdir) || $(INSTALL) -d $(libdir)
	@test -w $(incdir) || $(INSTALL) -d $(incdir)
	@test -w $(mandir)/man1 || $(INSTALL) -d $(mandir)/man1
	@test -w $(mandir)/man3 || $(INSTALL) -d $(mandir)/man3
	@test -w $(mandir)/man7 || $(INSTALL) -d $(mandir)/man7

install-servers: servers install-common
	@for subdir in $(SERVERDIRS); do \
		(cd $$subdir && $(MAKE) install) || exit $$? ; done

install-clients: clients install-common
	@for subdir in $(SUBDIRS); do \
		(cd $$subdir && $(MAKE) install) || exit $$? ; done
	@for subdir in $(CLIENTDIR); do \
		(cd $$subdir && $(MAKE) install) || exit $$? ; done

install-sdk: common
	@test -w $(libdir) || $(INSTALL) -d $(libdir)
	@test -w $(incdir) || $(INSTALL) -d $(incdir)
	@for subdir in $(SDKDIRS); do \
		(cd $$subdir && $(MAKE) install) || exit $$? ; done
	@$(CP) $(LIBRARY) $(libdir)

.PHONY: clean distclean dist tags 
clean: 
	@for subdir in $(SUBDIRS); do \
		(cd $$subdir && $(MAKE) $@) || exit $$? ; done
	@for subdir in $(CLIENTDIR); do \
		(cd $$subdir && $(MAKE) $@) || exit $$? ; done
	@for subdir in $(SERVERDIRS); do \
		(cd $$subdir && $(MAKE) $@) || exit $$? ; done
	@(cd $(PORTABILITY) && $(MAKE) $@) || exit $$?
	@$(RM) -f $(LIBRARY)

distclean: clean
	@(cd $(PORTABILITY) && $(MAKE) $@) || exit $$?
	@echo "Removing build directories ..."
	@$(RM) -fr build* $(DIST_NAME) $(DIST_NAME).md5sum
	@echo "Removing configuration files ..."
	@$(RM) -f Include/config_nws.h config.cache config.log config.status Makedefs tags
	@$(RM) -rf $(DIST_ROOT) $(DIST_NAME)

tags:
	$(CTAGS) -R 

dist: 
	@$(RM) -rf $(DIST_ROOT) $(DIST_NAME)
	@$(INSTALL) -d $(DIST_ROOT)
	@$(CP) $(DIST_FILES) $(DIST_ROOT)
	@for subdir in $(SUBDIRS); do \
		(cd $$subdir && $(MAKE) dist) || exit $$? ; done
	@for subdir in $(CLIENTDIR); do \
		(cd $$subdir && $(MAKE) dist) || exit $$? ; done
	@for subdir in $(SERVERDIRS); do \
		(cd $$subdir && $(MAKE) dist) || exit $$? ; done
	@(cd $(PORTABILITY); $(MAKE) $@) || exit $$?
	@(cd $(DIST_ROOT); $(TAR) -xf $(TOP)/$(PORTABILITY)/$(PORTABILITY).tar)
	@$(TAR) -cf $(DIST_NAME) $(DIST_DIR) || exit $$?
	@if test -x "`which md5sum`"; then md5sum $(DIST_NAME) > $(DIST_NAME).md5sum; fi
	@$(RM) -rf $(DIST_ROOT)

.PHONY: precondition portability 
portability: 
	@(cd $(PORTABILITY); $(MAKE) all) || exit $$? 

precondition: 
	@$(RM) -f compile.log
	@if test `./config.guess` != "$(configured)"; then \
		echo "NWS was configured for $(configured)!!"; \
		echo "you may want to run make clean and reconfigure for `./config.guess`!";\
		sleep 5; fi
	@$(INSTALL) -d $(buildobjdir)
	@$(INSTALL) -d $(buildlibdir)
	@$(INSTALL) -d $(buildincdir)
	@$(INSTALL) -d $(buildbindir)

Makedefs: Makedefs.in config.status
	./config.status

config.status: configure
	@if test ! -x ./config.status; then \
		echo "you have to run ./configure!"; exit 1; fi
	./config.status --recheck

