 #
 # This file is part of RHexLib, 
 #
 # Copyright (c) 2001 The University of Michigan, its Regents,
 # Fellows, Employees and Agents. All rights reserved, and distributed as
 # free software under the following license.
 # 
 #  Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions are
 # met:
 # 
 # 1) Redistributions of source code must retain the above copyright
 # notice, this list of conditions, the following disclaimer and the
 # file called "CREDITS" which accompanies this distribution.
 # 
 # 2) Redistributions in binary form must reproduce the above copyright
 # notice, this list of conditions, the following disclaimer and the file
 # called "CREDITS" which accompanies this distribution in the
 # documentation and/or other materials provided with the distribution.
 # 
 # 3) Neither the name of the University of Michigan, Ann Arbor or the
 # names of its contributors may be used to endorse or promote products
 # derived from this software without specific prior written permission.
 # 
 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 ######################################################################
 # $Id$
 ######################################################################

# Use the current directory as the base for the libraries
export LIBS_HOME = $(shell pwd)

# Define environment variables required by other Makefiles
export RHEX_DIR      = $(LIBS_HOME)/RHexLib
export FLTK_DIR      = $(LIBS_HOME)
export LIBRAW_DIR    = $(LIBS_HOME)
export LIBDC_DIR     = $(LIBS_HOME)
export QHULL_DIR     = $(LIBS_HOME)

# Define directories for various components
FLAME_DIR      = $(LIBS_HOME)/flame
FLTKLIB_DIR    = $(LIBS_HOME)/fltk
LIBRAW1394_DIR = $(LIBS_HOME)/libraw1394
LIBDC1394_DIR  = $(LIBS_HOME)/libdc1394
QHULLLIB_DIR   = $(LIBS_HOME)/qhull/src
ARACHI_DIR     = $(LIBS_HOME)/arachi
LIBUTILS_DIR   = $(RHEX_DIR)/libutils
DMV_DIR		   = $(LIBS_HOME)/DoubMatVec
SIMLIB_DIR	   = $(LIBS_HOME)/SimLib
GUILIB_DIR         = $(LIBS_HOME)/GUILib

# Decide on the hardware to use based on the operating system
UNAME = $(shell uname)

# Define some tools to be used
MAKE        = make
LN          = ln -sf
STRIP       = wstrip
RM          = rm
MV          = mv
CP          = cp
MKDIR       = mkdir
CD          = cd
WC          = wc
ECHO        = echo
FIND        = find

# Directories for various libraries
ifeq ($(UNAME), QNX)
LIB_DIR_LIST = RHexLib
endif

ifeq ($(UNAME), Linux)
ifeq ($(RHEX_HARDWARE), _ARACHI_)
LIB_DIR_LIST = RHexLib SimLib
else
LIB_DIR_LIST = RHexLib
endif
ifeq ($(RISE_HARDWARE), _ARACHI_)
LIB_DIR_LIST = RHexLib SimLib
endif
endif

CLEAN_LIB_LIST = $(patsubst %,%/clean,$(LIB_DIR_LIST))

# Directories to be created
TOPLIBDIR     = lib
TOPINCLUDEDIR = include

ALLTOPDIRS = \
	$(TOPLIBDIR) $(TOPINCLUDEDIR)

# Include files generated by qhull install
QHULL_INCLUDES = \
	include/geom.h include/io.h include/mem.h \
	include/merge.h include/poly.h include/qhull_a.h \
	include/qhull.h include/qset.h include/stat.h \
	include/user.h

# ------------------------------------------------------------------------
# --- Targets ------------------------------------------------------------

# ----------------------------
# --- Main top level targets

.PHONY : all libs depend
.PHONY : robinstall

ifeq ($(UNAME), QNX)
all : rhexlibinstall 
endif
ifeq ($(UNAME), Linux)
ifeq ($(RHEX_HARDWARE), _ARACHI_)
all : rhexlibinstall qhullinstall flameinstall simlibinstall guilibinstall clienttools
else
all : rhexlibinstall qhullinstall flameinstall guilibinstall clienttools
endif
ifeq ($(RISE_HARDWARE), _ARACHI_)
all : rhexlibinstall qhullinstall flameinstall simlibinstall clienttools #fltkinstall flameinstall 
endif
endif

depend :
	$(FIND) . -name .deps | xargs rm -r

# ----------------------------
# --- Directory creation

.PHONY : topdirs 

topdirs : $(ALLTOPDIRS)

$(ALLTOPDIRS) :
	@if test ! -d $(@); then \
		echo "Creating $(@) directory"; \
		$(RM) -f $(@); \
		$(MKDIR) -p $(@); \
		chmod 775 $(@); \
	fi

# ----------------------------
# --- Installation targets

.PHONY : rhexlibinstall flameinstall simlibinstall dmvinstall \ #fltkinstall
	guilibinstall

