#
# Makefile for Cerberus SimPlayer controller
#
# Author : Cetin Mericli
# 
# This file is largely based on default makefile in webots

ifndef WEBOTS_HOME
	WEBOTS_HOME = /usr/local/webots
endif

ifndef WEBOTS_HOME_PATH
	WEBOTS_HOME_PATH=$(WEBOTS_HOME)
endif

ifndef CONTROLLER_PATH
	CONTROLLER_PATH = ../Webots/controllers/SimPlayer
endif

KALMAN_HOME=../../CerberusPlayer/Planner/Role/Kalman
include $(KALMAN_HOME)/Makefile.def

ART2A_HOME=../../CerberusPlayer/Planner/Role/ART2A
include $(ART2A_HOME)/Makefile.def

CC_SOURCES=	../../CerberusPlayer/Common/CerberusMath.cc \
						../../CerberusPlayer/Common/CerberusMessage.cc \
						../../CerberusPlayer/Common/SensorValues.cc \
						../../CerberusPlayer/Motion/Matrix.cc \
						../../CerberusPlayer/Motion/Joint.cc \
					  	../../CerberusPlayer/Motion/JointData.cc \
						../../CerberusPlayer/Motion/Kinematics.cc \
					  	../../CerberusPlayer/Motion/Head.cc \
					  	../../CerberusPlayer/Motion/Leg.cc \
					  	../../CerberusPlayer/Motion/Tail.cc \
					  	../../CerberusPlayer/Motion/EllipticLocus.cc \
					  	../../CerberusPlayer/Motion/GT2005Parameters.cc \
					  	../../CerberusPlayer/Motion/GT2005Polygon.cc \
					  	../../CerberusPlayer/Motion/GT2005WalkingEngine.cc \
					  	../../CerberusPlayer/Motion/MotionInfo.cc \
					  	../../CerberusPlayer/Motion/MotionManager.cc \
					  	../../CerberusPlayer/Motion/MotionRequest.cc \
						../../CerberusPlayer/Vision/CRegion.cc \
						../../CerberusPlayer/Vision/Percept.cc \
						../../CerberusPlayer/Vision/RLERun.cc \
						../../CerberusPlayer/Vision/Ball.cc \
						../../CerberusPlayer/Vision/Beacon.cc \
						../../CerberusPlayer/Vision/Goal.cc \
						../../CerberusPlayer/Vision/GoalEdge.cc \
						../../CerberusPlayer/Vision/NewGoal.cc \
						../../CerberusPlayer/Vision/CerberusLines.cc \
						../../CerberusPlayer/Vision/CerberusPercepts.cc \
						../../CerberusPlayer/Vision/CerberusVision.cc \
						../../CerberusPlayer/Localization/Pose.cc \
						../../CerberusPlayer/Localization/Localization.cc \
						../../CerberusPlayer/Localization/MyEnvironment.cc \
						../../CerberusPlayer/Planner/BasicSkills.cc \
						../../CerberusPlayer/Planner/ExtendedSkills.cc \
						../../CerberusPlayer/Planner/Action/Act.cc \
						../../CerberusPlayer/Planner/Action/Actor.cc \
						../../CerberusPlayer/Planner/Action/SearchBall.cc \
						../../CerberusPlayer/Planner/Action/TrackBall.cc \
						../../CerberusPlayer/Planner/Action/GrabBall.cc \
						../../CerberusPlayer/Planner/Action/GrabbedWalk.cc \
						../../CerberusPlayer/Planner/Action/KickBall.cc \
						../../CerberusPlayer/Planner/Action/DoNothing.cc \
						../../CerberusPlayer/Planner/Action/SearchOpponentGoal.cc \
						../../CerberusPlayer/Planner/Action/AlignToOpponentGoal.cc \
						../../CerberusPlayer/Planner/Action/AlignToRobotWhileGrabbed.cc \
						../../CerberusPlayer/Planner/Action/GoalieLocate.cc \
						../../CerberusPlayer/Planner/Action/GoalieEscapeBall.cc \
						../../CerberusPlayer/Planner/Action/GoalieSaveBall.cc \
						../../CerberusPlayer/Planner/Action/InspectEnvironment.cc \
						../../CerberusPlayer/Planner/Action/DefenderClearBall.cc \
						../../CerberusPlayer/Planner/Action/GotoPose.cc \
						../../CerberusPlayer/Planner/Action/GoToPoseTrackBall.cc \
						../../CerberusPlayer/Planner/Action/GoToPoseSearchBeacon.cc \
						../../CerberusPlayer/Planner/Action/GoToPoseSearchBall.cc \
						../../CerberusPlayer/Planner/Action/TrackBallBetweenDistance.cc \
						../../CerberusPlayer/Planner/Action/GoToObject.cc \
						../../CerberusPlayer/Planner/Action/SearchBallWithHeadOnly.cc \
						../../CerberusPlayer/Planner/Action/GoToBall.cc \
						../../CerberusPlayer/Planner/Action/GoToInitPose.cc \
						../../CerberusPlayer/Planner/Action/SearchNewGoal.cc \
						../../CerberusPlayer/Planner/Action/AlignToNewGoal.cc \
						../../CerberusPlayer/Planner/Role/Role.cc \
						../../CerberusPlayer/Planner/Role/PrimaryAttackerRole.cc \
						../../CerberusPlayer/Planner/Role/PrimaryDefenderRole.cc \
						../../CerberusPlayer/Planner/Role/SecondaryAttacker.cc \
						../../CerberusPlayer/Planner/Role/GoalieRole.cc \
						../../CerberusPlayer/Planner/Role/DevelopmentalLearningRole.cc \
						../../CerberusPlayer/Planner/Role/RoleFactory.cc \
						../../CerberusPlayer/Planner/Role/Arkaq.cc \
						../../CerberusPlayer/Planner/Role/QMatrix.cc \
						../../CerberusPlayer/Planner/CerberusPlanner.cc \
						./SimDockObject/SimDockObject.cc \
						./SimMotion/SimMotion.cc \
						./SimCoreObject/SimCoreObject.cc \
					 	./SimPlayer.cc

