ifeq ($(makefile_defs),"1")
else
makefile_defs="1"
export makefile_defs

GIT := $(shell which git)

ifneq ($(GIT),)
   SCM_REV := $(shell if git --help describe |grep dirty 2>&1 >/dev/null ; \
        then git describe --always --dirty 2>/dev/null; \
        else git describe --always 2>/dev/null; fi)
endif

#version number
VERSION = 1
PATCHLEVEL = 6
SUBLEVEL = 0

ifneq ($(SCM_REV),)
RELEASE := $(SCM_REV)
else
REL_VERSION := $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)
RELEASE := $(REL_VERSION)$(EXTRAVERSION)
endif

APP_NAME ?= sems

CPPFLAGS += -D_DEBUG \
	  -D_THREAD_SAFE -D_REENTRANT \
	  -DSEMS_VERSION='"$(RELEASE)"' -DARCH='"$(ARCH)"'\
	  -DOS='"$(OS)"' 	\
	  -DSYSTEM_SAMPLECLOCK_RATE=32000LL \
          -DSEMS_APP_NAME='"$(APP_NAME)"'
#         -DMAX_RTP_SESSIONS=8192 \
#	  -DSUPPORT_IPV6 \
#	  -DNO_THREADID_LOG \
#         -DLOG_PRETTY_FUNCTION \
#         -DLOG_LOC_DATA_ATEND


# compile with session thread pool support?
#      use this for very high concurrent call count 
#      applications (e.g. for signaling only)
#      if compiled with thread pool, there will be a 
#      thread pool of configurable size processing the
#      signaling and application logic of the calls.
#      if compiled without thread pool support, every
#      session will have its own thread.
#
#USE_THREADPOOL = yes

# compile with spandsp DTMF detection? see soft-switch.org
#   this needs a fairly new version of spandsp - tested with 0.0.4pre11
#   will not work with spandsp 0.0.2 .
#   (which means that current debian and gentoo packages don't work)
#   
#USE_SPANDSP    = yes
# statically link spandsp library? 
#   (might need adjusting spandsp lib path LIBSPANDSP_LDIR - may be /usr/lib)
#LIBSPANDSP_STATIC = yes
#LIBSPANDSP_LDIR = /usr/local/lib/

# compile with sample rate conversion from secret rabbit code? 
# (see http://www.mega-nerd.com/SRC/)
#
#USE_LIBSAMPLERATE = yes

# compile with internal resampler?
#
#
USE_INTERNAL_RESAMPLER = yes

#
# ZRTP support? (see zfoneproject.com)
#WITH_ZRTP = yes

#
# exclude some modules from compilation?
# e.g. python modules:

exclude_core_modules = g729 silk
#exclude_apps_modules = py_sems ivr mailbox pin_collect conf_auth mp3 examples

# build in support for monitoring?
#
#
USE_MONITORING=yes

# Support for long debug messages? (useful for debugging SIP messages' contents)
#
# disable for slight performance gain
LONG_DEBUG_MESSAGE=yes

# Is this a debug build or not?
debug=no

# Propagate all unparsed SIP headers into AmSipReply. Disabled by default
# because only special applications might need this and it brings higher memory
# demands and affects performance.
#PROPAGATE_UNPARSED_REPLY_HEADERS=no


################### end of configuration section #######################

LDFLAGS += -lm

GETOS=$(COREPATH)/compat/getos
GETARCH=$(COREPATH)/compat/getarch


OS   := $(shell if [ -f $(GETOS) ] ; then $(GETOS); else  $(CC) $(EXTRA_CFLAGS) -o $(GETOS) $(GETOS).c && $(GETOS); fi)
ARCH := $(shell if [ -f $(GETARCH) ] ; then $(GETARCH); else $(CC) $(EXTRA_CFLAGS) -o $(GETARCH) $(GETARCH).c && $(GETARCH); fi)

ifdef USE_THREADPOOL
CPPFLAGS += -DSESSION_THREADPOOL
endif

ifndef USE_SPANDSP
exclude_core_modules += g722
endif

ifdef USE_MONITORING
CPPFLAGS += -DUSE_MONITORING
endif

ifndef LONG_DEBUG_MESSAGE
CPPFLAGS += -DLOG_BUFFER_LEN=2048
endif

ifeq ($(PROPAGATE_UNPARSED_REPLY_HEADERS),yes)
CPPFLAGS += -DPROPAGATE_UNPARSED_REPLY_HEADERS
endif

