#!/usr/bin/make -f
# 
# debian/rules file

# Environment
#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)

CXXFLAGS = -Wall -g
CFLAGS = -Wall -g

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

backends := ldap pipe gmysql gpgsql gsqlite gsqlite3 geo

# Targets
config.status:
	dh_testdir
	
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
endif
	
	# First run the bootstrap.
	./bootstrap

	# Add here commands to configure the package.
	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" ./configure \
		--host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--sysconfdir=/etc/powerdns \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--libdir='$${prefix}/lib/powerdns' \
		--libexecdir='$${prefix}/lib' \
		--with-dynmodules="$(backends)" \
		--with-modules="" \
		--with-pgsql-includes=`pg_config --includedir` \
		--disable-recursor \
		--with-sqlite3-lib=/usr/lib/`dpkg-architecture -qDEB_HOST_MULTIARCH`

build: build-stamp
build-stamp: config.status
	
	# Add here commands to compile the arch part of the package.
	$(MAKE) 
	
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp config.log config.sub config.guess
	
	# Add here commands to clean up after the build process.
	[ ! -f modules/db2backend/Makefile ] || $(MAKE) -C modules/db2backend/ distclean
	[ ! -f modules/pdnsbackend/Makefile ] || $(MAKE) -C modules/pdnsbackend/ distclean
	[ ! -f modules/xdbbackend/Makefile ] || $(MAKE) -C modules/xdbbackend/ distclean
	[ ! -f modules/mysqlbackend/Makefile ] || $(MAKE) -C modules/mysqlbackend/ distclean
	[ ! -f modules/goraclebackend/Makefile ] || $(MAKE) -C modules/goraclebackend/ distclean
	[ ! -f modules/opendbxbackend/Makefile ] || $(MAKE) -C modules/opendbxbackend/ distclean
	[ ! -f Makefile ] || $(MAKE) distclean
	[ ! -f pdns/docs/Makefile ] || $(MAKE) -C pdns/docs clean
	
	dh_clean 

install:
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	
	# Install the stuff
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) -C pdns/docs html/index.html
	
	# Set up init.d virtual facility
	mkdir -p $(CURDIR)/debian/pdns-server/etc/insserv.conf.d
	cp $(CURDIR)/debian/pdns-server.insserv \
		$(CURDIR)/debian/pdns-server/etc/insserv.conf.d/pdns-server
	
	dh_install

# Build architecture independant packages.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i ChangeLog
	dh_installdocs -i
	dh_link -i
	dh_compress -i -X changelog.html
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture dependant packages.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a ChangeLog
	dh_installdocs -a
	dh_installdebconf -a
	dh_installman -a
	dh_installexamples -a
	dh_lintian -a
	dh_link -a
	dh_strip -a --dbg-package=pdns-server-dbg
	dh_compress -a
	dh_fixperms -a
	chmod 755 $(CURDIR)/debian/pdns-server/etc/resolvconf/update.d/pdns
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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