#############################################################################
# user defined variables

USE_MATLAB = 0

#define where to find the newmat, netcdf and matlab libraries and headers
#NEWMAT_LIB_DIR = /usr/local/newmat
#NEWMAT_INC_DIR = /usr/local/newmat
#NEWMAT_LIB_DIR = /usr/lib
#NEWMAT_INC_DIR = /usr/include/newmat
NEWMAT_LIB_DIR = ./newmat
NEWMAT_INC_DIR = ./newmat
QNX_LIB_DIR = ./QNX-additions
QNX_INC_DIR = ./QNX-additions
NETCDF_LIB_DIR = /usr/lib
NETCDF_INC_DIR = /usr/include
LRAUV_LCM_INC_DIR = ./lrauv-lcmtypes

ifeq ($(USE_MATLAB),1)
 #MATLAB_LIB_DIR = /pathToMatlab/matlab/2009b/bin/glnxa64
 #MATLAB_INC_DIR = /pathToMatlab/matlab/2009b/extern/include
 #MATLAB_LD_DIR = /pathToMatlab/matlab/2009b/bin/glnxa64:/pathToMatlab/matlab/2009b/sys/os/glnxa64

 MATLAB_LIB_DIR = /usr/local/MATLAB/R2012a/bin/glnxa64
 MATLAB_INC_DIR = /usr/local/MATLAB/R2012a/extern/include
 MATLAB_LD_DIR = /usr/local/MATLAB/R2012a/bin/glnxa64:/usr/local/MATLAB/R2012a/sys/os/glnxa64
endif

#define compilation variables
#OUTPUT_DIR = bin
PG = -pg
# PG = 
OUTPUT_DIR = linux
CXX = g++
ifeq ($(USE_MATLAB),1)
	CFLAGS = -g -Wall $(PG) -DUSE_MATLAB
else
	CFLAGS = -g -Wall $(PG) -Wno-deprecated -Wno-reorder -Wno-unused-but-set-variable -Wno-format-security -Wno-unused-variable -Wno-sign-compare -Wno-write-strings -O0
endif

ifeq ($(USE_MATLAB),1)
 LIBS = -lnewmat -lm -lnetcdf -lrt -lglut -lGLU -leng -lmat -lmx -lGL -lpthread $(pkg-config --libs pcl_common-1.7 pcl_io-1.7 pcl_octree-1.7 pcl_surface-1.7 pcl_kdtree-1.7) /usr/lib/libgctp.so.0d
 INC_PATHS = -I$(NEWMAT_INC_DIR) -I$(NETCDF_INC_DIR) -I$(MATLAB_INC_DIR) $(pkg-config --cflags pcl_common-1.7 pcl_io-1.7 pcl_octree-1.7 pcl_surface-1.7 pcl_kdtree-1.7)
 LIB_PATHS = -L$(NEWMAT_LIB_DIR) -L$(NETCDF_LIB_DIR) -L$(MATLAB_LIB_DIR)
 LD_FLAGS = -Wl,-rpath -Wl,$(MATLAB_LIB_DIR) -Wl,-rpath -Wl,$(MATLAB_LD_DIR)
else
# Use one of the next two lines, either the replay version or the other
# for trn_replay # LIBS = -lnewmat -lm -lnetcdf -lrt -lpthread /usr/lib/libgctp.so.0d

LIBS = -lnewmat -lqnx /usr/lib/libgctp.so -lm -llcm -lnetcdf -lrt -lpthread -lconfig++


# LIBS += -lhdf5_hl -lhdf5 # for David's computer only
 #LIBS += -lglut -lGLU -lGL  # for plotting functionality only


 INC_PATHS = -I$(NEWMAT_INC_DIR) -I$(QNX_INC_DIR) -I$(LRAUV_LCM_INC_DIR) -I./ #-I$(NETCDF_INC_DIR)
 LIB_PATHS = -L$(NEWMAT_LIB_DIR) -L$(QNX_LIB_DIR) #-L$(NETCDF_LIB_DIR)
 LD_FLAGS = $(PG) 
endif


SOURCES = TerrainNav.cpp TNavConfig.cpp TNavFilter.cpp TNavPointMassFilter.cpp \
	TNavParticleFilter.cpp TNavBankFilter.cpp TerrainMapOctree.cpp \
	TerrainNavLog.cpp TNavPFLog.cpp \
	mapio.cpp structDefs.cpp trn_log.cpp myOutput.cpp matrixArrayCalcs.cpp \
	TerrainMapDEM.cpp OctreeSupport.cpp Octree.cpp OctreeNode.cpp TRNUtils.cpp \
	TerrainNavClient.cpp Replay.cpp LcmTrn.cpp TerrainNavLcmClient.cpp 
