#   D-ITG Main Makefile

###################
# Dynamic Options #
###################

# Enable SCTP support if specified on command line (requires kernel version >= 2.5.15)
ifdef sctp
	CXXOPT += -DSCTP
	LDOPT  += -lsctp
endif

# Enable DCCP support if specified on command line (requires kernel version >= 2.6.14-rc1)
ifdef dccp
	CXXOPT += -DDCCP
endif

# Enable ARM support
ifdef arm
	CXXOPT += -DARM
endif

# Set debug level as specified on command line (debug level may be increased to have more information)
ifdef debug
	CXXOPT += -DDEBUG=$(debug) -ggdb
endif

# Enable OnOff traffic types if specified on command line.
ifdef onoff
	CXXOPT += -DONOFF
endif

##################
# Static options # 
##################

# OS options
OS := $(shell uname)
ifeq "$(OS)" "Linux"
    OS := -DLINUX_OS
endif
ifeq "$(OS)" "FreeBSD"
    OS := -DLINUX_OS -DBSD
endif

export OS
export RM = rm -f
export CP = cp
export MV = mv
export SUFFIX =
export CXXFLAGS = $(CXXOPT) $(OS) -Wall -Wno-deprecated -fPIC
export LDFLAGS = -lpthread $(LDOPT)
export BASEDIR = $(shell pwd)
export BIN = $(BASEDIR)/../bin/
export COMMON = $(BASEDIR)/common/
export NRCLEAN = $(BASEDIR)/ITGSend/newran/
export EXEC_DIR = /usr/local/bin

ifndef CXX
	CXX = g++
endif
ifndef RANLIB
	export RANLIB = ranlib
endif

export THOBJS = common/thread.o
export OBJS = common/ITG.o common/timestamp.o common/serial.o
SUBDIRS = ITGSend ITGRecv ITGLog ITGDec libITG ITGManager

#################
# Generic Rules #
#################

.PHONY: $(SUBDIRS)
all: $(SUBDIRS)
	@ echo '-----------------------------------------------------------------------------------'
	@ echo 'D-ITG executables created in "bin" folder'
ifndef dccp
	@ echo 'To enable DCCP support add "dccp=on" option to make command line'
else
	@ echo 'DCCP support enabled'
endif
ifndef sctp
	@ echo 'To enable SCTP support add "sctp=on" option to make command line'
else
	@ echo 'SCTP support enabled'
endif
ifndef arm
	@ echo 'To enable ARM support add "arm=on" option to make command line'
else
	@ echo 'ARM support enabled'
endif
ifndef debug
	@ echo 'To enable DEBUG mode (verbosity level *) add "debug=*" option to make command line'
else
	@ echo 'DEBUG mode enabled (verbosity level = '$(debug)')'
endif
ifndef onoff
	@ echo 'To enable ON/OFF traffic types add "onoff=on" option to make command line'
else
	@ echo 'ON/OFF traffic types enabled'
endif
	@ echo -e 'NOTE: To change options type: make clean all [options]' 


ITGSend ITGRecv ITGLog ITGDec libITG: $(THOBJS) $(OBJS)
	@ (cd $@ && $(MAKE))

ITGManager:
	@ (cd $@ && $(MAKE))

install:
	(cd ITGSend && $(MAKE) install)
	(cd ITGRecv && $(MAKE) install)
	(cd ITGLog && $(MAKE) install)
	(cd ITGDec && $(MAKE) install)
	(cd ITGManager && $(MAKE) install)

uninstall:
	(cd /usr/local/bin && rm ITGSend ITGRecv ITGLog ITGDec ITGManager)
	
clean:
	$(RM) "$(COMMON)"*.o
	(cd ITGSend && $(MAKE) clean)
	(cd ITGRecv && $(MAKE) clean)
	(cd ITGLog && $(MAKE) clean)
	(cd ITGDec && $(MAKE) clean)
	(cd libITG && $(MAKE) clean)
	(cd ITGManager && $(MAKE) clean)

