#############################################################################
# user defined variables
# QNX Version
#
#define where to find the newmat libraries and headers
NEWMAT_INC_DIR = ../newmat
NETCDF_INC_DIR = /usr/local/include
NEWMAT_LIB_DIR = ../newmat
NETCDF_LIB_DIR = /usr/local/lib

OUTPUT_DIR = qnx

DEPEND_TOOL = makedepend
CXX = cc
CFLAGS = -DAUTONOMY -WC,-ei -O2 -g -WC,-xs -Wc,-ei -D_QNX -DUNIX
#
# LIBS = -lnewmat -lm -lnetcdf  
# Apparently -lm doesn't exist in QNX, so I took it out
#
LIBS = -lnewmat -lnetcdf
INC_PATHS = -I$(NEWMAT_INC_DIR) -I$(NETCDF_INC_DIR) -I../utils -I../framework -I../G2TerrainNav
LIB_PATHS = -L$(NEWMAT_LIB_DIR) -L$(NETCDF_LIB_DIR) ../utils/utils.lib

#SOURCES = main.cpp TerrainNav.cpp TerrainMap.cpp mapio.cpp myOutput.cpp

SOURCES = TerrainNav.cpp TerrainMap.cpp TerrainNavClient.cpp \
	mapio.cpp myOutput.cpp \
	TNavFilter.cpp TNavPointMassFilter.cpp \
	TNavParticleFilter.cpp \
	sensCalcs.cpp \
	matrixArrayCalcs.cpp
# For consistency, we'll use the structDefs code in G2TerrainNav
#	structDefs.cpp sensCalcs.cpp

PROGRAM = main

# Special definition for objects whose source is in a neighboring directory.
# The Dorado code base shares the structDefs implementation with the Rock Lab
# at Stanford. We'll remove it from terrainNav and move forward using the
# Rock Lab version that lives in G2TerrainNav. Henthorn 2016.03.07
#
STRUCTDEFS = $(OUTPUT_DIR)/structDefs.o

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

-include ../autonomy.make # required for autonomy code

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

all:	$(OUTPUT_DIR)/$(PROGRAM) terrainNav.lib

# Special definition for objects whose source is in a neighboring directory.
$(STRUCTDEFS): ../G2TerrainNav/structDefs.cpp ../G2TerrainNav/structDefs.h
	@echo compiling $<...
	$(CXX) $(CFLAGS) $(INC_PATHS) -c $< -o $@
	@echo

terrainNav.lib: 	$(OBJECTS) $(STRUCTDEFS)
		$(AR) terrainNav.lib +- $(OBJECTS) $(STRUCTDEFS)

#builds source files into an executable named $(PROGRAM)
$(OUTPUT_DIR)/$(PROGRAM): terrainNav.lib main.cpp
	@echo building $@...
	$(CXX) $(CFLAGS) $(INC_PATHS) $(LIB_PATHS) $^ -o $@ $(LIBS) terrainNav.lib
	@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 $<
	@$(DEPEND_TOOL) $(INC_PATHS) $? -f $@.tmp
	@mv $@.tmp $@
	@echo

#	@set -e; $(CXX) -MM $(CFLAGS) $(INC_PATHS) $< \
#	| awk '/o:/ {printf "%s", "$@ $(OUTPUT_DIR)/"} {print}' > $@; \
#	[ -s $@ ] || rm -f $@

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