# Makefile
# This file holds the make rules for the Telephony Engine modules

# override DESTDIR at install time to prefix the install directory
DESTDIR :=

# override DEBUG at compile time to enable full debug or remove it all
DEBUG :=

CC  := @CC@ -Wall
CXX := @CXX@ -Wall
MOC := @QT4_MOC@
QT4_INC := @QT4_INC@
QT4_LIB := @QT4_LIB@
QT4_INC_NET := @QT4_INC_NET@
QT4_LIB_NET := @QT4_LIB_NET@
SED := sed
DEFS :=
INCLUDES := -I.. -I@top_srcdir@
CFLAGS := @CFLAGS@ @MODULE_CFLAGS@ @INLINE_FLAGS@
CPPFLAGS := @CFLAGS@ @MODULE_CPPFLAGS@ @INLINE_FLAGS@
LDFLAGS:= @LDFLAGS@
YATELIBS:= -L.. -lyate @LIBS@
MODFLAGS:= @MODULE_LDFLAGS@
MODRELAX:= @MODULE_LDRELAX@
MODSTRIP:= @MODULE_SYMBOLS@
INCFILES := @top_srcdir@/yateclass.h @top_srcdir@/yatengine.h @top_srcdir@/yatephone.h ../yateversn.h

SUBDIRS :=
MKDEPS  := ../config.status
PROGS := cdrbuild.yate cdrfile.yate regexroute.yate \
	tonegen.yate tonedetect.yate wavefile.yate \
	extmodule.yate conference.yate moh.yate pbx.yate \
	dumbchan.yate callfork.yate mux.yate \
	yrtpchan.yate ystunchan.yate \
	ysipchan.yate \
	yiaxchan.yate \
	yjinglechan.yate jabber/jabberserver.yate jabber/jbfeatures.yate \
	ysockschan.yate filetransfer.yate \
	server/pbxassist.yate server/dbpbx.yate server/lateroute.yate \
	server/park.yate server/queues.yate server/queuesnotify.yate \
	server/regfile.yate server/accfile.yate server/register.yate \
	server/callcounters.yate \
	server/dbwave.yate \
	server/yradius.yate \
	server/sipfeatures.yate \
	server/heartbeat.yate server/clustering.yate \
	server/mgcpgw.yate server/mgcpca.yate \
	server/mrcpspeech.yate \
	server/ysigchan.yate \
	server/ciscosm.yate server/sigtransport.yate \
	server/presence.yate server/subscription.yate \
	server/users.yate \
	server/analog.yate server/analogdetect.yate \
	client/jabberclient.yate \
	callgen.yate analyzer.yate rmanager.yate msgsniff.yate
LIBS :=
DIRS := client server jabber qt4

ifneq (@HAVE_PGSQL@,no)
PROGS := $(PROGS) server/pgsqldb.yate
endif

ifneq (@HAVE_MYSQL@,no)
PROGS := $(PROGS) server/mysqldb.yate
endif

ifneq (@HAVE_RESOLV@,no)
PROGS := $(PROGS) enumroute.yate
endif

ifneq (@HAVE_SOUNDCARD@,no)
PROGS := $(PROGS) client/osschan.yate
endif

ifneq (@HAVE_ALSA@,no)
PROGS := $(PROGS) client/alsachan.yate
endif

ifneq (@HAVE_COREAUDIO@,no)
PROGS := $(PROGS) client/coreaudio.yate
endif

ifneq (@HAVE_QT4@,no)
PROGS := $(PROGS) qt4/updater.yate
endif

ifneq (@HAVE_ZAP@,no)
PROGS := $(PROGS) server/zapcard.yate
endif

ifneq (@HAVE_WANPIPE@,no)
ifeq (@BUILD_WPCARD@,yes)
PROGS := $(PROGS) server/wpcard.yate
endif
ifeq (@HAVE_WANPIPE_API@,yes)
ifeq (@BUILD_TDMCARD@,yes)
PROGS := $(PROGS) server/tdmcard.yate
endif
endif
endif

ifneq (@HAVE_SCTP_NETINET@,no)
PROGS := $(PROGS) server/lksctp.yate
endif

ifneq (@HAVE_SPANDSP@,no)
PROGS := $(PROGS) faxchan.yate
endif

