#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS

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

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

# some generics
confflags += --prefix=/usr --sysconfdir=/etc/asterisk

PACKAGE:=asterisk-addons

DEBVERSION:=$(shell head -n 1 debian/changelog \
		    | sed -e 's/^[^(]*(\([^)]*\)).*/\1/')
LOCALVERSION:=$(shell echo $(DEBVERSION) | sed -e 's/^.*://' -e 's/-[0-9.]*$$//' )
UPVERSION:=$(shell echo $(LOCALVERSION) | sed -e 's/~\(rc[0-9]\)/-\1/')

UPREALVER:=

FILENAME := $(PACKAGE)_$(LOCALVERSION).orig.tar.gz
URL := http://downloads.digium.com/pub/asterisk/releases/asterisk-addons-$(UPVERSION).tar.gz

-include /usr/share/quilt/quilt.make

CFLAGS = -Wall -g -fPIC

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

autotools-stamp:
	for dir in . channels menuselect; do \
		rm -f $$dir/config.sub $$dir/config.guess ; \
		cp /usr/share/misc/config.sub $$dir/config.sub ; \
		cp /usr/share/misc/config.guess $$dir/config.guess ; \
	done
	touch $@

configure: config.status
config.status: patch autotools-stamp
	dh_testdir
	./configure $(confflags)

build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: config.status
	$(MAKE) CFLAGS="$(CFLAGS)"
	touch $@

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

clean: clean-unpatched unpatch
clean-unpatched:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp config.status
	$(MAKE) distclean
	rm -f menuselect/mxml/Makefile menuselect/mxml/mxml.list
	for dir in . channels menuselect; do \
		rm -f $$dir/config.sub $$dir/config.guess ; \
	done
	rm -f autotools-stamp
	dh_clean 

install: install-indep install-arch
install-indep:

INST_DIR=$(CURDIR)/debian/tmp
install-arch:
	dh_testdir
	dh_testroot
	dh_prep -a 
	dh_installdirs -a
	install -d -m 755 $(INST_DIR)/usr/lib/asterisk/modules
	$(MAKE) install DESTDIR=$(INST_DIR)
	dh_install --sourcedir=$(INST_DIR) --list-missing

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs -pasterisk-oo323c doc/ChangeLog.chan_ooh323
	dh_installdocs
	dh_installexamples
	dh_installman
	dh_link
	dh_strip
	dh_compress 
	dh_fixperms
	# -n: there are no /usr/lib libraries in these packages
	dh_makeshlibs -n
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

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

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

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