# default commands definitions
NULL_DEVICE=/dev/null
PWD_COMMAND=pwd
RM_COMMAND=rm -f
LS_COMMAND=ls
CP_COMMAND=cp

# find out whether we are in a Windows Command Prompt
ifeq ($(OS),Windows_NT)
	ifneq ($(TERM),msys)
		OSTYPE=windows
		NULL_DEVICE=nul
		PWD_COMMAND=cd
		RM_COMMAND=del /Q
		LS_COMMAND=dir /B
		CP_COMMAND=copy
	endif
endif

ifndef OSTYPE
	OSTYPE=$(shell uname)
endif

ifeq ($(OSTYPE),Linux)
	OSTYPE=linux
endif

ifeq ($(OSTYPE),linux-gnu)
	OSTYPE=linux
endif

ifeq ($(OSTYPE),)
	OSTYPE=windows
endif

ifeq ($(OSTYPE),msys)
	OSTYPE=windows
endif

ifneq ($(findstring MINGW,$(OSTYPE)),)
	OSTYPE=windows
endif

ifneq ($(findstring CYGWIN,$(OSTYPE)),)
	OSTYPE=windows
endif

ifndef DOS_CONSOLE
	WINDOWS_APP=-mwindows
endif

ifeq ($(OSTYPE),windows)
	PLATFORM_CFLAG=-DWIN32 $(WINDOWS_APP)
endif

ifeq ($(OSTYPE),linux)
	PLATFORM_CFLAG=-DLINUX
endif


ifndef WEBOTS_HOME
#assuming we are compiling controller in the installation directory
	export WEBOTS_HOME=../../../..
else
	WEBOTS_HOME:=$(WEBOTS_HOME)
endif

# compute the current directory, the CURDIR variable may be already set
ifndef $(CURDIR)
	CURDIR=$(shell $(PWD_COMMAND))
endif

# compute the name of the controller from its directory (i.e. braiten)
BAD_NAME = $(basename $(notdir $(CURDIR)))

# this is a hack to work around a problem with spaces in dir names
NAME = $(word $(words $(BAD_NAME)),$(BAD_NAME))

