#/* LICENSE:
#  =========================================================================
#    CMPack'04 Source Code Release for OPEN-R SDK 1.1.5-r2 for ERS7
#    Copyright (C) 2004 Multirobot Lab [Project Head: Manuela Veloso]
#    School of Computer Science, Carnegie Mellon University
#    All rights reserved.
#  ========================================================================= */

C++ := g++

# Set this to 1 or 2 to set the version of the program
VERSION := 2

# Build version 2 with graphics option (1=yes, 0=no)
# (version 1 always uses graphics...)
GRAPHICS := 1

############################################################################
############################################################################
ifeq ($(VERSION),1)

CXXFLAGS := -g -DPLATFORM_LINUX -Wall `gtk-config --cflags gtk` -I../..
LFLAGS := `gtk-config --libs gtk` -lpthread 

GLADE_FILE := chokechain.glade

GLADE_SRCS := \
	gui/interface.cc \
	gui/support.cc \

GLADE_HEADERS := \
	gui/interface.h \
	gui/support.h \

SRCS := \
	main.cc \
	DataInterface.cc \
	../../agent/shared_code/CircBufPacket.cc \
	../../agent/shared_code/Gaussian2.cc \
	serial.cc \
	SPOutDecoder.cc \
	State.cc \
	StreamProcessor.cc \
	WaveServer.cc \
	WLDecoder.cc \
	../log_processing/shared/BehaviorPacketDecoder.cc \
	../log_processing/shared/LocalizationPacketDecoder.cc \
	../log_processing/shared/LogReader.cc \
	../log_processing/shared/MotionPacketDecoder.cc \
	../log_processing/shared/PacketDecoder.cc \
	../log_processing/shared/TrackerPacketDecoder.cc \
	../log_processing/shared/VisionPacketDecoder.cc \
	../log_processing/shared/VisionRawPacketDecoder.cc \
	../log_processing/shared/VisionRLEPacketDecoder.cc \
	../log_processing/shared/WMDebugPacketDecoder.cc \
	../log_processing/shared/WorldModelPacketDecoder.cc \
	gui/GuiState.cc \
	gui/UI.cc \
	gui/callbacks.cc \
	$(GLADE_SRCS) \

TARGET = chokechain

endif
############################################################################
############################################################################

############################################################################
############################################################################
ifeq ($(VERSION),2)

CXXFLAGS := -g -DPLATFORM_LINUX -Wall -I../.. -DGRAPHICS=$(GRAPHICS) -DUSE_GSL
LFLAGS := -lpthread -lgsl -lgslcblas

ifeq ($(GRAPHICS),1)

CXXFLAGS += `fltk-config --cxxflags`
LFLAGS += `fltk-config --ldstaticflags` 

fluidfl = $(shell find . -name '*.fl' 2> /dev/null) # Pipe stderr to dev null
fluidhfiles = $(patsubst %.fl,%.h,$(fluidfl))
fluidccfiles = $(patsubst %.fl,%.cc,$(fluidfl))
fluidfiles = $(fluidhfiles) $(fluidccfiles)

GENERATED_SOURCES = $(fluidfiles)

GRAPHICS_CCFILES = \
	$(shell ls gui_chokechain/*.cc 2> /dev/null) \
	$(shell ls gui_lib/*.cc 2> /dev/null) \
	$(fluidccfiles) 

endif

SRCS := \
	chokechain2.cc \
	DataInterface.cc \
	../../agent/shared_code/CircBufPacket.cc \
	../../agent/shared_code/Gaussian2.cc \
	../../agent/shared_code/matrix.cc \
	serial.cc \
	SPOutDecoder.cc \
	StreamProcessor.cc \
	WaveServer.cc \
	WLDecoder.cc \
	../log_processing/shared/BehaviorPacketDecoder.cc \
	../log_processing/shared/LocalizationPacketDecoder.cc \
	../log_processing/shared/LogReader.cc \
	../log_processing/shared/MotionPacketDecoder.cc \
	../log_processing/shared/PacketDecoder.cc \
	../log_processing/shared/TrackerPacketDecoder.cc \
	../log_processing/shared/VisionPacketDecoder.cc \
	../log_processing/shared/VisionRawPacketDecoder.cc \
	../log_processing/shared/VisionRLEPacketDecoder.cc \
	../log_processing/shared/WMDebugPacketDecoder.cc \
	../log_processing/shared/WorldModelPacketDecoder.cc \
	gui_chokechain/RLEViewer.cc \
	gui_chokechain/GuiState.cc \
	$(GRAPHICS_CCFILES)

TARGET := chokechain2

endif
############################################################################
#############################################################################


OBJS := $(SRCS:.cc=.o)
DEPENDS := $(SRCS:%.cc=%.d)

GEN_HS :=
DEP_PROCESS:=dep_process

#############################################################################

# Make and install it
all: $(TARGET)
	(cd ../bin; \rm -f $(TARGET); \ln -s ../choke_chain/$(TARGET) ./)

# This is version 2 which uses fltk for graphics
chokechain2: $(OBJS) 
	$(C++) -o  $@ $(CFLAGS) $^ $(LFLAGS)

# This is version 1 which uses gtk for graphics
chokechain: $(OBJS)
	$(C++) -o $@ $(CFLAGS) $^ $(LFLAGS)

clean: 
	rm -f $(OBJS) $(TARGET) ../bin/$(TARGET) 
	rm -f $(GENERATED_SOURCES) $(DEPENDS)

dep: depend

depend:
	rm -f $(DEPENDS)

include $(DEPENDS)

#############################################################################

# GTK-generated files
$(GLADE_HEADERS) $(GLADE_SRCS): $(GLADE_FILE)
	glade -w $<

# FLTK-generated files (using fluid, the interface generation utility)
%.h %.cc: %.fl
	./fluid_dir.pl $<

%.o: %.cc
	$(C++) -c -o $@ $< $(CXXFLAGS)

%.d: %.cc $(GEN_HS)
	$(C++) -M $(CXXFLAGS) $< >$@.tmp
	$(DEP_PROCESS) $@.tmp $(basename $@).o $(GEN_HS) >$@
	rm $@.tmp