ifneq (@HAVE_H323@,no)
PROGS := $(PROGS) h323chan.yate
endif

ifneq (@HAVE_GSM@,no)
PROGS := $(PROGS) gsmcodec.yate
endif

ifneq (@HAVE_ILBC@,no)
PROGS := $(PROGS) ilbccodec.yate
endif

ifneq (@HAVE_SPEEX@,no)
PROGS := $(PROGS) speexcodec.yate
endif

ifneq (@HAVE_AMRNB@,no)
PROGS := $(PROGS) amrnbcodec.yate
endif

ifneq (@HAVE_OPENSSL@,no)
PROGS := $(PROGS) openssl.yate
endif

ifeq (@HAVE_COREDUMPER@,yes)
COREDUMP_INC := -DHAVE_COREDUMPER @COREDUMPER_INC@
COREDUMP_LIB := @COREDUMPER_LIB@
endif

LOCALFLAGS =
LOCALLIBS =
CCOMPILE = $(CC) $(DEFS) $(DEBUG) $(INCLUDES) $(CFLAGS)
COMPILE = $(CXX) $(DEFS) $(DEBUG) $(INCLUDES) $(CPPFLAGS)
LINK = $(CXX) $(LDFLAGS)
MODLINK = $(CXX) $(MODFLAGS) $(MODSTRIP) $(LDFLAGS)
MODCOMP = $(COMPILE) $(MODFLAGS) $(MODSTRIP) $(LDFLAGS)

prefix = @prefix@
exec_prefix = @exec_prefix@
moddir = @libdir@/yate

# include optional local make rules
-include YateLocal.mak

.PHONY: all debug ddebug xdebug
all: subdirs do-all $(LIBS) $(PROGS)

debug:
	$(MAKE) all DEBUG=-g3 MODSTRIP=

ddebug:
	$(MAKE) all DEBUG='-g3 -DDEBUG' MODSTRIP=

xdebug:
	$(MAKE) all DEBUG='-g3 -DXDEBUG' MODSTRIP=

.PHONY: strip
strip: all do-strip
	strip --strip-debug --discard-locals $(PROGS)

