 # $Id$
 #
 # This file is part of the RHexLib Robot Control Software
 #
 #  The contents of this file are subject to the RHexLib Public
 # License Version 2.0 (the "License"); you may not use this file
 # except in compliance with the License. To obtain a copy of the
 # License, please contact <ulucs+rhexlib@cs.cmu.edu>.
 #
 #  Software distributed under the License is distributed on an "AS
 # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 # implied. See the License for the specific language governing
 # rights and limitations under the License.
 #
 #  The Original Code is RHexLib Robot Control Software.
 #
 #  The Initial Developer of the Original Code is
 #    The Regents of the University of Michigan.
 #
 #  Portions created by 
 #    Regents of the University of Michigan
 # are Copyright (C) 2003.  All Rights Reserved.
 #
 #  Contributor(s): Uluc Saranli.
 #
 #  Alternatively, the contents of this file may be used under the
 # terms of the GNU Public license (the "GPL License"), in which
 # case the provisions of the GNU Public License are applicable
 # instead of those above.  If you wish to allow use of your version
 # of this file only under the terms of the GPL License and not to
 # allow others to use your version of this file under the RHexLib
 # Public License, indicate your decision by deleting the provisions
 # above and replace them with the notice and other provisions
 # required by the GPL License.  If you do not delete the provisions
 # above, a recipient may use your version of this file under either
 # the RHexLib Public License or the GPL License."
 #
 # Uluc Saranli <ulucs+rhexlib@cs.cmu.edu>

 ######################################################################
 # $Id$
 #
 # The Makefile for the Module Manager
 #
 # Created       : Uluc Saranli, 11/16/2000
 # Last Modified : Uluc Saranli, 06/27/2001
 #
 ######################################################################

# First, check whether we can access the RHexLib directory.
ifndef RHEX_DIR
$(error RHEX_DIR environment variable is not defined!)
endif

# Check for other required environment variables
include $(RHEX_DIR)/tools/checkenv.mk
include $(RHEX_DIR)/tools/tooldefs.mk

DIRNAME     = RHexLib
VERSION     = 1.0
PACKAGENAME = RHexLib_core
#BINDIR      = examples examples/quickstart examples/esp examples/exprunner
BINDIR      = examples 
DOCDIR      = doc
LIBDIR      = lib

LIBS = 	libutils/libutils.a base/libbase.a libclient/libclient.a 

### Derived parameters

CLEAN_LIST = $(LIBS:.a=.clean)

CLEANOBJ_LIST = $(LIBS:.a=.cleanobj)

FILES_LIST = $(LIBS:.a=.dir)

DEPEND_LIST = $(LIBS:.a=.dep)

include $(RHEX_DIR)/tools/flagdefs.mk

### Standard targets.

.PHONY : default libs libdir exec $(BINDIR) all modeBuilder
.PHONY : clean $(CLEAN_LIST) cleanobj $(CLEANOBJ_LIST) 
.PHONY : wc tarball depend dep bindepend 
.PHONY : $(BINDIR) $(LIBS) libutils libclient clienttools

default : libs modeBuilder

libs : include/utils libutils/Makefile include/client $(LIBS)

clienttools : libs
	@$(MAKE) -C libclient tools

libdir :
	@if test ! -d $(LIBDIR); then \
		echo "Creating lib directory"; \
		mkdir -p $(LIBDIR); \
		chmod 775 $(LIBDIR); \
	fi

libutils/Makefile:
	@$(CD) libutils; CXX='$(CXX) $(COMPFLAGS)' ./configure

all : libs modeBuilder exec 

exec :  $(BINDIR)

$(BINDIR) : 
	@$(ECHO) Compiling $@ ...
	@$(MAKE) -C $@

$(LIBS): libdir
	@$(ECHO) Compiling $(@D) ...
	@$(MAKE) --no-print-directory -C $(@D) $(@F)
	@$(LN) ../$(@) $(LIBDIR)

clean : libutils/Makefile $(CLEAN_LIST)
	@for dir in $(BINDIR); do \
	  $(ECHO) Cleaning $$dir...; \
	  $(MAKE) --no-print-directory -C $$dir clean; \
	done
	@$(ECHO) Cleaning $(DOCDIR)...
	@$(MAKE) --no-print-directory -C $(DOCDIR) realclean
	@$(ECHO) Cleaning libutils...
	@$(MAKE) --no-print-directory -C libutils clean
	@$(ECHO) Cleaning tools...
	@$(MAKE) --no-print-directory -C tools clean
	@$(RM) -f lib/*.a
	@$(RM) -f include/utils
	@$(RM) -f include/client
	@$(ECHO) Cleaning clienttools...
	@$(MAKE) --no-print-directory -C libclient/tools clean

$(CLEAN_LIST): FORCE
	@$(ECHO) Cleaning $(@D)...
	@$(MAKE) --no-print-directory -C $(@D) clean

cleanobj : $(CLEANOBJ_LIST)
	@for dir in $(BINDIR); do \
	  @$(MAKE) --no-print-directory -C $$dir cleanobj; \
	done

$(CLEANOBJ_LIST): FORCE
	@$(MAKE) -C $(@D) cleanobj

wc : 
	@$(WC) -l `find . -name Makefile` `find . -name '*.cc'` `find . -name '*.hh'` | $(SORT) -r -n

tarball : clean
	@$(RM) -f ../$(PACKAGENAME)-$(VERSION).tar.gz
	find . -name Makefile.dep | xargs $(RM) -f
	find . -name Makefile.dep.bak | xargs $(RM) -f
	@$(CD) ..; find $(DIRNAME) -name CVS > cvsfiles.list
	$(CD) ..; $(TAR) cvf $(PACKAGENAME)-$(VERSION).tar $(DIRNAME) -Xcvsfiles.list; $(GZIP) $(PACKAGENAME)-$(VERSION).tar
	$(CD) ..; $(RM) -f cvsfiles.list

depend dep : include/utils include/client $(DEPEND_LIST) bindepend

include/utils: libutils/Makefile
	@$(ECHO) Installing libutils includes ...
	@$(MAKE) -C libutils incinstall

include/client: 
	@if test ! -L $(RHEX_DIR)/include/client; then \
		rm -rf $(RHEX_DIR)/include/client; \
		echo "Linking $(RHEX_DIR)/include/client"; \
		ln -s $(RHEX_DIR)/libclient/include/client $(RHEX_DIR)/include; \
	fi; 

modeBuilder: 
	@$(ECHO) Compiling modeBuilder ...
	@$(MAKE) -C tools modeBuilder

$(DEPEND_LIST) : FORCE
	@$(MAKE) -C $(@D) depend

bindepend:
	for dir in $(BINDIR); do \
	  $(MAKE) -C $$dir depend; \
	done

FORCE :
