 #
 # 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$
 ######################################################################

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

ifndef RHEX_HARDWARE
$(error RHEX_HARDWARE environment variable is not defined!)
endif

export RHEX_TOPDIR   = $(shell pwd)

# Define environment variables required by other Makefiles
export RHEX_DIR      = $(LIBS_TOPDIR)/RHexLib
export ARACHI_DIR    = $(LIBS_TOPDIR)/arachi
export SIMLIB_DIR    = $(LIBS_TOPDIR)/SimLib
export SIMSECT_DIR   = $(RHEX_TOPDIR)/Simulation/SimSect++

# Define directories for various components
BEHAVIORS_DIR = $(RHEX_TOPDIR)/RobotCode/Behaviors
HARDWARE_DIR  = $(RHEX_TOPDIR)/RobotCode/Hardware
OCU_DIR       = $(RHEX_TOPDIR)/OCUCode
SWRI_DIR      = $(RHEX_TOPDIR)/SWRI

RHEXGUI_DIR   = $(OCU_DIR)/RHexGUI
LOGGUI_DIR    = $(OCU_DIR)/LogGUI
SWRILOG_DIR   = $(OCU_DIR)/SWRILogger
CLI_DIR       = $(OCU_DIR)/CLI

MODULES_DIR   = $(BEHAVIORS_DIR)/modules
MODES_DIR     = $(BEHAVIORS_DIR)/modes

SUPERVISOR_DIR = $(SWRI_DIR)/Supervisor
OPERATOR_DIR   = $(SWRI_DIR)/Operator

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

ifeq ($(RHEX_HARDWARE), _MCRHIO_)
HARDWARE_LIBS = CommonHW ResearchHW
HARDWARE_CLASS_DIR = ResearchHW
endif

ifeq ($(RHEX_HARDWARE), _RUGGED_)
HARDWARE_LIBS = CommonHW RuggedHW
HARDWARE_CLASS_DIR = RuggedHW
endif

ifeq ($(RHEX_HARDWARE), _AQUA_)
HARDWARE_LIBS = CommonHW AquaHW
HARDWARE_CLASS_DIR = AquaHW
endif

ifeq ($(RHEX_HARDWARE), _SIMSECT_)
HARDWARE_LIBS = CommonHW SimSectHW
HARDWARE_CLASS_DIR = SimSectHW 
endif

ifeq ($(RHEX_HARDWARE), _ARACHI_)
HARDWARE_LIBS = CommonHW RHexSimHW
HARDWARE_CLASS_DIR = RHexSimHW 
endif

ifeq ($(RHEX_HARDWARE), _DUMMY_)
HARDWARE_LIBS = CommonHW DummyHW
HARDWARE_CLASS_DIR = DummyHW
endif

# Define some tools to be used
MAKE        = make
LN          = ln -sf
STRIP       = wstrip
RM          = rm
SED         = sed
MV          = mv
CP          = cp
MKDIR       = mkdir
CD          = cd
WC          = wc
TAR         = tar
GZIP        = gzip
SORT        = sort
ECHO        = echo
FIND        = find
MODEBUILDER = $(RHEX_DIR)/util/modeBuilder

# Directories to be created
TOPLIBDIR     = lib
TOPINCLUDEDIR = include
TOPROBOTDIR   = Supervisor
TOPOCUDIR     = Operator

ALLTOPDIRS = $(TOPLIBDIR) $(TOPINCLUDEDIR) $(TOPROBOTDIR) $(TOPOCUDIR)

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

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

.PHONY : all libs robinstall $(HARDWARE_LIBS)
.PHONY : robotexec supervisor operatorexec operator

all : allexec

ifeq ($(UNAME), QNX)
allexec : robotexec
roblibs : modulelib modelib
robinstall : modeinstall moduleinstall
endif

ifeq ($(UNAME), Linux)
allexec : models robotexec operatorexec
roblibs : simsect modulelib modelib
robinstall : simsectinstall modeinstall moduleinstall
endif

models :
	@$(MAKE) -C $(SWRI_DIR)/Config/models

hwinstall : $(HARDWARE_LIBS)
	@$(LN) $(HARDWARE_DIR)/include/hardware include

