CXX		= g++
#CXX            = /usr0/local/libexec/gcc-3.2/bin/g++
#CXX            = /usr/local/libexec/gcc-2.95.3/bin/g++
#CXX		= insure
RM = /bin/rm
PERL = /usr/local/bin/perl5

PERLFLAGS = -w

CXXFLAGS += -Wall
#CXXFLAGS += -Woverloaded-virtual
# I have to use -Wno-deprecated for gcc3.2 because I still want to use ostrstream 
# instead of ostringstream since it allows me (non-copied) access to the char* data array
CXXFLAGS += -Wno-deprecated

OPTFLAGS += -O2

#These are for profiling
#CXXFLAGS += -pg
#LDFLAGS += -pg

DEBUGMODE = TRUE

WD = $(shell pwd)
#RCCPARSER_DIR = $(WD)/../rccparser/1.2.4-install
RCCPARSER_DIR = $(WD)/../rccparser/1.2.5-install
RCCPARSER_SRC_DIR = $(WD)/../rccparser/1.2.5/src
GD_DIR        = $(WD)/../gd/2.0.12-install
CLANGLIBDIR = $(WD)/../clanglib
RCSSNET_INC = /usr/local/include
RCSSNET_LIB = /usr/local/lib
PLANNING_DIR = $(WD)/planning

CXXFLAGS += -I$(WD)
CXXFLAGS += -I$(PLANNING_DIR)
CXXFLAGS += -I$(RCCPARSER_DIR)/include
CXXFLAGS += -I$(GD_DIR)/include
CXXFLAGS += -I$(CLANGLIBDIR)
CXXFLAGS += -I$(RCSSNET_INC)

ifneq ($(strip $(DEBUGMODE)),)
#we are in debug mode
CXXFLAGS += -g
else
CXXFLAGS += -DNO_ACTION_LOG
endif
#CXXFLAGS += $(OPTFLAGS)

LDFLAGS += -L$(RCCPARSER_DIR)/lib
LDFLAGS += -L$(GD_DIR)/lib
LDFLAGS += -L$(CLANGLIBDIR) 
LDFLAGS += -Wl,-rpath -Wl,$(RCSSNET_LIB)
LDFLAGS += -Wl,-rpath -Wl,$(GD_DIR)/lib
#LDFLAGS += -static

# I am removing the rccparser because of this bug
# https://sourceforge.net/tracker/index.php?func=detail&aid=766636&group_id=26880&atid=388605
#CXXLIBS += -lrcgparser -lrccparser -lrcssclangparser -lrcssnet -lgd -lpng -lz -ljpeg -lfreetype -lpthread -lm
CXXLIBS += -lrcgparser -lrcssclangparser -lrcssnet -lgd /usr/local/lib/libpng.so.3 -lz -ljpeg -lfreetype -lpthread -lm
# use this to make a static version
#CXXLIBS += -lrcgparser -lrcssclangparser -lrcssnet -lgd /usr/local/lib/libpng.a -lz -ljpeg -lfreetype -lpthread -lm

PROG = coach

OTHER_GEN_FILES = \
fixed_rules.txt \
Formations/all_forms.list \
Formations/basic.pform \
Formations/cmloki532.pform \
Formations/cmloki433.pform \
Formations/cmloki334.pform \
Formations/hand3232.pform \
empty.rules \
very_def.rules \
very_off.rules \

all: $(PROG) $(OTHER_GEN_FILES)

full: cln depend all

NORM_OFILES  =  FileReader.o ParamReader.o data.o ErrBoundValue.o CoachTagFunction.o
NORM_OFILES  += ServerParam.o PlayerParam.o CoachParam.o 
NORM_OFILES  += HeteroPlayerType.o PlayerTypeStorage.o
NORM_OFILES  += client.o misc.o test.o soccer_utils.o LogDouble.o
NORM_OFILES  += WorldState.o WorldHistory.o WorldStateWMI.o
NORM_OFILES  += SorterMsgBuilder.o
NORM_OFILES  += ModuleRegistry.o Module.o
NORM_OFILES  += SingleCallRunner.o OnlineRunner.o OnlineParser.o
NORM_OFILES  += LogfileRunner.o LogfileDataHandler.o
NORM_OFILES  += ModTest.o ModCMRep.o CoachMessageQueue.o
NORM_OFILES  += Formation.o FormationLearner.o ModFormation.o
NORM_OFILES  += ModSetplayPlan.o
NORM_OFILES  += PlayerOccupancy.o AbstractState.o AbstractStateElements.o ModAbstract.o
NORM_OFILES  += AbstractStateDifference.o AdviceTree.o
NORM_OFILES  += ModStatImage.o
NORM_OFILES  += ModFeatures.o
NORM_OFILES  += MarkovChain.o LibSeaGraphWriter.o
NORM_OFILES  += MDP.o SoccerMDP.o MDPConversion.o QTable.o QTableFlex.o Policy.o
NORM_OFILES  += SoccerModelWValue.o
NORM_OFILES  += StateTraceAnalyzer.o
NORM_OFILES  += ModTrainer.o TrainerConnection.o
NORM_OFILES  += BinaryFile.o
NORM_OFILES  += ModGlobalAdapt.o FixedAdviceAU.o CLangFileReader.o
NORM_OFILES  += GAPredicateAdaptor.o GAStrategySelector.o
NORM_OFILES  += ABMDP.o
NORM_SRCS   = $(NORM_OFILES:%.o=%.C)

