#!/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

%:
	dh "$@" --with autotools_dev

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

# Sanity check for a stable release:
SAVED_ABI_HASH = fa819827cbff2ea35341af5458859233

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

# We copy around a number of things in case we're linux:
BUILD_OS=$(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)

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=/var/run/asterisk

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	ENABLE_DEBUG += --enable-dev-mode
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

DEBVERSION:=$(shell dpkg-parsechangelog | sed -n -e 's/Version: //p')
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

# resolve if release is experimental
EXP_RELEASE := $(if $(shell dpkg-parsechangelog | grep '^Distribution: \(experimental\|UNRELEASED\)'),exp)

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)" \
		--host=$(DEB_HOST_GNU_TYPE)		\
		--build=$(DEB_BUILD_GNU_TYPE)		\
		--prefix=/usr				\
		--mandir=\$${prefix}/share/man		\
		--infodir=\$${prefix}/share/info	\
		--disable-asteriskssl			\
		--with-gsm				\
		--with-imap=system			\
		--without-pwlib				\
		$(if $(EXP_RELEASE),,--without-sdl) \
		${ENABLE_DEBUG} || { cat config.log; exit 1; }

#		--with-cap \

override_dh_auto_build:
	$(MAKE) menuselect.makeopts BUILD_CFLAGS="$(CFLAGS) $(CPPFLAGS)" BUILD_LDFLAGS="$(LDFLAGS)"
	menuselect/menuselect --enable codec_opus_open_source menuselect.makeopts
	@if [ "x${ENABLE_DEBUG}" != "x" ] ; then \
		menuselect/menuselect --enable BETTER_BACKTRACES menuselect.makeopts ; \
		menuselect/menuselect --enable DEBUG_THREADS menuselect.makeopts ; \
	fi
	$(FETCH_ENV) dh_auto_build -- $(BUILDFLAGS)
	# Sanity check: don't break the ABI for modules:
	@abi_hash=`awk -F'"' '/AST_BUILDOPT_SUM/ {print $$2}' include/asterisk/buildopts.h`; \
	  if [ "$$abi_hash" != "$(SAVED_ABI_HASH)" ]; then \
	    echo "Debian packaging problem: ABI checksum changed."; \
	    echo "  Was: $(SAVED_ABI_HASH)"; \
	    echo "  Now: $$abi_hash"; \
	    echo "Check include/asterisk/buildopts.h"; \
	    [ -n "$(EXP_RELEASE)" ] || exit 1; \
	  fi

SND_FILE=asterisk-core-sounds-en-gsm-1.4.20.tar.gz
override_dh_clean:
	if [ -r sounds/$(SND_FILE) ]; then mv sounds/$($SND_FILE) debian/; fi
	GREP=grep AWK=awk $(MAKE) distclean
	if [ -r debian/$(SND_FILE) ]; then mv debian/$(SND_FILE) sounds/; fi

	[ ! -f .version.debian_sav ] || mv .version.debian_sav .version
	if [ -f configure.debian_sav ]; then mv configure.debian_sav configure; fi

	dh_clean

override_dh_gencontrol:
	AST_BUILDOPT_SUM=`grep AST_BUILDOPT_SUM include/asterisk/buildopts.h  | sed -e 's/.\+ "\(.\+\)\"/\1/g'`; \
		dh_gencontrol -- -Vasterisk:ABI=$$AST_BUILDOPT_SUM

SUBPACKS_EXTRA = \
		 voicemail voicemail-odbcstorage voicemail-imapstorage \
		 ooh323 mysql mp3
ifeq (linux,$(BUILD_OS))
SUBPACKS_EXTRA += dahdi mobile vpb
endif
SUBPACKS_EXTRA_DIRS = $(SUBPACKS_EXTRA:%=$(CURDIR)/debian/asterisk-%)
SUBPACKS_EXTRA_DIRS_MOD = $(SUBPACKS_EXTRA_DIRS:%=%/usr/lib/asterisk/modules)

override_dh_auto_install:
	$(FETCH_ENV) dh_auto_install -- $(BUILDFLAGS) config samples
	cp -a configs $(CURDIR)/debian/tmp/usr/share/asterisk/conf
	mkdir -p $(CURDIR)/debian/tmp/usr/bin/
	mkdir -p $(CURDIR)/debian/tmp/usr/share/man/man1
	mkdir -p $(CURDIR)/debian/tmp/usr/share/dahdi/span_config.d
	cp -a debian/asterisk-config-custom $(CURDIR)/debian/tmp/usr/bin/
	cp -a debian/asterisk-config-custom $(CURDIR)/debian/tmp/usr/share/man/man1/
	cp -a debian/50-asterisk $(CURDIR)/debian/tmp/usr/share/dahdi/span_config.d/
	$(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_install-arch:
	dh_install
	extra_packs=`find $(SUBPACKS_EXTRA_DIRS_MOD) -name '*.so' -printf '%f\n'`\
		    ; cd $(CURDIR)/debian/asterisk-modules/usr/lib/asterisk/modules \
		    && rm -f $$extra_packs

	# Rename dh_install file for -imapstorage and -odbcstorage.
	mv $(CURDIR)/debian/asterisk-voicemail-imapstorage/usr/lib/asterisk/modules/app_voicemail_imapstorage.so \
		$(CURDIR)/debian/asterisk-voicemail-imapstorage/usr/lib/asterisk/modules/app_voicemail.so
	mv $(CURDIR)/debian/asterisk-voicemail-odbcstorage/usr/lib/asterisk/modules/app_voicemail_odbcstorage.so \
		$(CURDIR)/debian/asterisk-voicemail-odbcstorage/usr/lib/asterisk/modules/app_voicemail.so

override_dh_fixperms:
	dh_fixperms
	# should follow dh_fixperms; asterisk configurations may contain
	# sensitive information, such as passwords
	if [ -d $(CURDIR)/debian/asterisk-config ]; then \
		chmod o-rwx $(CURDIR)/debian/asterisk-config/etc/asterisk/*; \
		chmod o+rx  $(CURDIR)/debian/asterisk-config/etc/asterisk/manager.d; \
	fi

override_dh_strip:
	dh_strip -a --dbgsym-migration='asterisk-dbg (<< 1:13.12.1~dfsg-1~)'

# .PHONY: binary-indep binary-arch binary install