$(HARDWARE_LIBS):
	@$(MAKE) -C $(HARDWARE_DIR)/$(@)
	@$(LN) $(HARDWARE_DIR)/$(@)/*.a lib

robotexec : robotlinks supervisor

robotlinks : topdirs 
	@$(LN) $(SUPERVISOR_DIR)/supervisor $(TOPROBOTDIR)
	@$(LN) $(SUPERVISOR_DIR)/start.sh $(TOPROBOTDIR)
	@$(LN) $(SUPERVISOR_DIR)/SimSect.rc $(TOPROBOTDIR)
	@$(LN) $(SUPERVISOR_DIR)/hexaped.oogl $(TOPROBOTDIR)
	@$(LN) $(SWRI_DIR)/Config $(TOPROBOTDIR)
	@$(LN) $(SWRI_DIR)/Config/models/*.ocdl $(TOPROBOTDIR)

supervisor : robinstall hwinstall 
	@$(MAKE) -C $(SUPERVISOR_DIR)

operatorexec : operator operatorlinks uitools 

operatorlinks : topdirs
	@$(LN) $(OPERATOR_DIR)/*.cfg $(TOPOCUDIR)
	@$(LN) $(OPERATOR_DIR)/*.acc $(TOPOCUDIR)
	@$(LN) $(OPERATOR_DIR)/start.sh $(TOPOCUDIR)
	@$(LN) $(RHEXGUI_DIR)/rhex_gui $(TOPOCUDIR)
	@$(LN) $(LOGGUI_DIR)/log_gui $(TOPOCUDIR)
	@$(LN) $(SWRILOG_DIR)/exp_logger $(TOPOCUDIR)/swri_logger
	@$(LN) $(SWRILOG_DIR)/start_logger.sh $(TOPOCUDIR)
	@$(LN) $(SWRILOG_DIR)/stop_logger.sh $(TOPOCUDIR)
	@$(LN) $(CLI_DIR)/log_data $(TOPOCUDIR)
	@$(LN) $(CLI_DIR)/log_query $(TOPOCUDIR)
	@$(LN) $(CLI_DIR)/repair_mlab $(TOPOCUDIR)

operator : robinstall
	@$(MAKE) -C $(OPERATOR_DIR)

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 : simsectinstall moduleinstall modeinstall

simsectinstall : topdirs simsect
	@$(LN) $(SIMSECT_DIR)/lib/*.a lib
	$(RM) -f include/simsect
	@$(LN) $(SIMSECT_DIR)/include include/simsect

moduleinstall : topdirs modulelib
	@$(LN) $(MODULES_DIR)/*.a lib
	$(RM) -f include/behaviors
	@$(LN) $(BEHAVIORS_DIR)/include include/behaviors

modeinstall : topdirs modelib
	@$(LN) $(MODES_DIR)/*.a lib

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

.PHONY : simsect modulelib modelib

simsect :
	@$(MAKE) -C $(SIMSECT_DIR)

modulelib :
	@$(MAKE) -C $(BEHAVIORS_DIR)/modules

modelib :
	@$(MAKE) -C $(BEHAVIORS_DIR)/modes

# ----------------------------
# --- Tools and executables

.PHONY : uitools rhexgui loggui swrilogger cliui

ifeq ($(UNAME), QNX)
uitools : 
endif

ifeq ($(UNAME), Linux)
uitools : rhexgui loggui swrilogger cliui
endif

rhexgui : robinstall
	@$(MAKE) -C $(RHEXGUI_DIR)

loggui : robinstall
	@$(MAKE) -C $(LOGGUI_DIR)

swrilogger : robinstall
	@$(MAKE) -C $(SWRILOG_DIR)

cliui : robinstall
	@$(MAKE) -C $(CLI_DIR)


# ----------------------------
# --- Packaging

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

# Directories for cleaning various libraries
ifeq ($(UNAME), QNX)
LIB_DIR_LIST = \
	$(RHEX_TOPDIR)/RobotCode/Behaviors/modes \
	$(RHEX_TOPDIR)/RobotCode/Behaviors/modules \
	$(RHEX_TOPDIR)/RobotCode/Hardware/CommonHW \
	$(RHEX_TOPDIR)/RobotCode/Hardware/$(HARDWARE_CLASS_DIR)
endif

ifeq ($(UNAME), Linux)
LIB_DIR_LIST = \
	$(RHEX_TOPDIR)/Simulation/SimSect++ \
	$(RHEX_TOPDIR)/RobotCode/Behaviors/modes \
	$(RHEX_TOPDIR)/RobotCode/Behaviors/modules \
	$(RHEX_TOPDIR)/RobotCode/Hardware/CommonHW \
	$(RHEX_TOPDIR)/RobotCode/Hardware/SimSectHW \
	$(RHEX_TOPDIR)/RobotCode/Hardware/RHexSimHW
endif

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

ifeq ($(UNAME), QNX)
UITOOL_LIB_LIST = 
endif

ifeq ($(UNAME), Linux)
UITOOL_DIR_LIST = \
	$(RHEX_TOPDIR)/OCUCode/CLI \
	$(RHEX_TOPDIR)/OCUCode/LogGUI \
	$(RHEX_TOPDIR)/OCUCode/RHexGUI \
	$(RHEX_TOPDIR)/OCUCode/SWRILogger
endif

CLEAN_TOOL_LIST = $(patsubst %,%/clean,$(UITOOL_DIR_LIST))

.PHONY : clean libclean uitoolclean $(CLEAN_LIB_LIST) $(CLEAN_TOOL_LIST)

ifeq ($(UNAME), QNX)
clean : libclean uitoolclean supervisorclean
endif

ifeq ($(UNAME), Linux)
clean : libclean uitoolclean supervisorclean operatorclean
endif

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

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

uitoolclean : $(CLEAN_TOOL_LIST)

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

supervisorclean :
	@$(ECHO) Cleaning directory $(SUPERVISOR_DIR)...
	@$(MAKE) --no-print-directory -C $(SUPERVISOR_DIR) clean

operatorclean :
	@$(ECHO) Cleaning directory $(OPERATOR_DIR)...
	@$(MAKE) --no-print-directory -C $(OPERATOR_DIR) clean