# for trn_replay #	TerrainNavClient.cpp Replay.cpp

#	ParticlePlot.cpp GlutWindow.cpp GlutWindow2d.cpp ColorHelper.cpp 
# TODO: remove plotting stuff?


PROGRAM = main

SERVER = trn_server

LRTRN_APP = lrtrn_app

TILETEST = tile_test

REPLAY = trn_replay

BODUMP = bodump

OCTREE2PATCHES = octree2Patches

CSVOCTREEMAKER = csvOctreeMaker

GRDOCTREEMAKER = grdOctreeMaker

TRANSOCT = translateOctree

LOG_TEST = test_trn_log

OBJECTS = $(SOURCES:%.cpp=$(OUTPUT_DIR)/%.o)
DEPEND = $(SOURCES:%.cpp=$(OUTPUT_DIR)/%.d)

LIB_OBJECTS = $(OUTPUT_DIR)/TerrainNav.o \
	$(OUTPUT_DIR)/PositionLog.o \
	$(OUTPUT_DIR)/TerrainNavLog.o \
	$(OUTPUT_DIR)/TNavConfig.o \
	$(OUTPUT_DIR)/TNavFilter.o \
	$(OUTPUT_DIR)/TNavPointMassFilter.o \
	$(OUTPUT_DIR)/TNavParticleFilter.o \
	$(OUTPUT_DIR)/TNavBankFilter.o \
	$(OUTPUT_DIR)/TerrainMapOctree.o \
	$(OUTPUT_DIR)/OctreeSupport.o \
	$(OUTPUT_DIR)/TerrainMapDEM.o \
	$(OUTPUT_DIR)/mapio.o \
	$(OUTPUT_DIR)/structDefs.o \
	$(OUTPUT_DIR)/trn_log.o \
	$(OUTPUT_DIR)/myOutput.o \
	$(OUTPUT_DIR)/matrixArrayCalcs.o \
	NAV_LIBRARY = $(OUTPUT_DIR)/libterrainNav.a

#	$(OUTPUT_DIR)/TerrainNavClient.o \

#############################################################################
# build commands

all: $(NAV_LIBRARY) $(OUTPUT_DIR)/$(PROGRAM) $(OUTPUT_DIR)/$(SERVER) $(OUTPUT_DIR)/$(BODUMP) \
     $(OUTPUT_DIR)/$(LOG_TEST) $(OUTPUT_DIR)/$(OCTREE2PATCHES) $(OUTPUT_DIR)/$(GRDOCTREEMAKER) \
     $(OUTPUT_DIR)/$(CSVOCTREEMAKER) $(OUTPUT_DIR)/$(TRANSOCT) $(OUTPUT_DIR)/$(LRTRN_APP) \
     $(OUTPUT_DIR)/$(TILETEST) $(OUTPUT_DIR)/$(REPLAY)

# Use one of the next two lines here , either the replay version or the other
#     $(OUTPUT_DIR)/$(TILETEST)
# for trn_replay #     $(OUTPUT_DIR)/$(TILETEST) $(OUTPUT_DIR)/$(REPLAY)

$(OUTPUT_DIR)/$(LOG_TEST): $(OUTPUT_DIR)/trn_log.o test_trn_log.cpp
	@echo building $@...
	$(CXX) $(INC_PATHS) $(CFLAGS) $^ -o $@
	@echo

#builds library object
$(NAV_LIBRARY):	$(LIB_OBJECTS)
	ar -r $(NAV_LIBRARY) $(LIB_OBJECTS)

#builds source files into an executable named $(PROGRAM)
$(OUTPUT_DIR)/$(PROGRAM): $(OBJECTS) mainMbari.cpp
	@echo building $@...
	$(CXX) $(CFLAGS) $(INC_PATHS) $(LIB_PATHS) $^ -o $@ $(LIBS) $(LD_FLAGS)
	@echo

#builds source files into an executable named $(SERVER)
$(OUTPUT_DIR)/$(SERVER): $(OBJECTS) $(QNX_LIB_DIR)/libqnx.a trn_server.cpp
	@echo building $@...
	$(CXX) $(CFLAGS) $(INC_PATHS) $(LIB_PATHS) $^ -o $@ $(LIBS)  $(LD_FLAGS)
	@echo