# if the source files were not explicitly defined, we assume that
# there is only one C or Java source file

ifndef C_SOURCES
	C_SOURCES   =$(shell $(LS_COMMAND) $(NAME).c 2> $(NULL_DEVICE))
endif
ifndef CPP_SOURCES
	CPP_SOURCES =$(shell $(LS_COMMAND) $(NAME).cpp 2> $(NULL_DEVICE))
endif
ifndef CC_SOURCES
	CC_SOURCES  =$(shell $(LS_COMMAND) $(NAME).cc 2> $(NULL_DEVICE))
endif

ifndef JAVA_SOURCES
	JAVA_SOURCES=$(shell $(LS_COMMAND) $(NAME).java 2> $(NULL_DEVICE))
endif
ifndef SOURCE_INCLUDES
	SOURCE_INCLUDES=
endif

INCLUDE=-I"$(WEBOTS_HOME)/include" $(KALMAN_INCLUDE)

ifeq ($(OSTYPE),windows)
	EXE_EXTENSION=.exe
else
	EXE_EXTENSION=
endif

# the objects files result from the C or C++ sources
C_OBJECTS   = $(C_SOURCES:.c=.o)
CPP_OBJECTS = $(CPP_SOURCES:.cpp=.o)
CC_OBJECTS  = $(CC_SOURCES:.cc=.o)
RC_OBJECTS  = $(RC_SOURCES:.rc=.o)

# set the controller library needed
CTR_LIB=Controller

# set CC to your favorite C or C++ compiler
ifneq ($(C_SOURCES),)
	CC=gcc
else
	ifneq ($(CPP_SOURCES),)
		CC=g++
	else
		ifneq ($(CC_SOURCES),)
			CC=g++
		else
			CC=unknown_compiler
		endif
	endif
endif

ifeq ($(OSTYPE),windows)
	PLATFORM_LIBS=-lmingw32 $(WINDOWS_APP)
else
	PLATFORM_LIBS=
endif

LIBRARY=-L"$(WEBOTS_HOME)/lib" -l$(CTR_LIB) $(PLATFORM_LIBS) $(LIBRARIES) $(KALMAN_LIB) $(ART2A_LIB)

ifneq ($(WEBOTS_DEMO_CODE),)
	CC_OPTS_DEMO=-DWEBOTS_DEMO_CODE=$(WEBOTS_DEMO_CODE)
endif

# set the C compiler options
CC_OPTS=-c $(INCLUDE) $(CC_OPTS_DEMO) -Wall $(CFLAGS) $(CFLAGS_WIN) $(PLATFORM_CFLAG) -fpermissive -DNOMINMAX

# set JAVAC to your favorit Java compiler
JAVAC=javac

# set the Java compiler options

ifeq ($(OSTYPE),windows)
	JAVAC_OPTS=-classpath "$(WEBOTS_HOME)/lib/Controller.jar;."
else
	JAVAC_OPTS=-classpath "$(WEBOTS_HOME)/lib/Controller.jar":.
endif

ifneq ($(C_SOURCES)$(CC_SOURCES)$(CPP_SOURCES),)
	TARGETS+=$(NAME)$(EXE_EXTENSION)
endif

TARGETS+=$(EXTRA_TARGETS)

all: $(TARGETS)
compil: all install

# rules to make the executable file from C/C++ objects
$(NAME)$(EXE_EXTENSION): $(C_OBJECTS) $(CC_OBJECTS) $(CPP_OBJECTS) $(RC_OBJECTS)
	$(CC) -o $(NAME)$(EXE_EXTENSION) $(C_OBJECTS) $(CC_OBJECTS) $(CPP_OBJECTS) $(RC_OBJECTS) $(LIBRARY) $(EXTRA_LFLAGS)
	$(EXTRA_FINAL_CMD)

	@echo "***********************************************************"
	@echo "****                   SimPlayer make done...                        ****"
	@echo "***********************************************************"


# rule to make automatic dependencies
# adapted from the GNU make info file
%.d:%.c
	$(CC) $(INCLUDE) $(CFLAGS) $(PLATFORM_CFLAG) -MM $< -MT "$(@:.d=.o) $@ " > $@

