#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)


CFLAGS = -Wall -g

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

# FOR AUTOCONF 2.52 AND NEWER ONLY
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

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

DFSG:=$(shell echo $(DEBVERSION) | sed -e 's/-[0-9.]*$$//')

FILENAME := yate_$(UPVERSION).orig.tar.gz
SRCDIR := yate
DFSGNAME := yate_$(DFSG).orig.tar.gz
URL := http://yate.null.ro/tarballs/yate2/yate-$(UPVERSION).tar.gz

check-ilbc:
ifneq (,$(wildcard libs/ilbc/iLBC_define.h))
	echo "WARNING: iLBC must be removed from sources before packaging."
	exit 1
endif
	touch $@

configure:
	if [ -f configure ]; then cp -a configure configure.sav; fi
	autoconf

config.status: check-ilbc configure
	dh_testdir
	# Add here commands to configure the package.
	CFLAGS="$(CFLAGS)" ./configure $(confflags) --prefix=/usr \
	       --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info \
	       --sysconfdir=/etc --disable-ilbc --without-amrnb \
	       --without-coredumper
		#--prefix=/ --exec-prefix=/usr \
		#--datarootdir=/usr/share --docdir=/usr/share/doc/yate-doc \
		#--mandir=/usr/share/man --includedir=/usr/include \
		#--with-openssl --with-libgsm --with-libqt4

build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: config.status
	dh_testdir
	# Add here commands to compile the package.
	$(MAKE)
	#docbook-to-man debian/yate.sgml > yate.1
	touch $@

build-indep: build-indep-stamp
build-indep-stamp: config.status
	cd share/scripts/yaypm ; python ./setup.py build
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 
	# Add here commands to clean up after the build process.
	[ ! -f Makefile ]||$(MAKE) clean distclean
	rm -f config.status
	rm -f config.sub config.guess
	cp /usr/share/misc/config.sub config.sub
	cp /usr/share/misc/config.guess config.guess
	if [ -f configure.sav ]; then mv configure.sav configure; fi
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_installdirs
	# Add here commands to install the package into debian/yate.
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
	# remove extra files
	rm -f $(CURDIR)/debian/tmp/usr/share/doc/yate*/COPYING
	rm -f $(CURDIR)/debian/tmp/usr/share/doc/yate*/ChangeLog


# Build architecture-independent files here.
binary-indep: install
	dh_testdir -i
	dh_testroot -i
	dh_installlogrotate -i
	dh_installinit -i -- defaults 21
	dh_installdocs -i 
	dh_installexamples -i -XCVS
	dh_installcron -i
	dh_installchangelogs ChangeLog -i
	dh_install -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

subpacks_names = alsa dahdi qt4 mysql openh323 pgsql
subpacks = $(subpacks_names:%=$(CURDIR)/debian/yate-%) $(CURDIR)/debian/yate

# Build architecture-dependent files here.
binary-arch: install
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a ChangeLog
	dh_installdocs -a
	dh_installexamples -a
	dh_install -a
	find $(subpacks) -name '*.yate' -type f  -printf '-name %f -o\n' \
	  | xargs | sed -e 's/ -o$$//' | xargs find $(CURDIR)/debian/yate-core \
	  | xargs $(RM) -fv
	find $(subpacks) -name '*.conf' -type f  -printf '-name %f -o\n' \
	  | xargs | sed -e 's/ -o$$//' | xargs find $(CURDIR)/debian/yate-core \
	  | xargs $(RM) -fv
	dh_installinit -a
	dh_installman -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs  -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

print-version:
	@@echo "Debian version:          $(DEBVERSION)"
	@@echo "DFSG     version:        $(DFSG)"
	@@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)
	@@echo Removing non dfsg compliant parts out of it
	@@[ -d ../tarballs/dfsg/. ]||mkdir -p ../tarballs/dfsg
	@@tar -xzf ../tarballs/$(FILENAME) -C ../tarballs/dfsg
	@@rm -rf ../tarballs/dfsg/$(SRCDIR)/libs/ilbc/*
	@@echo -e '#Debian Dummy\n%:\n\t:' >../tarballs/dfsg/$(SRCDIR)/libs/ilbc/Makefile.in
	@@echo Building the dfsg tarball
	@@GZIP=-9 tar -b1 -czf ../tarballs/$(DFSGNAME) -C ../tarballs/dfsg $(SRCDIR)
	@@echo Cleaning up
	@@rm -rf ../tarballs/dfsg
	@@rm -f ../tarballs/$(FILENAME)

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