rhexlibinstall : topdirs rhexlib
	@$(LN) $(RHEX_DIR)/lib/*.a lib
	@$(RM) -f include/rhexlib
	@$(LN) $(RHEX_DIR)/include include/rhexlib

simlibinstall : topdirs simlib
	@$(LN) $(SIMLIB_DIR)/lib/*.a lib
	@$(RM) -f include/cdl
	@$(RM) -f include/pcm
	@$(RM) -f include/rti
	@$(LN) $(SIMLIB_DIR)/include/cdl include/cdl
	@$(LN) $(SIMLIB_DIR)/include/pcm include/pcm
	@$(LN) $(SIMLIB_DIR)/include/rti include/rti

dmvinstall : topdirs dmvlib
	@$(LN) $(DMV_DIR)/lib/*.a lib
	@$(RM) -f include/dmv
	@$(LN) $(DMV_DIR)/include/dmv include/dmv

flameinstall : topdirs flame
	@$(RM) -f include/flame
	@$(LN) $(FLAME_DIR)/include include/flame
	@$(LN) $(FLAME_DIR)/lib/libamulet.a lib

guilibinstall : topdirs guilib
	@$(RM) -f include/guilib
	@$(LN) $(GUILIB_DIR)/include include/guilib
	@$(LN) $(GUILIB_DIR)/libgui.a lib

fltkinstall : topdirs fltk

qhullinstall : topdirs qhull

# ----------------------------
# --- Various library targets

.PHONY : rhexlib guilib #flame fltk 

rhexlib :
	@$(ECHO) Compiling directory $(RHEX_DIR) ...
	@$(MAKE) --no-print-directory -C $(RHEX_DIR)

clienttools :
	@$(MAKE) --no-print-directory -C $(RHEX_DIR) clienttools

simlib :
	@$(ECHO) Compiling directory $(SIMLIB_DIR) ...
	@$(MAKE) --no-print-directory -C $(SIMLIB_DIR)

dmvlib :
	@$(ECHO) Compiling directory $(DMV_DIR) ...
	@$(MAKE) --no-print-directory -C $(DMV_DIR)

fltk : topdirs $(FLTKLIB_DIR)/config.h \
	lib/libfltk.a lib/libfltk_forms.a include/FL

lib/libfltk.a lib/libfltk_forms.a include/FL : 
	@$(ECHO) Compiling directory $(FLTKLIB_DIR) ...
	@$(MAKE) --no-print-directory -C $(FLTKLIB_DIR) install

$(FLTKLIB_DIR)/config.h :
	@$(CD) $(FLTKLIB_DIR); \
	./configure --enable-threads --enable-xdbe --prefix=$(FLTK_DIR)

libraw1394 : topdirs $(LIBRAW1394_DIR)/config.h \
	lib/libraw1394.a include/libraw1394

lib/libraw1394.a  include/libraw1394: 
	@$(ECHO) Compiling directory $(LIBRAW1394_DIR) ...
	@$(MAKE) --no-print-directory -C $(LIBRAW1394_DIR) install

$(LIBRAW1394_DIR)/config.h :
	@$(CD) $(LIBRAW1394_DIR); \
	export CFLAGS=-I$(LIBS_HOME)/include; \
	export LDFLAGS=-L$(LIBS_HOME)/lib; \
	./configure --silent --prefix=$(LIBRAW_DIR)

libdc1394 : topdirs libraw1394 $(LIBDC1394_DIR)/config.h \
	lib/libdc1394_control.a include/libdc1394

lib/libdc1394_control.a  include/libdc1394: 
	@$(ECHO) Compiling directory $(LIBDC1394_DIR) ...
	@$(MAKE) --no-print-directory -C $(LIBDC1394_DIR) install

$(LIBDC1394_DIR)/config.h : 
	@$(CD) $(LIBDC1394_DIR); \
	export CFLAGS=-I$(LIBS_HOME)/include; \
	export LIBS=-L$(LIBS_HOME)/lib; \
	./configure --silent --prefix=$(LIBDC_DIR)

flame : topdirs lib/libamulet.a

lib/libamulet.a :
	@$(ECHO) Compiling directory $(FLAME_DIR) ...
	@$(CD) $(FLAME_DIR)/bin; \
	AMULET_DIR=$(FLAME_DIR) AMULET_VARS_FILE=Makefile.vars.gcc.linux-ELF \
	$(MAKE) --no-print-directory 

guilib:  
	@$(ECHO) Compiling directory $(GUILIB_DIR) ...
	@$(MAKE) --no-print-directory -C $(GUILIB_DIR)

qhull : topdirs lib/libqhull.a

lib/libqhull.a $(QHULL_INCLUDES) :
	@$(ECHO) Compiling directory $(QHULLLIB_DIR) ...
	@$(MAKE) --no-print-directory -C $(QHULLLIB_DIR) installlib

# ------------------------------------------------------------------------
# --- Clean targets 

.PHONY : clean libclean $(CLEAN_LIB_LIST)

clean : libclean 

libclean : $(CLEAN_LIB_LIST)
	@$(RM) -rf $(TOPLIBDIR)
	@$(RM) -rf $(TOPINCLUDEDIR)

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

flameclean :
	@$(RM) -rf include/flame
	@$(RM) -rf lib/libamulet.a
	@$(CD) $(FLAME_DIR)/bin; \
	AMULET_DIR=$(FLAME_DIR) AMULET_VARS_FILE=Makefile.vars.gcc.linux-ELF \
	$(MAKE) clean

fltkclean :
	@$(MAKE) -C $(FLTKLIB_DIR) clean
	@$(RM) -rf lib/libfltk.a
	@$(RM) -rf lib/libfltk_forms.a
	@$(RM) -rf include/FL

qhullclean :
	@$(MAKE) -C $(QHULLLIB_DIR) cleanall
	@$(RM) -rf $(QHULL_INCLUDES)
	@$(RM) -rf lib/libqhull.a
