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

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)

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	BUILDFLAGS += OPTIMIZE=-O0
else
	BUILDFLAGS += OPTIMIZE=-O2
endif
BUILDFLAGS += MAKECMDGOALS=dont-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)))
	CFLAGS += -g
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)
DEB_SRC_VERSION:=$(shell echo $(DEB_NOEPOCH_VERSION) | sed -e 's/-[^-]\+$$//')
UPVERSION:=$(shell echo $(DEB_SRC_VERSION) | sed -e 's/[.~]dfsg//' -e 's/~\(\(rc\|beta\)[0-9]\)/-\1/')

FILENAME := asterisk_$(DEB_SRC_VERSION).orig.tar.gz
UPFILENAME := asterisk_$(UPVERSION).orig.tar.gz
URL := http://downloads.asterisk.org/pub/telephony/asterisk/releases/asterisk-$(UPVERSION).tar.gz

# 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

config.status:
	dh_testdir

	[ -f .version.debian_sav ] || cp -a .version .version.debian_sav
	echo $(DEB_NOEPOCH_VERSION) > .version

	if [ ! -r configure.debian_sav ]; then cp -a configure configure.debian_sav; fi

	chmod 755 $(CURDIR)/debian/dummyprogs/fetch
	
	$(FETCH_ENV) ./configure \
		--host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--with-cap \
		--with-gsm \
		--with-imap=system \
		--with-pwlib=/usr/share/pwlib/include/ \
		--with-h323=/usr/share/openh323/ 

build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: config.status
	dh_testdir
	$(FETCH_ENV) $(MAKE) $(BUILDFLAGS) h323-mak
	$(FETCH_ENV) $(MAKE) $(BUILDFLAGS)
	touch $@

build-indep: build-indep-stamp
build-indep-stamp: config.status
	dh_testdir
	touch $@

clean:
	dh_testdir
	dh_testroot
	
	# The clean target seems to be run before patches are applied.
	# A brute-force fix to keep the tarball from getting deleted:
	mv sounds/asterisk-core-sounds-en-gsm-1.4.19.tar.gz debian/
	GREP=grep AWK=awk $(MAKE) distclean
	mv debian/asterisk-core-sounds-en-gsm-1.4.19.tar.gz sounds/
	-$(RM) -rf debian/build

	if [ -r configure.debian_sav ]; then mv configure.debian_sav configure; fi
	-test -d configs && chmod -x configs/*.sample
	-$(RM) -f build-arch-stamp build-indep-stamp install-arch install-indep
	-$(RM) -f config.status menuselect.makeopts

	[ ! -f .version.debian_sav ] || mv .version.debian_sav .version

	# these were generated while building
	-$(RM) -f doc/core-en_US.xml utils/poll.c

	dh_clean

install: install-arch install-indep
install-arch: build-arch
	dh_testdir
	dh_testroot
	dh_clean -k -s
	dh_installdirs -s
	
	$(FETCH_ENV) $(MAKE) $(BUILDFLAGS) DESTDIR=$(CURDIR)/debian/tmp install
	cp -a configs $(CURDIR)/debian/tmp/usr/share/asterisk/conf

	dh_install -s --sourcedir=debian/tmp
	dh_lintian -s

	$(RM) -f $(CURDIR)/debian/asterisk/usr/sbin/aelparse
	$(RM) -f $(CURDIR)/debian/asterisk/usr/sbin/conf2ael
	$(RM) -f $(CURDIR)/debian/asterisk/usr/sbin/muted
	$(RM) -f $(CURDIR)/debian/asterisk/usr/sbin/streamplayer
	$(RM) -f $(CURDIR)/debian/asterisk/usr/sbin/stereorize
	$(RM) -f $(CURDIR)/debian/asterisk/usr/sbin/hashtest*
	$(RM) -f $(CURDIR)/debian/asterisk/usr/sbin/refcounter
	touch $@

install-indep: build-indep
	dh_testdir
	dh_testroot
	dh_clean -k -i
	dh_installdirs -i

	$(FETCH_ENV) $(MAKE) $(BUILDFLAGS) DESTDIR=$(CURDIR)/debian/tmp install samples
	rm -rf $(CURDIR)/debian/tmp/usr/share/asterisk/sounds/es
	rm -rf $(CURDIR)/debian/tmp/usr/share/asterisk/sounds/fr
	mv $(CURDIR)/debian/tmp/usr/share/asterisk/sounds/en \
	   $(CURDIR)/debian/tmp/usr/share/asterisk/sounds/en_US_f_Allison

	dh_install -i --sourcedir=debian/tmp
	dh_lintian -i

	# create a simple config
	echo "; please read the documentation regarding the Manager Interface (asterisk-doc package)" > \
		$(CURDIR)/debian/asterisk-config/etc/asterisk/manager.d/README.conf
	# Unapply the changes in AST-2011-013 to config file chan_sip.conf:
	# (Changes left in the patch as we do want to keep the sample config
	# files fixed)
	filterdiff -i '*/configs/sip.conf.sample' $(CURDIR)/debian/patches/AST-2011-013 \
		| patch -R $(CURDIR)/debian/asterisk-config/etc/asterisk/sip.conf
	touch $@

binary: binary-indep binary-arch
binary-indep: install-indep
	dh_testdir -i
	dh_testroot -i
	dh_installlogrotate -i 
	dh_installdocs -i -XREADME.cygwin
	mkdir $(CURDIR)/debian/asterisk-doc/usr/share/doc/asterisk-doc/html
	cp -a $(CURDIR)/doc/tex/asterisk/*.html $(CURDIR)/doc/tex/asterisk/*.png \
	      $(CURDIR)/debian/asterisk-doc/usr/share/doc/asterisk-doc/html
	dh_installexamples -i
	dh_installcron -i
	dh_installchangelogs ChangeLog -i
	dh_link -i 
	dh_compress -i
	dh_fixperms -i
	# should follow dh_fixperms; asterisk configurations may contain
	# sensitive information, such as passwords
	chmod o-rwx $(CURDIR)/debian/asterisk-config/etc/asterisk/*
	chmod o+rx  $(CURDIR)/debian/asterisk-config/etc/asterisk/manager.d
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: install-arch
	dh_testdir -a
	dh_testroot -a
	dh_installlogrotate -a
	dh_installdocs -a
	dh_installman utils/*.1 doc/*.8 contrib/scripts/*.8
	dh_installexamples -a
	dh_installchangelogs ChangeLog -a
	dh_installinit -a -- defaults 21
	dh_strip -a --dbg-package=asterisk-dbg
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	rm -f debian/asterisk/usr/lib/asterisk/modules/chan_h323.so
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

print-version:
	@@echo "Debian version:          $(DEBVERSION)"
	@@echo "Upstream version:        $(UPVERSION)"

get-orig-source:
	@@dh_testdir
	@@[ -d ../tarballs/. ]||mkdir -p ../tarballs
	@@echo Downloading $(UPFILENAME) from $(URL) ...
	@@wget -nv -T10 -t3 -O ../tarballs/$(FILENAME) $(URL)

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