#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-

PKG  = $(word 1,$(shell dpkg-parsechangelog --show-field source))
CHANGELOG_VER = $(shell dpkg-parsechangelog --show-field version)
SOURCE_VER ?= $(shell dpkg-parsechangelog --show-field version | sed -ne 's/^\(\([0-9]\+\):\)\?\(.*\)-.*/\3/p')

ASTERISK_URL_DOWNLOAD = "http://downloads.asterisk.org/pub/telephony/asterisk/releases/asterisk-20.19.0.tar.gz"

include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/autotools.mk

DEB_SHLIBDEPS_INCLUDE := $(CURDIR)/debian/tmp/usr/lib
DEB_DH_SHLIBDEPS_ARGS_ALL += -u--ignore-missing-info
DEB_MAKE_INVOKE = $(DEB_MAKE_ENVVARS) $(MAKE) -C $(DEB_BUILDDIR)
BUILD_TREE = .

# 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

# Asterisk needs
export PROC := $(shell dpkg-architecture -qDEB_BUILD_GNU_CPU)
# show full gcc arguments instead of [CC] and [LD]
DEB_MAKE_INVOKE += NOISY_BUILD=yes
# Force music files selection:
DEB_MAKE_INVOKE += MENUSELECT_MOH= \
  ASTDATADIR=/usr/share/asterisk \
  ASTVARRUNDIR=/var/run/asterisk \

# Debian needs
DEB_DH_INSTALLINIT_ARGS = --name asterisk -- defaults 30 70
DEB_CONFIGURE_EXTRA_FLAGS = --without-h323 --without-misdn --with-pjproject-bundled --with-jansson-bundled --enable-binary-modules
DEB_CONFIGURE_SCRIPT_ENV += $(FETCH_ENV)

# on the system. However it is generally not a good idea to actually
# get remote tarballs at build time. So if nither wget nor fetch
# happen to be installed, the configure script will find a dummy
# fetch script that always returns an error.
DEB_MAKE_ENVVARS = PATH=$$PATH:$(CURDIR)/debian/dummyprogs

configure/asterisk::
	# generate proper menuselect.makeopts
	(cd $(BUILD_TREE); \
		$(FETCH_ENV) $(MAKE) $(BUILDFLAGS) menuselect.makeopts)
	(cd $(BUILD_TREE); \
		menuselect/menuselect \
			--disable-category MENUSELECT_ADDONS \
			--disable-category MENUSELECT_MOH \
			--disable BUILD_NATIVE \
			--enable app_meetme \
			--enable res_mwi_external \
			--enable res_stasis_mailbox \
			--enable res_ari_mailboxes \
			--enable codec_opus \
			--enable chan_sip \
			--enable res_monitor \
			$(MENUSELECT_EXTRA_ARGS) \
			menuselect.makeopts \
	)
	# generate asterisk advertised version

	echo "$(CHANGELOG_VER)" > $(BUILD_TREE)/.version
	# DONE
	touch $(BUILD_TREE)/configure-stamp

build/asterisk::
	touch $(BUILD_TREE)/build-stamp

common-install-arch common-install-indep:: common-install-impl
common-install-impl::
	$(DEB_MAKE_ENVVARS) $(MAKE) -C $(BUILD_TREE) DESTDIR=$(CURDIR)/debian/tmp/ install install-headers samples

install/asterisk::
	$(DEB_MAKE_ENVVARS) $(MAKE) -C $(BUILD_TREE) DESTDIR=$(CURDIR)/debian/tmp/ install

binary-install/asterisk::
	mkdir -p $(CURDIR)/debian/$(cdbs_curpkg)/usr/share/asterisk/bin
	cp $(CURDIR)/debian/asterisk_fix $(CURDIR)/debian/$(cdbs_curpkg)/usr/share/asterisk/bin/
	chmod +x $(CURDIR)/debian/$(cdbs_curpkg)/usr/share/asterisk/bin/asterisk_fix

binary-install/asterisk::
	rm -rf debian/$(cdbs_curpkg)/var/spool/asterisk/voicemail/default
	rm -rf debian/$(cdbs_curpkg)/var/lib/asterisk/sounds/en
	mkdir -p debian/$(cdbs_curpkg)/usr/share/asterisk
	mv debian/tmp/var/lib/asterisk/documentation debian/$(cdbs_curpkg)/usr/share/asterisk/

install/asterisk-doc::
	dh_installexamples -p$(cdbs_curpkg) $(BUILD_TREE)/sample.call
	dh_installexamples -p$(cdbs_curpkg) $(BUILD_TREE)/configs/*
	dh_installexamples -p$(cdbs_curpkg) $(BUILD_TREE)/agi/agi-test.agi
	dh_installexamples -p$(cdbs_curpkg) $(BUILD_TREE)/agi/fastagi-test
	dh_installdocs -p$(cdbs_curpkg) $(BUILD_TREE)/CREDITS
	dh_installdocs -p$(cdbs_curpkg) $(BUILD_TREE)/doc/*
	dh_installdocs -p$(cdbs_curpkg) $(BUILD_TREE)/contrib
	dh_installdocs -p$(cdbs_curpkg) $(BUILD_TREE)/images/*.gif
	dh_installdocs -p$(cdbs_curpkg) $(BUILD_TREE)/Zaptel-to-DAHDI.txt

clean::
	rm -f *.cdbs-config_list

.PHONY: get-orig-source

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