.PHONY: clean
clean: do-clean
	@-$(RM) $(PROGS) $(LIBS) *.o qt4/*.moc core 2>/dev/null
	@-for i in $(PROGS) ; do \
	    $(RM) -rf $$i.dSYM 2>/dev/null; \
	done;

.PHONY: install
install: all do-install
	@mkdir -p "$(DESTDIR)$(moddir)/" && \
	for i in $(DIRS) ; do \
	    mkdir -p "$(DESTDIR)$(moddir)/$$i" ; \
	done; \
	for i in $(PROGS) ; do \
	    @INSTALL_D@ "$$i" "$(DESTDIR)$(moddir)/$$i" ; \
	done;

.PHONY: uninstall
uninstall: do-uninstall
	@-for i in $(PROGS) ; do \
	    rm "$(DESTDIR)$(moddir)/$$i" ; \
	done; \
	for i in $(DIRS) $(SUBDIRS) ; do \
	    rmdir "$(DESTDIR)$(moddir)/$$i" ; \
	done;
	@-rmdir "$(DESTDIR)$(moddir)"

.PHONY: subdirs
subdirs:
	@mkdir -p $(DIRS)

%.o: @srcdir@/%.cpp $(MKDEPS) $(INCFILES)
	$(COMPILE) -c $<

do-all do-strip do-clean do-install do-uninstall:
	$(if $(SUBDIRS),\
	@target=`echo $@ | $(SED) -e 's/^do-//'`; \
	for i in $(SUBDIRS) ; do \
	    if test -f ./$$i/Makefile ; then \
		$(MAKE) -C ./$$i $${target} || exit 1;\
	    fi; \
	done \
	)

Makefile: @srcdir@/Makefile.in $(MKDEPS)
	cd .. && ./config.status

lib%.so: %.o
	$(LINK) -shared -o $@ $^ $(YATELIBS)

server/%.yate: @srcdir@/server/%.cpp $(MKDEPS) $(INCFILES)
	$(MODCOMP) -o $@ $(LOCALFLAGS) $< $(LOCALLIBS) $(YATELIBS)

client/%.yate: @srcdir@/client/%.cpp $(MKDEPS) $(INCFILES)
	$(MODCOMP) -o $@ $(LOCALFLAGS) $< $(LOCALLIBS) $(YATELIBS)

qt4/%.yate: @srcdir@/qt4/%.cpp $(MKDEPS) $(INCFILES)
	$(MODCOMP) -o $@ $(LOCALFLAGS) $(QT4_INC) -I@top_srcdir@/clients/qt4 $< $(LOCALLIBS) $(QT4_LIB) $(YATELIBS)

qt4/%.moc: @srcdir@/qt4/%.h $(MKDEPS) $(INCFILES)
	mkdir -p qt4 && $(MOC) $(DEFS) $(INCLUDES) $(QT4_INC) -I@top_srcdir@/clients/qt4 -I@srcdir@/qt4 -o $@ $<

%.yate: @srcdir@/%.cpp $(MKDEPS) $(INCFILES)
	$(MODCOMP) -o $@ $(LOCALFLAGS) $< $(LOCALLIBS) $(YATELIBS)

# Take special care of the modules that depend on optional libs

server/ysigchan.yate server/wpcard.yate server/tdmcard.yate server/zapcard.yate server/analog.yate server/ciscosm.yate server/sigtransport.yate: ../libyatesig.so
server/ysigchan.yate server/analog.yate server/ciscosm.yate server/sigtransport.yate: LOCALFLAGS = -I@top_srcdir@/libs/ysig
server/wpcard.yate server/tdmcard.yate: LOCALFLAGS = -I@top_srcdir@/libs/ysig @WANPIPE_FLAGS@
server/zapcard.yate: LOCALFLAGS = -I@top_srcdir@/libs/ysig @ZAP_FLAGS@
server/ysigchan.yate server/wpcard.yate server/tdmcard.yate server/zapcard.yate server/analog.yate server/ciscosm.yate server/sigtransport.yate: LOCALLIBS = -lyatesig

server/analogdetect.yate: ../libs/ymodem/libyatemodem.a
server/analogdetect.yate: LOCALFLAGS = -I@top_srcdir@/libs/ymodem
server/analogdetect.yate: LOCALLIBS = -L../libs/ymodem -lyatemodem

h323chan.yate: LOCALFLAGS = -DPHAS_TEMPLATES -D_REENTRANT -DP_HAS_SEMAPHORES @H323_INC@
h323chan.yate: LOCALLIBS = @H323_LIB@

server/pgsqldb.yate: LOCALFLAGS = @PGSQL_INC@
server/pgsqldb.yate: LOCALLIBS = -lpq

server/mysqldb.yate: LOCALFLAGS = @MYSQL_INC@
server/mysqldb.yate: LOCALLIBS = @MYSQL_LIB@

enumroute.yate: LOCALLIBS = @RESOLV_LIB@

client/alsachan.yate: LOCALLIBS = -lasound

client/coreaudio.yate: LOCALLIBS = -framework CoreServices  -framework CoreAudio -framework AudioUnit -framework AudioToolBox

yiaxchan.yate: ../libs/yiax/libyateiax.a
yiaxchan.yate: LOCALFLAGS = -I@top_srcdir@/libs/yiax
yiaxchan.yate: LOCALLIBS = -L../libs/yiax -lyateiax

yjinglechan.yate jabber/jabberserver.yate jabber/jbfeatures.yate client/jabberclient.yate: ../libyatejabber.so
yjinglechan.yate jabber/jabberserver.yate jabber/jbfeatures.yate client/jabberclient.yate: LOCALFLAGS = -I@top_srcdir@/libs/yxml -I@top_srcdir@/libs/yjabber
yjinglechan.yate jabber/jabberserver.yate jabber/jbfeatures.yate client/jabberclient.yate: LOCALLIBS = -lyatejabber

server/dbpbx.yate server/pbxassist.yate: ../libs/ypbx/libyatepbx.a
server/dbpbx.yate server/pbxassist.yate: LOCALFLAGS = -I@top_srcdir@/libs/ypbx
server/dbpbx.yate server/pbxassist.yate: LOCALLIBS = -L../libs/ypbx -lyatepbx

server/mgcpca.yate: ../libyatemgcp.so ../libyatesig.so ../libs/ysdp/libyatesdp.a
server/mgcpca.yate: LOCALFLAGS = -I@top_srcdir@/libs/ymgcp -I@top_srcdir@/libs/ysig -I@top_srcdir@/libs/ysdp
server/mgcpca.yate: LOCALLIBS = -lyatemgcp -lyatesig -L../libs/ysdp -lyatesdp

server/mgcpgw.yate: ../libyatemgcp.so
server/mgcpgw.yate: LOCALFLAGS = -I@top_srcdir@/libs/ymgcp
server/mgcpgw.yate: LOCALLIBS = -lyatemgcp

server/lksctp.yate: LOCALFLAGS = @SCTP_FLAGS@
server/lksctp.yate: LOCALLIBS = -lsctp

ilbccodec.yate: ../libs/ilbc/libilbc.a
ilbccodec.yate: LOCALLIBS = -L../libs/ilbc -lilbc
ilbccodec.yate: LOCALFLAGS = @ILBC_INC@

gsmcodec.yate: LOCALLIBS = @GSM_LIB@
gsmcodec.yate: LOCALFLAGS = @GSM_INC@

speexcodec.yate: LOCALLIBS = @SPEEX_LIB@
speexcodec.yate: LOCALFLAGS = @SPEEX_INC@

amrnbcodec.yate: LOCALFLAGS = @AMRNB_INC@
amrnbcodec.yate: LOCALLIBS = @AMRNB_LIB@

faxchan.yate: LOCALLIBS = @SPANDSP_LIB@
faxchan.yate: LOCALFLAGS = @SPANDSP_INC@

ysipchan.yate: ../libs/ysip/libyatesip.a ../libs/ysdp/libyatesdp.a
ysipchan.yate: LOCALFLAGS = -I@top_srcdir@/libs/ysip -I@top_srcdir@/libs/ysdp
ysipchan.yate: LOCALLIBS = -L../libs/ysip -lyatesip -L../libs/ysdp -lyatesdp

yrtpchan.yate: ../libs/yrtp/libyatertp.a
yrtpchan.yate: LOCALFLAGS = -I@top_srcdir@/libs/yrtp
yrtpchan.yate: LOCALLIBS = -L../libs/yrtp -lyatertp

openssl.yate: LOCALFLAGS = @OPENSSL_INC@
openssl.yate: LOCALLIBS = @OPENSSL_LIB@

rmanager.yate: LOCALFLAGS = $(COREDUMP_INC)
rmanager.yate: LOCALLIBS = $(COREDUMP_LIB)

qt4/updater.yate: qt4/updater.moc
qt4/updater.yate: LOCALFLAGS = $(QT4_INC_NET)
qt4/updater.yate: LOCALLIBS = $(QT4_LIB_NET)

server/sipfeatures.yate: ../libs/yxml/libyatexml.a
server/sipfeatures.yate: LOCALFLAGS = -I@top_srcdir@/libs/yxml
server/sipfeatures.yate: LOCALLIBS = -L../libs/yxml -lyatexml

../libyatesig.so ../libs/ysig/libyatesig.a:
	$(MAKE) -C ../libs/ysig

../libs/ilbc/libilbc.a:
	$(MAKE) -C ../libs/ilbc

../libs/ysip/libyatesip.a:
	$(MAKE) -C ../libs/ysip

../libs/yrtp/libyatertp.a:
	$(MAKE) -C ../libs/yrtp

../libs/ysdp/libyatesdp.a:
	$(MAKE) -C ../libs/ysdp

../libs/yiax/libyateiax.a:
	$(MAKE) -C ../libs/yiax

../libyatemgcp.so ../libs/ymgcp/libyatemgcp.a:
	$(MAKE) -C ../libs/ymgcp

../libs/ymodem/libyatemodem.a:
	$(MAKE) -C ../libs/ymodem

../libs/yxml/libyatexml.a:
	$(MAKE) -C ../libs/yxml

../libyatejabber.so ../libs/yjabber/libyatejabber.a:
	$(MAKE) -C ../libs/yjabber

../libs/ypbx/libyatepbx.a:
	$(MAKE) -C ../libs/ypbx
