#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# This file is public domain software, originally written by Joey Hess. 

# Uncomment this to turn on verbose mode.
# export DH_VERBOSE=1
export DH_OPTIONS
export DH_COMPAT=4
export DEB_DH_INSTALL_SOURCEDIR=debian/tmp
export PASSTHRU_CODEC_MODULES=codecs/mod_g729 codecs/mod_g723_1 codecs/mod_amr
export SPIDERMONKEY_MODULES=languages/mod_spidermonkey languages/mod_spidermonkey_core_db languages/mod_spidermonkey_odbc \
			    languages/mod_spidermonkey_socket languages/mod_spidermonkey_teletone
export APPLICATIONS_MODULES=applications/mod_commands applications/mod_conference applications/mod_dptools applications/mod_enum \
			    applications/mod_esf applications/mod_expr applications/mod_fifo applications/mod_limit \
			    applications/mod_rss applications/mod_voicemail applications/mod_fsv \
			    applications/mod_fax
export ASR_TTS_MODULES=asr_tts/mod_openmrcp
export CODECS_MODULES=codecs/mod_ilbc codecs/mod_h26x codecs/mod_speex codecs/mod_voipcodecs
export DIALPLANS_MODULES=dialplans/mod_dialplan_asterisk dialplans/mod_dialplan_directory dialplans/mod_dialplan_xml
export DIRECTORIES_MODULES=
export DOTNET_MODULES=
export ENDPOINTS_MODULES=endpoints/mod_dingaling endpoints/mod_iax endpoints/mod_portaudio endpoints/mod_sofia \
			 endpoints/mod_woomera endpoints/mod_loopback \
			 ../../libs/openzap/mod_openzap
export EVENT_HANDLERS_MODULES=event_handlers/mod_event_multicast event_handlers/mod_event_socket event_handlers/mod_cdr_csv
export FORMATS_MODULES=formats/mod_local_stream formats/mod_native_file formats/mod_sndfile formats/mod_tone_stream formats/mod_shout
export LANGUAGES_MODULES=languages/mod_perl languages/mod_lua
export LOGGERS_MODULES=loggers/mod_console loggers/mod_logfile loggers/mod_syslog
export SAY_MODULES=say/mod_say_en say/mod_say_it say/mod_say_de say/mod_say_fr say/mod_say_es say/mod_say_nl
export TIMERS_MODULES=
export DISABLED_MODULES=applications/mod_soundtouch directories/mod_ldap languages/mod_java languages/mod_python \
			languages/mod_spidermonkey_skel ast_tts/mod_cepstral asr_tts/mod_lumenvox endpoints/mod_wanpipe \
			event_handlers/mod_event_test event_handlers/mod_radius_cdr event_handlers/mod_zeroconf
export XML_INT_MODULES=xml_int/mod_xml_rpc  xml_int/mod_xml_curl xml_int/mod_xml_cdr
export MYMODULES=$(PASSTHRU_CODEC_MODULES) $(SPIDERMONKEY_MODULES) $(APPLICATIONS_MODULES) $(ASR_TTS_MODULES) $(CODECS_MODULES) $(DIALPLANS_MODULES) $(DIRECTORIES_MODULES) $(DOTNET_MODULES) $(ENDPOINTS_MODULES) $(EVENT_HANDLERS_MODULES) $(FORMATS_MODULES) $(LANGUAGES_MODULES) $(LOGGERS_MODULES) $(SAY_MODULES) $(TIMERS_MODULES) $(XML_INT_MODULES)
export MODULES=$(MYMODULES)

ifndef AUTOCONF
	AUTOCONFS := $(wildcard /usr/bin/autoconf*)
	AUTOCONF = $(shell which autoconf)
ifneq (,$(findstring 2.50,${AUTOCONFS}))
	AUTOCONF = /usr/bin/autoconf2.50
endif
endif

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS = -ggdb

FEATURES = --enable-core-libedit-support --enable-core-odbc-support --with-libcurl --with-openssl

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	FEATURES += --disable-optimization
endif

config.status: configure
	dh_testdir
	rm -f modules.conf
	touch modules.conf
	echo $(MODULES)
	for i in $(MODULES); do echo $$i >> modules.conf; done
	./configure --prefix=/opt/freeswitch --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) ${FEATURES}

configure:
	if test ! -f Makefile.in ;  then \
	    if test -f ./bootstrap.sh ; then \
		AUTOCONF=${AUTOCONF} ./bootstrap.sh ; \
	    else \
		echo "No Makefile.in and no known way to bootstrap." ; \
		exit 1; \
	    fi; \
	fi;

build: build-stamp
build-stamp:  config.status
	dh_testdir

	$(MAKE)

	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 

	-$(MAKE) update-clean
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub build/config/config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess build/config/config.guess
endif
	rm -f config.status
	rm -f */*/config.status
	rm -f */*/*/config.status
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs -A --sourcedir=debian/tmp

	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install uhd-sounds-install moh-install


# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
#	dh_installchangelogs
#	dh_installdocs
#	dh_installexamples
#	dh_install
#	dh_installmenu
#	dh_installdebconf	
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installcatalogs
#	dh_installpam
#	dh_installmime
	dh_installinit --sourcedir=debian/tmp
#	dh_installcron
#	dh_installinfo
#	dh_undocumented
#	dh_installman
	cp debian/monit/freeswitch.monitrc debian/freeswitch/etc/monit/freeswitch.monitrc
	dh_install --sourcedir=debian/tmp
	dh_link
	dh_strip --dbg-package=freeswitch
	dh_compress
	dh_fixperms --exclude /opt/freeswitch/conf
#	dh_perl
#	dh_python
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps --exclude=/opt/freeswitch
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install