# Additions for Solaris support.
ifeq ($(OS),solaris)
	CPPFLAGS += -DHAVE_SYS_SOCKIO_H -DBSD_COMP -fPIC -include compat/solaris.h
        CFLAGS += -DHAVE_SYS_SOCKIO_H -DBSD_COMP -fPIC -include compat/solaris.h
        # For nanosleep.
        LDFLAGS += -lrt
        # For inet_aton.
        LDFLAGS += -lresolv

	# I don't have libspeex installed.
	# binrpcctrl does some really weird header stuff that doesn't work.
	exclude_core_modules += binrpcctrl ilbc speex
endif




# fix sparc -> sparc64
ifeq ($(ARCH),sparc)
	ifeq ($(shell uname -m),sun4u)
		ARCH := sparc64
	endif
endif

# need OS specific for this ?
CXX = g++
CC = gcc
LD = $(CC)

CXXFLAGS += -Wall -Wno-reorder -Wno-unused -fPIC -g $(EXTRA_CXXFLAGS) 
CFLAGS   += -Wall -fPIC -g $(EXTRA_CFLAGS)

# only optimize if releasing, as it slows down the build process
ifneq ($(debug),yes)
	CXXFLAGS += -O2
	CFLAGS += -O2
endif

ifeq ($(DEBUG_PLAYOUT), yes)
CPPFLAGS +=  -DDEBUG_PLAYOUTBUF
endif

ifdef WITH_ZRTP
CPPFLAGS += -DWITH_ZRTP \
	    -DBUILD_ZRTP_MUTEXES \
	    -DBUILD_DEFAULT_CACHE -DBUILD_DEFAULT_TIMER -DUNIX -DBUILD_ZRTP_MUTEXES \
	    -I/usr/local/include/zrtp 
endif

TARGET=
LIB_LDFLAGS = -shared

ifeq ($(LD), clang)
LDFLAG_DYN=
LDFLAG_PTHREAD=
else
LDFLAG_DYN = -rdynamic
LDFLAG_PTHREAD = -lpthread
endif

ifeq ($(OS), linux)
	LDFLAGS +=  -ldl $(LDFLAG_DYN) $(LDFLAG_PTHREAD)
	CPPFLAGS += -DPROPAGATE_COREDUMP_SETTINGS
else
ifeq ($(OS), freebsd)
	LDFLAGS += $(LDFLAG_DYN) $(LDFLAG_PTHREAD)
else
ifeq ($(OS), openbsd)
	LDFLAGS += $(LDFLAG_DYN) $(LDFLAG_PTHREAD)
else
ifeq ($(OS), netbsd)
	LDFLAGS += $(LDFLAG_DYN) $(LDFLAG_PTHREAD)
else
ifeq ($(OS), macosx)
	LDFLAGS += $(LDFLAG_DYN) $(LDFLAG_PTHREAD)
	LIB_LDFLAGS = -flat_namespace -undefined suppress -bundle
	CXXFLAGS += -fno-common
	CFLAGS += -fno-common

	#necessary for sa_len|ss_len|sin_len
	# may be needed on other *BSD
	CPPFLAGS += -DBSD44SOCKETS

	# add the DarwinPorts directory
	ifneq ($(ARCH), iphone)
	      CPPFLAGS += -D__DARWIN_UNIX03
	      CPPFLAGS += -I /opt/local/include
	      LDFLAGS += -L/opt/local/lib
	endif	      
else
ifeq ($(OS), solaris)
       	LDFLAGS+= -fPIC -ldl -lsocket -lnsl $(LDFLAG_PTHREAD)
	TARGET=solaris
	# These don't build on Solaris because of missing Python, invalid make args, or other.
	exclude_app_modules += py_sems py_comp pin_collect mailbox ivr diameter_client conf_auth mp3 cc_acc_xmlrpc xmlrpc2di

endif
endif
endif
endif
endif
endif

LDFLAGS += -lstdc++ 

ifeq ($(ARCH),iphone)
LDFLAGS += -lgcc_eh
endif

ifdef WITH_ZRTP
LDFLAGS +=-lzrtp -lbn
endif

LIB_LDFLAGS += $(LDFLAGS)


# install path is $(basedir) $(prefix) 
# example:
#  creating a bin. archive in /tmp, which unpacks in /usr/local
#  basedir=/tmp
#  prefix=/usr/local

DESTDIR ?= 
PREFIX ?= /usr/local
prefix ?= $(PREFIX)
BASEDIR ?=
basedir = $(BASEDIR)

# Redhat users should use sems.redhat instead of sems
# the file will be to $(bin-prefix)/$(bin-dir)/sems copied.
start-script = sems.redhat

