ifneq ("$(VERBOSE)", "YES")
	MAKE := $(MAKE) -s VERBOSE=NO
endif

BASEDIR      = $(shell pwd)
BASEINCLUDES = $(BASEDIR)/include
BASEBIN      = $(BASEDIR)/bin
BASELIB      = $(BASEDIR)/lib
HEADERS      = *.h *.i

SRCONLY_DIRS = $(BASEDIR)/cdevClient\
               $(BASEDIR)/cdevContextMap\
               $(BASEDIR)/cdevMonitorTable\
               $(BASEDIR)/cdevPacket\
               $(BASEDIR)/cdevServer\
               $(BASEDIR)/cdevTagMap\
               $(BASEDIR)/common\

LIB_DIRS     = $(BASEDIR)/cdevReactor\
               $(BASEDIR)/NameServer/src\
	       $(BASEDIR)/NameServer/cdev\
	       $(BASEDIR)/lib
	       
UTIL_DIRS    = $(BASEDIR)/NameServer/dumpServer

EXAMPLE_DIRS = $(BASEDIR)/NameServer/examples\
               $(BASEDIR)/example

TEST_DIRS    = $(BASEDIR)/tests\
               $(BASEDIR)/cdevReactor/TimerTest\
	       $(BASEDIR)/cdevReactor/ServerTest

DOC_DIRS     = $(BASEDIR)/doc\

HEADER_DIRS  = $(BASEDIR)/cdevReactor\
               $(BASEDIR)/cdevTagMap\
               $(BASEDIR)/cdevContextMap\
               $(BASEDIR)/cdevMonitorTable\
               $(BASEDIR)/cdevPacket\
	       $(BASEDIR)/common\
	       $(BASEDIR)/cdevClient\
	       $(BASEDIR)/cdevServer\
	       $(BASEDIR)/NameServer/src\
	       $(BASEDIR)/NameServer/cdev 	       

CLEAN_DIRS   = $(SRCONLY_DIRS)\
	       $(LIB_DIRS)\
               $(UTIL_DIRS)\
	       $(EXAMPLE_DIRS)\
	       $(TEST_DIRS)\
	       $(DOC_DIRS)

PURGE_DIRS   = $(SRCONLY_DIRS)\
               $(BASEDIR)/cdevReactor\
               $(BASEDIR)/NameServer/src\
	       $(BASEDIR)/NameServer/cdev\
	       $(BASEDIR)/liteClient\
               $(UTIL_DIRS)\
	       $(EXAMPLE_DIRS)\
	       $(TEST_DIRS)\
	       $(DOC_DIRS)

all: directories headers archive shared utils
			
directories: 
	@echo "=> Creating directories for binary files"
	@mkdir -p $(BASEBIN)
	@mkdir -p $(BASELIB)
	@echo "<= Done...\n"
	
headers:
	@echo "=> Linking header files to include directory"
	@for dir in $(HEADER_DIRS); \
	do \
		cd $$dir; \
		for file in $(HEADERS); \
		do \
			if test -f $$file; then cp $$dir/$$file $(BASEINCLUDES)/$$file; fi; \
		done \
	done
	@cp $(BASEDIR)/include/makeinclude/Makefile.lib $(BASEDIR)/lib/Makefile
	@echo "<= Done...\n"
	
archive:
	@echo "=> Building archive version of libraries"
	@for dir in $(LIB_DIRS); \
	do \
		$(MAKE) -C $$dir SHOBJ=NO; \
	done
	@echo "<= Done...\n"
	
shared:
	@echo "=> Building shared version of libraries"
	@for dir in $(LIB_DIRS); \
	do \
		$(MAKE) -C $$dir SHOBJ=YES; \
	done
	@echo "<= Done...\n"
	
utils:
	@echo "=> Building utility programs"
	@for dir in $(UTIL_DIRS); \
	do \
		$(MAKE) -C $$dir SHOBJ=YES; \
	done
	@echo "<= Done...\n"
	 
examples:
	@echo "=> Building example programs"
	@for dir in $(EXAMPLE_DIRS); \
	do \
		$(MAKE) -C $$dir SHOBJ=YES; \
	done
	@echo "<= Done...\n"

apps:
	@echo "=> Building test programs"
	@for dir in $(TEST_DIRS); \
	do \
		$(MAKE) -C $$dir SHOBJ=YES; \
	done
	@echo "<= Done...\n"
		
clean:
	@echo "=> Cleaning CDEV Generic Server Directories"
	@for dir in $(CLEAN_DIRS); \
	do \
		$(MAKE) -C $$dir clean; \
	done
	@echo "<= Done...\n"
	
purge:
	@echo "=> Purging CDEV Generic Server Directories"
	@echo "   => Removing links to header files"
	@rm -rf $(BASEINCLUDES)/*.h $(BASEINCLUDES)/*.i
	@echo "      Done..."
	@for dir in $(PURGE_DIRS); \
	do \
		$(MAKE) -C $$dir purge; \
	done
	@rm -rf $(BASEDIR)/lib
	@rm -rf $(BASEDIR)/bin
	@echo "<= Done...\n"
