#!/usr/bin/make -f
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# Starting with dpkg-dev >= 1.16.1.1 you can use the new buildflags.mk file
# to set all *FLAGS
# When building programs that handle untrusted data
# (parsers, network listeners, etc.),
#  or run with elevated privileges (PAM, X, etc.),
#  please enable "PIE" and "BINDNOW" in the build.
#  The "all" option enables "PIE" and "BINDNOW" and future hardening flags

# uncomment the next line to create a "MALLOC_DEBUG" version of asterisk
MENUSELECT_EXTRA_ARGS := --enable MALLOC_DEBUG
# uncomment the next 2 lines to create a "debug" version of asterisk
MENUSELECT_EXTRA_ARGS += --enable DONT_OPTIMIZE
#MENUSELECT_EXTRA_ARGS += --enable DEBUG_THREADS
# ...and the next line to be able to use the asterisk test framework
#MENUSELECT_EXTRA_ARGS += --enable TEST_FRAMEWORK

PKG  = $(word 2,$(shell dpkg-parsechangelog | grep Source))
VER ?= $(shell dpkg-parsechangelog | grep Version: | awk -F': ' '{print $$NF}' | sed -ne 's/^\(\([0-9]\+\):\)\?\(.*\)-.*/\3/p')
ASTERISK_URL_DOWNLOAD = "http://downloads.asterisk.org/pub/telephony/asterisk/releases/asterisk-${VER}.tar.gz"

%:
	dh $@

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  confflags += --build $(DEB_HOST_GNU_TYPE)
else
  confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

export PROC := $(shell dpkg-architecture -qDEB_BUILD_GNU_CPU)

# don't let asterisk mess with build flags
BUILDFLAGS += AST_FORTIFY_SOURCE= DEBUG= OPTIMIZE=

# show full gcc arguments instead of [CC] and [LD]
BUILDFLAGS += NOISY_BUILD=yes

BUILDFLAGS += ASTDATADIR=/usr/share/asterisk ASTVARRUNDIR=/run/asterisk

include /usr/share/dpkg/pkg-info.mk
DEBVERSION:=$(DEB_VERSION)
DEB_NOEPOCH_VERSION:=$(shell echo $(DEBVERSION) | cut -d':' -f 2)

# make sure we have 'fetch' . We need to have either wget or fetch
# on the system. However it is generally not a good idea to actually
# get remote tarballs at build time. So if neither wget nor fetch 
# happen to be installed, the configure script will find a dummy
# fetch script that always returns an error.
FETCH_ENV = PATH=$$PATH:$(CURDIR)/debian/dummyprogs

override_dh_autoreconf:
	if [ ! -r configure.debian_sav ]; then cp -a configure configure.debian_sav; fi
	[ -f .version.debian_sav ] || cp -a .version .version.debian_sav
	echo $(DEB_NOEPOCH_VERSION) > .version
	dh_autoreconf -- ./bootstrap.sh

# Make sure the configure script gets an CFLAGS parameter. Otherwise
# it will build with -march=native
# Pass CPPFLAGS in the CFLAGS as otherwise the build system will
# ignore them.
override_dh_auto_configure:
	chmod 755 $(CURDIR)/debian/dummyprogs/fetch
	$(FETCH_ENV) ./configure			\
		CFLAGS="-fgnu89-inline $(CFLAGS) $(CPPFLAGS)" LDFLAGS="$(LDFLAGS) -Wl,--as-needed" \
		--host=$(DEB_HOST_GNU_TYPE)		\
		--build=$(DEB_BUILD_GNU_TYPE)		\
		--prefix=/usr				\
		--mandir=\$${prefix}/share/man		\
		--infodir=\$${prefix}/share/info	\
		--with-imap=system			\
		--with-jansson-bundled

override_dh_auto_build:
	$(MAKE) menuselect.makeopts BUILD_CFLAGS="$(CFLAGS) $(CPPFLAGS)" BUILD_LDFLAGS="$(LDFLAGS)"
	menuselect/menuselect \
		--disable-category MENUSELECT_ADDONS \
		--disable-category MENUSELECT_MOH \
		--disable BUILD_NATIVE \
		--disable chan_dahdi \
		--disable codec_dahdi \
		--disable res_timing_dahdi \
		--disable app_flash \
		--enable codec_opus \
		--enable res_mwi_external \
		--enable res_stasis_mailbox \
		--enable res_ari_mailboxes \
		--enable CORE-SOUNDS-FR-GSM \
		--enable channelstorage_cpp_map_name_id \
		$(MENUSELECT_EXTRA_ARGS) \
		menuselect.makeopts
	$(FETCH_ENV) dh_auto_build -- $(BUILDFLAGS)
	mkdir -p doc/json
	wazo-asterisk-doc-extractor doc/core-en_US.xml > doc/json/pjsip.json

override_dh_clean:
	[ ! -f .version.debian_sav ] || mv .version.debian_sav .version
	if [ -f configure.debian_sav ]; then mv configure.debian_sav configure; fi
	$(RM) -f include/asterisk/.gitignore
	$(RM) -f include/asterisk/autoconfig.h.in*
	dh_clean

REMOVED_PROTO_CONFS = dundi mgcp skinny unistim
REMOVED_PROTO_FILES = $(REMOVED_PROTO_CONFS:%=$(CURDIR)/debian/tmp/etc/asterisk/%.conf)

override_dh_auto_install:
	$(FETCH_ENV) dh_auto_install -- $(BUILDFLAGS) -j1 config samples
	cp -a configs $(CURDIR)/debian/tmp/usr/share/asterisk/conf
	$(RM) -f $(REMOVED_PROTO_FILES)
	mkdir -p $(CURDIR)/debian/tmp/usr/bin/
	mkdir -p $(CURDIR)/debian/tmp/usr/share/man/man1
	$(RM) -f $(CURDIR)/debian/tmp/usr/sbin/conf2ael
	$(RM) -f $(CURDIR)/debian/tmp/usr/sbin/muted
	$(RM) -f $(CURDIR)/debian/tmp/usr/sbin/streamplayer
	$(RM) -f $(CURDIR)/debian/tmp/usr/sbin/stereorize
	$(RM) -f $(CURDIR)/debian/tmp/usr/sbin/hashtest*
	$(RM) -f $(CURDIR)/debian/tmp/usr/sbin/refcounter

override_dh_strip:
	dh_strip -a --dbgsym-migration='asterisk-debug-dbg (<< 8:20.1.0-1~wazo2.deb10)'

override_dh_systemd_enable:
	dh_systemd_enable --name=asterisk

override_dh_installinit:
	dh_installinit --name=asterisk

.PHONY: get-orig-source

get-orig-source:
	@echo "# Downloading..."
	wget -nv -T10 -t3 $(ASTERISK_URL_DOWNLOAD) -O ../$(PKG)_$(VER).orig.tar.gz