#builds source files into an executable named $(LRTRN_APP)
$(OUTPUT_DIR)/$(LRTRN_APP): $(OBJECTS) $(QNX_LIB_DIR)/libqnx.a lrtrn_app.cpp
	@echo building $@...
	$(CXX) $(CFLAGS) $(INC_PATHS) $(LIB_PATHS) $^ -o $@ $(LIBS) -llcm $(LD_FLAGS)
	@echo

#builds source files into an executable named $(TILETEST)
$(OUTPUT_DIR)/$(TILETEST): $(OBJECTS) tile_test.cpp
	@echo building $@...
	$(CXX) $(CFLAGS) $(INC_PATHS) $(LIB_PATHS) $^ -o $@ $(LIBS) $(LD_FLAGS)
	@echo

#builds source files into an executable named $(REPLAY)
$(OUTPUT_DIR)/$(REPLAY): $(OBJECTS) trn_replay.cpp
	@echo building $@...
	$(CXX) $(CFLAGS) $(INC_PATHS) $(LIB_PATHS) -L/usr/lib $^ -o $@ $(LIBS) $(LD_FLAGS)
	@echo

#builds source files into an executable named bodump
$(OUTPUT_DIR)/$(BODUMP): $(OBJECTS) bodump.cc
	@echo building $@...
	$(CXX) $(CFLAGS) $(INC_PATHS) -I. $(LIB_PATHS) $^ -o $@ $(LIBS) $(LD_FLAGS)
	@echo

#builds source files into an executable named $(PROGRAM)
$(OUTPUT_DIR)/$(CSVOCTREEMAKER): $(OBJECTS) csvOctreeMaker.cpp 
	@echo building $@...
	$(CXX) $(CFLAGS) $(INC_PATHS) $(LIB_PATHS) $^ -o $@ $(LIBS) $(LD_FLAGS)
	@echo

#builds source files into an executable named $(PROGRAM)
$(OUTPUT_DIR)/$(GRDOCTREEMAKER): $(OBJECTS) grdOctreeMaker.cpp 
	@echo building $@...
	$(CXX) $(CFLAGS) $(INC_PATHS) $(LIB_PATHS) $^ -o $@ $(LIBS) $(LD_FLAGS)
	@echo

#builds source files into an executable named $(PROGRAM)
$(OUTPUT_DIR)/$(OCTREE2PATCHES): $(OBJECTS) octree2Patches.cpp 
	@echo building $@...
	$(CXX) $(CFLAGS) $(INC_PATHS) $(LIB_PATHS) $^ -o $@ $(LIBS) $(LD_FLAGS)
	@echo

#builds source files into an executable named $(PROGRAM)
$(OUTPUT_DIR)/$(TRANSOCT): $(OBJECTS) translateOctree.cpp 
	@echo building $@...
	$(CXX) $(CFLAGS) $(INC_PATHS) $(LIB_PATHS) $^ -o $@ $(LIBS) $(LD_FLAGS)
	@echo


#generic command to build object files from source files
$(OUTPUT_DIR)/%.o :%.cpp
	@echo compiling $<...
	$(CXX) $(CFLAGS) $(INC_PATHS) -c $< -o $@
	@echo

#generic command to build dependency files from source files
$(OUTPUT_DIR)/%.d :%.cpp
	@[ -d $(OUTPUT_DIR) ] || mkdir -p $(OUTPUT_DIR)
	@echo generating dependency file for $<
	@set -e; $(CXX) -MM $(CFLAGS) $(INC_PATHS) $< \
	| awk '/o:/ {printf "%s", "$@ $(OUTPUT_DIR)/"} {print}' > $@; \
	[ -s $@ ] || rm -f $@
	@echo

###########################################################################
# clean-up commands

#clean will only delete the object and dependency files
.PHONY: clean
clean:
	rm -f $(OBJECTS) $(DEPEND) $(OUTPUT_DIR)/$(SERVER) $(OUTPUT_DIR)/$(PROGRAM) $(OUTPUT_DIR)/$(LOG_TEST)

#purge will delete the program file, object files, dependency files, and
#output directory
.PHONY: purge
purge:
	rm -f $(PROG) $(OBJECTS) $(DEPEND)
	rm -rf $(OUTPUT_DIR)

#include the dependencies
ifneq ($(MAKECMDGOALS),purge)
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPEND)
endif
endif