# install prefixes for various stuff
cfg-prefix = $(basedir)$(prefix)
bin-prefix = $(basedir)$(prefix)
modules-prefix = $(basedir)$(prefix)
doc-prefix = $(basedir)$(prefix)
audio-prefix = $(basedir)$(prefix)
lib-prefix = $(basedir)$(prefix)

# dirs
cfg-dir = etc/$(APP_NAME)/

bin-dir = sbin/
modules-dir = lib/$(APP_NAME)/plug-in/
audio-dir = lib/$(APP_NAME)/audio/
lib-dir = lib/$(APP_NAME)/

ifeq ($(OS), linux)
	doc-dir = share/doc/$(APP_NAME)/
	man-dir = share/man/
else
ifeq ($(OS), freebsd)
	doc-dir = share/doc/$(APP_NAME)/
	man-dir = man/
else
ifeq ($(OS), openbsd)
	doc-dir = share/doc/$(APP_NAME)/
	man-dir = man/
else
ifeq ($(OS), netbsd)
	doc-dir = share/doc/$(APP_NAME)
	man-dir = man/
else
	doc-dir = doc/$(APP_NAME)/
	man-dir = man/
endif
endif
endif
endif

# target dirs for various stuff
bin-target = $(prefix)/$(bin-dir)
cfg-target ?= $(prefix)/$(cfg-dir)
app-cfg-target = $(cfg-target)etc/
modules-target = $(prefix)/$(modules-dir)
audio-target = $(prefix)/$(audio-dir)
lib-target = $(prefix)/$(lib-dir)

ifeq ($(OS), solaris)
#use GNU versions
INSTALL ?= ginstall
TAR ?= gtar
else
INSTALL ?= install 
TAR ?= tar
endif

INSTALL-TOUCH = touch          # used to create the file first (good to 
                               #  make solaris install work)
INSTALL-CFG = $(INSTALL) -m 644
INSTALL-BIN = $(INSTALL) -m 755
INSTALL-MODULES = $(INSTALL) -m 755
INSTALL-DOC = $(INSTALL) -m 644
INSTALL-AUDIO = $(INSTALL) -m 644

#export stuff to sub-makes
export REL_VERSION RELEASE OS ARCH
export CPPFLAGS CXXFLAGS LDFLAGS CFLAGS LIB_LDFLAGS
export CXX CC LD
export DESTDIR PREFIX prefix basedir ser-prefix
export start-script
export cfg-prefix bin-prefix modules-prefix doc-prefix audio-prefix lib-prefix
export cfg-dir bin-dir modules-dir audio-dir lib-dir doc-dir man-dir
export bin-target cfg-target app-cfg-target modules-target audio-target lib-target
export INSTALL TAR INSTALL-TOUCH INSTALL-CFG INSTALL-BIN INSTALL-MODULES INSTALL-DOC INSTALL-AUDIO
export APP_NAME

export USE_SPANDSP LIBSPANDSP_STATIC LIBSPANDSP_LDIR 
export USE_LIBSAMPLERATE USE_INTERNAL_RESAMPLER
export WITH_ZRTP
export USE_MONITORING
export exclude_core_modules exclude_app_modules
endif  # ifeq ($(makefile_defs, 1)

mk-install-dirs: $(DESTDIR)$(cfg-target) \
		 $(DESTDIR)$(app-cfg-target) \
		 $(DESTDIR)$(bin-prefix)/$(bin-dir) \
		 $(DESTDIR)$(modules-prefix)/$(modules-dir) \
		 $(DESTDIR)$(audio-prefix)/$(audio-dir) \
		 $(DESTDIR)$(doc-prefix)/$(doc-dir)

$(DESTDIR)$(cfg-target): 
	mkdir -p $(DESTDIR)$(cfg-target)

$(DESTDIR)$(app-cfg-target): 
	mkdir -p $(DESTDIR)$(app-cfg-target)

$(DESTDIR)$(bin-prefix)/$(bin-dir):
	mkdir -p $(DESTDIR)$(bin-prefix)/$(bin-dir)

$(DESTDIR)$(modules-prefix)/$(modules-dir):
	mkdir -p $(DESTDIR)$(modules-prefix)/$(modules-dir)

$(DESTDIR)$(audio-prefix)/$(audio-dir):
	mkdir -p $(DESTDIR)$(audio-prefix)/$(audio-dir)

$(DESTDIR)$(doc-prefix)/$(doc-dir):
	mkdir -p $(DESTDIR)$(doc-prefix)/$(doc-dir)