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

USE_MATLAB = 0

#define where to find the newmat, netcdf and matlab libraries and headers
NEWMAT_LIB_DIR = /usr/lib
NEWMAT_INC_DIR = /usr/include/newmat
NETCDF_LIB_DIR = /usr/lib
NETCDF_INC_DIR = /usr/include
#NEWMAT_LIB_DIR = /home/debbie/packages/newmat10/lib
#NEWMAT_INC_DIR = /home/debbie/packages/newmat10/include
#NETCDF_LIB_DIR = /home/debbie/packages/netcdf-4.0/lib
#NETCDF_INC_DIR = /home/debbie/packages/netcdf-4.0/include

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
 
 #MATLAB_LIB_DIR = /home/debbie/packages/matlab75/bin/glnx86
 #MATLAB_INC_DIR = /home/debbie/packages/matlab75/extern/include
 #MATLAB_LD_DIR = /home/debbie/packages/matlab75/sys/os/glnx86

#use these instead if on laptop
 #MATLAB_LIB_DIR = /home/debbie/packages/matlab/2009b/bin/glnxa64
 #MATLAB_INC_DIR = /home/debbie/packages/matlab/2009b/extern/include
 #MATLAB_LD_DIR = /home/debbie/packages/matlab/2009b/bin/glnxa64:/home/debbie/packages/matlab/2009b/sys/os/glnxa64
endif

#define compilation variables
OUTPUT_DIR = linux
CXX = g++
ifeq ($(USE_MATLAB),1)
	CFLAGS = -Wall -pg -DUSE_MATLAB
else
	CFLAGS = -Wall -pg #-O2
endif

ifeq ($(USE_MATLAB),1)
 LIBS = -lnewmat -lm -lnetcdf -lrt -lglut -lGLU -leng -lmat -lmx -lGL -lpthread $(pkg-config --libs pcl_common-1.6 pcl_io-1.6 pcl_octree-1.6 pcl_surface-1.6 pcl_kdtree-1.6)
# -lpcl_io -lpcl_common -lpcl_kdtree -lpcl_octree -lOpenNI -lusb-1.0
 INC_PATHS = -I$(NEWMAT_INC_DIR) -I$(NETCDF_INC_DIR) -I$(MATLAB_INC_DIR) $(pkg-config --cflags pcl_common-1.6 pcl_io-1.6 pcl_octree-1.6 pcl_surface-1.6 pcl_kdtree-1.6)
#-I$(PCL_INC_DIR)
 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
 LIBS = -lnewmat -lm -lnetcdf -lrt -lglut -lGLU -lGL -lpthread 
 #LIBS += $(shell pkg-config --libs pcl_common-1.6 pcl_io-1.6 pcl_octree-1.6 pcl_surface-1.6 pcl_kdtree-1.6 pcl_features-1.6) 
 LIBS += -lpcl_io -lOpenNI -lusb-1.0 -lpcl_surface -lpcl_features -lpcl_search -lpcl_kdtree -lpcl_octree -lflann_cpp -lpcl_common -lpcl_visualization
 LIBS += -lvtkCommon -lboost_system
 LIBS += -lvtkalglib \
-lvtkCharts \
-lvtkCommon \
-lvtkDICOMParser \
-lvtkexoIIc \
-lvtkFiltering \
-lvtkGenericFiltering \
-lvtkGeovis \
-lvtkGraphics \
-lvtkHybrid \
-lvtkImaging \
-lvtkInfovis \
-lvtkIO \
-lvtkmetaio \
-lvtkproj4 \
-lvtkRendering \
-lvtksys \
-lvtkverdict \
-lvtkViews \
-lvtkVolumeRendering \
-lvtkWidgets
 INC_PATHS = -I$(NEWMAT_INC_DIR) -I$(NETCDF_INC_DIR) 
 INC_PATHS += `pkg-config --cflags pcl_common-1.6 pcl_io-1.6 pcl_octree-1.6 pcl_surface-1.6 pcl_kdtree-1.6 pcl_features-1.6` 
 INC_PATHS += -I/usr/local/include/vtk-5.10
 LIB_PATHS = -L$(NEWMAT_LIB_DIR) -L$(NETCDF_LIB_DIR)
 LIB_PATHS += -L/usr/local/lib/vtk-5.10
 LD_FLAGS = -pg
endif

SOURCES = TerrainNav.cpp TNavFilter.cpp TNavPointMassFilter.cpp \
	TNavParticleFilter.cpp TNavExtendKalmanFilter.cpp \
	TNavSigmaPointFilter.cpp TerrainMap.cpp TerrainNavClient.cpp \
	mapio.cpp structDefs.cpp myOutput.cpp matrixArrayCalcs.cpp \
	ParticlePlot.cpp GlutWindow.cpp GlutWindow2d.cpp ColorHelper.cpp
PROGRAM = main

SERVER = trn_server

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

LIB_OBJECTS = $(OUTPUT_DIR)/TerrainNav.o \
	$(OUTPUT_DIR)/TNavFilter.o \
	$(OUTPUT_DIR)/TNavPointMassFilter.o \
	$(OUTPUT_DIR)/TNavParticleFilter.o \
	$(OUTPUT_DIR)/TNavExtendKalmanFilter.o \
	$(OUTPUT_DIR)/TNavSigmaPointFilter.o \
	$(OUTPUT_DIR)/TerrainMap.o \
	$(OUTPUT_DIR)/TerrainNavClient.o \
	$(OUTPUT_DIR)/mapio.o \
	$(OUTPUT_DIR)/structDefs.o \
	$(OUTPUT_DIR)/myOutput.o \
	$(OUTPUT_DIR)/matrixArrayCalcs.o \

NAV_LIBRARY = $(OUTPUT_DIR)/libterrainNav.a


#############################################################################
# build commands
all: $(NAV_LIBRARY) $(OUTPUT_DIR)/$(PROGRAM) $(OUTPUT_DIR)/$(SERVER)

#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) main.cpp
	@echo building $@...
	$(CXX) $(INC_PATHS) $(LIB_PATHS) $^ -o $@ $(LIBS) $(LD_FLAGS)
	@echo

#builds source files into an executable named $(SERVER)
$(OUTPUT_DIR)/$(SERVER): $(OBJECTS) trn_server.cpp
	@echo building $@...
	$(CXX) $(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)

#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
