#!/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=/opt/asterisk/share ASTVARRUNDIR=/var/run/asterisk ASTCONFPATH=/opt/asterisk/etc/asterisk.conf ASTETCDIR=/opt/asterisk/etc ASTVARLIBDIR=/opt/asterisk/var/lib ASTLIBDIR=/opt/asterisk/lib ASTLOGDIR=/opt/asterisk/var/log

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

DEBVERSION:=$(shell head -n 1 debian/changelog \
		    | sed -e 's/^[^(]*(\([^)]*\)).*/\1/')
DEB_NOEPOCH_VERSION:=$(shell echo $(DEBVERSION) | cut -d':' -f 2)
DEB_SRC_VERSION:=$(shell echo $(DEB_NOEPOCH_VERSION) | sed -e 's/-[0-9.]*$$//')
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=/opt \
		--bindir=\$${prefix}/asterisk/bin \
		--sbindir=\$${prefix}/asterisk/sbin \
		--sysconfdir=\$${prefix}/asterisk/etc \
		--localstatedir=\$${prefix}/asterisk/var \
		--libdir=\$${prefix}/asterisk/lib \
		--includedir=\$${prefix}/include \
		--datarootdir=\$${prefix}/asterisk/share \
		--datadir=\$${prefix}/asterisk/share \
		--infodir=\$${prefix}/asterisk/info \
		--localedir=\$${prefix}/asterisk/localedir \
		--mandir=\$${prefix}/asterisk/share/man \
		--docdir=\$${prefix}/asterisk/doc \
		--htmldir=\$${prefix}/asterisk/doc/html

build: build-arch build-indep

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

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

clean:
	dh_testdir
	dh_testroot
	
	# Don't remove soundfiles or builder will have to download them again
	sed -i -e 's/rm -f \*.tar.gz//g' sounds/Makefile
	
	GREP=grep AWK=awk $(MAKE) distclean
	-$(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/opt/asterisk/share/conf

	mkdir -p $(CURDIR)/debian/tmp/usr/share/icons/hicolor/scalable/hildon
	mkdir -p $(CURDIR)/debian/tmp/usr/share/icons/hicolor/48x48/hildon
	mkdir -p $(CURDIR)/debian/tmp/usr/share/applications/hildon
	mkdir -p $(CURDIR)/debian/tmp/opt/asterisk/bin
	mkdir -p $(CURDIR)/debian/tmp/etc/sudoers.d

	cp -p $(CURDIR)/debian/asterisk-64x64.xpm $(CURDIR)/debian/tmp/usr/share/icons/hicolor/scalable/hildon/asterisk.xpm
	cp -p $(CURDIR)/debian/asterisk-48x48.xpm $(CURDIR)/debian/tmp/usr/share/icons/hicolor/48x48/hildon/asterisk.xpm
	cp -p $(CURDIR)/debian/asterisk.desktop $(CURDIR)/debian/tmp/usr/share/applications/hildon
	cp -p $(CURDIR)/debian/asterisk-maemo $(CURDIR)/debian/tmp/opt/asterisk/bin/
	chmod 755 $(CURDIR)/debian/tmp/opt/asterisk/bin/asterisk-maemo
	cp -p $(CURDIR)/debian/asterisk.sudoers $(CURDIR)/debian/tmp/etc/sudoers.d/


	dh_install -s --sourcedir=debian/tmp

	# XXX
	$(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

	dh_install -i --sourcedir=debian/tmp

	# create a simple config
	mkdir -p $(CURDIR)/debian/asterisk-config/opt/asterisk/etc/manager.d
	echo "; please read the documentation regarding the Manager Interface (asterisk-doc package)" > \
		$(CURDIR)/debian/asterisk-config/opt/asterisk/etc/manager.d/README.conf
	touch $@

binary: binary-indep binary-arch
binary-indep: install-indep
	dh_testdir -i
	dh_testroot -i
	dh_installlogrotate -i 
	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/opt/asterisk/etc/*
	chmod o+rx  $(CURDIR)/debian/asterisk-config/opt/asterisk/etc/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_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
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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

TMP_TARBALL_TOP=../tarballs/asterisk-$(UPVERSION).tmp/asterisk-$(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