%.d:%.cc
	$(CC) $(INCLUDE) $(CFLAGS) $(PLATFORM_CFLAG) -MM $< -MT "$(@:.d=.o) $@ " > $@

%.d:%.cpp
	$(CC) $(INCLUDE) $(CFLAGS) $(PLATFORM_CFLAG) -MM $< -MT "$(@:.d=.o) $@ " > $@

ifneq ($(MAKECMDGOALS),clean)
-include $(C_SOURCES:.c=.d) $(CC_SOURCES:.cc=.d) $(CPP_SOURCES:.cpp=.d)
endif

# generic rule to make the object files
%.o:%.c $(SOURCE_INCLUDES)
	$(CC) $(CC_OPTS) $(EXTRA_CFLAGS) $< -o $@

%.o:%.cpp $(SOURCE_INCLUDES)
	$(CC) $(CC_OPTS) $(EXTRA_CFLAGS) $< -o $@

%.o:%.cc $(SOURCE_INCLUDES)
	$(CC) $(CC_OPTS) $(EXTRA_CFLAGS) $< -o $@

%.o:%.rc
	windres -i $< -o $@ $(RC_INCLUDE) --define __WIN32__ --define __WIN95__ --define __GNUWIN32__



install: $(TARGETS)
	@$(CP_COMMAND) $(NAME)$(EXE_EXTENSION) $(CONTROLLER_PATH)/$(NAME)$(EXE_EXTENSION)
	@chmod +x $(CONTROLLER_PATH)/$(NAME)$(EXE_EXTENSION)

	@echo "***********************************************************"
	@echo "****              SimPlayer controller is deployed...              ****"
	@echo "***********************************************************"	

    #@-$(CP_COMMAND) $(NAME)$(EXE_EXTENSION) $(CONTROLLER_PATH)/$(NAME)$(EXE_EXTENSION)
    
# how to clean up the directory
clean: $(EXTRA_TARGETS)
	@-$(RM_COMMAND) *.o *.d *~ $(CONTROLLER_PATH)$(NAME)$(EXE_EXTENSION) $(NAME)$(EXE_EXTENSION) core *.exe_webots *.opt *.ncb *.obj *.plg vc60.idb 2> $(NULL_DEVICE)
	@-$(RM_COMMAND) ./SimCoreObject/*.o ./SimCoreObject/*.d
	@-$(RM_COMMAND) ./SimDockObject/*.o ./SimDockObject/*.d
	@-$(RM_COMMAND) ./SimMotion/*.o ./SimMotion/*.d	
	@-$(RM_COMMAND) ../../CerberusPlayer/Motion/*.o ../../CerberusPlayer/Motion/*.d			
	@-$(RM_COMMAND) ../../CerberusPlayer/Common/*.o ../../CerberusPlayer/Common/*.d		
	@-$(RM_COMMAND) ../../CerberusPlayer/Vision/*.o ../../CerberusPlayer/Vision/*.d			
	@-$(RM_COMMAND) ../../CerberusPlayer/Localization/*.o ../../CerberusPlayer/Localization/*.d
	@-$(RM_COMMAND) -R ../../CerberusPlayer/Planner/*.o ../../CerberusPlayer/Planner/*.d
	@-$(RM_COMMAND) -R ../../CerberusPlayer/Planner/Action/*.o ../../CerberusPlayer/Planner/Action/*.d
	@-$(RM_COMMAND) -R ../../CerberusPlayer/Planner/FuzzyEngine/*.o ../../CerberusPlayer/Planner/FuzzyEngine/*.d
	@-$(RM_COMMAND) -R ../../CerberusPlayer/Planner/Role/*.o ../../CerberusPlayer/Planner/Role/*.d			

	@echo "***********************************************************"
	@echo "****                   SimPlayer clean done...                        ****"
	@echo "***********************************************************"	

# show the ostype recognized by the Makefile
ostype:
	@echo OSTYPE=$(OSTYPE)
        
test:
	@echo TEST=$(TEST)
    