PLANNING_OFILES = ControllerFunction.o TestFunction.o Plan.o PlanExecution.o 
PLANNING_OFILES += PlannerController.o Problem.o PlanNode.o ShareContModules.o
PLANNING_OFILES += FourLevelPlanner.o SharePlannerModules.o
PLANNING_OFILES += PlayerDistribution.o BallMovement.o
PLANNING_OFILES += MovementObservation.o OppModel.o OppModelSet.o 
PLANNING_OFILES += BinaryString.o setplay_types.o plan_utils.o
PLANNING_OFILES += DefOppModel.o
PLANNING_OFILES += model_test.o
PLANNING_OFILES += opp_models.o
PLANNING_OFILES += PlannerModules.o
PLANNING_OFILES += ContModules.o
PLANNING_SRCS = $(PLANNING_OFILES:%.o=$(PLANNING_DIR)/%.C)

GEN_OFILES = asdiff_predicates.o
GEN_SRCS = $(GEN_OFILES:%.o=%.C)

RCCPARSER_OFILES = rcclexer.o rccparse.o rccparser.o
RCCPARSER_SRCS = $(RCCPARSER_OFILES:%.o=$(RCCPARSER_SRC_DIR)/%.cpp)

OFILES = $(NORM_OFILES) $(PLANNING_OFILES) $(GEN_OFILES) $(RCCPARSER_OFILES)
SRCS   = $(NORM_SRCS)   $(PLANNING_SRCS)   $(GEN_SRCS)   $(RCCPARSER_SRCS)

$(PROG): $(OFILES) $(RCCPARSER_DIR)/lib/librccparser.a $(CLANGLIBDIR)/librcssclangparser.a 
	$(CXX) $(LDFLAGS) -o $@ $(OFILES) $(CXXLIBS)

%.o : %.cpp
	$(CXX) -c $(CXXFLAGS) $(CXXINCLUDE) $<
%.o : $(PLANNING_DIR)/%.C
	$(CXX) -c $(CXXFLAGS) $(CXXINCLUDE) $<
%.o : $(RCCPARSER_SRC_DIR)/%.cpp
	$(CXX) -c $(CXXFLAGS) $(CXXINCLUDE) $<

asdiff_predicates.C: asdiff_predicates.names names2code.pl
	$(PERL) $(PERLFLAGS) names2code.pl asdiff_predicates.names asdiff_predicates.C

fixed_rules.txt: repl_mult.pl Formations/num_assignments fixed_rules_tmpl.txt
	 ./repl_mult.pl Formations/num_assignments fixed_rules_tmpl.txt fixed_rules.txt
%.rules: %.trules repl_mult.pl Formations/num_assignments
	 ./repl_mult.pl Formations/num_assignments $< $@

.SUFFIXES: .form .pform
#.form.pform:  $< form_perm.pl Formations/num_assignments
#	./form_perm.pl Formations/num_assignments $< $@
%.pform: %.form form_perm.pl Formations/num_assignments
	./form_perm.pl Formations/num_assignments $< $@

# should always fire
.PHONY: FORCE
Formations/all_forms.list: FORCE
	echo "# DO NOT EDIT: automatically generated by the coach's Makefile" > $@
	echo "version: 0.2" >> $@
	cd Formations; ls *.form *.pform >> all_forms.list

cln: clean
clean: 
	rm -f $(PROG) *.o $(PLANNING_DIR)/*.o $(GEN_SRCS) fixed_rules.txt Formations/basic-perm.form

genfiles: $(GEN_SRCS) $(OTHER_GEN_FILES)

depend: genfiles
	$(RM) -f $(DEPEND)
	$(CXX) -M $(CXXFLAGS) $(CXXINCLUDE) $(SRCS) >> $(DEPEND)

DEPEND = Makefile.depend
ifeq ($(wildcard $(DEPEND)),$(DEPEND))
include $(DEPEND)
endif
