#!/usr/bin/make -f

# debian rules for libdbh
# (c) 2010-2012 Edscott Wilson Garcia

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

package = libdbh
pkgversion ?= $(shell dpkg-parsechangelog | awk '/^Version:/ {print $$2}')
version ?= $(shell echo $(pkgversion) | sed 's/\([0-9\.]\)-.*$$/\1/')
soname =  1
major_version = $(shell echo $(version) | cut -d. -f1,2)
serpack = $(package)-$(soname)

virtpack = $(package)-$(major_version)

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_BUILD_ARCH      ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)


# To be used if binary packages were buildable on any arch
#ARCH_FLAG=-a
#ARCHS=any
ARCH_FLAG=-s
ARCHS=alpha armel armhf amd64 i386 ia64 powerpc sparc kfreebsd-i386 kfreebsd-amd64 hurd-i386

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif	

CONFIGURE_FLAGS = --prefix=/usr --host=$(DEB_HOST_GNU_TYPE) \
		  --build=$(DEB_BUILD_GNU_TYPE) \
		  --mandir=\$${prefix}/share/man \
		  --enable-shared 

configure: configure-stamp 

configure-stamp: 
	dh_testdir
	-mkdir debian/build
	cd debian/build && CFLAGS="$(CFLAGS)"  \
	../../configure $(CONFIGURE_FLAGS)  || { cat config.log; exit 1; }
	touch $@
 
prep: 
	dh_prep


build: build-arch build-indep
build-arch: build-stamp 
build-indep:

build-stamp: configure-stamp
	dh_testdir
	$(MAKE) $(MAKEFLAGS) -C debian/build/
	touch $@

test: build-stamp
	$(MAKE) -C debian/build/ test

clean: 
	dh_testroot
	-rm -rf debian/build
	-find debian -type l | xargs rm -f
	dh_clean configure-stamp* build-stamp* install-stamp* debian/*.shlibs

install: build prep install-lib install-doc

install-lib: build-stamp 
	dh_testdir
	dh_testroot
	-mkdir debian/build/tmpinst
	$(MAKE) -C debian/build/ install prefix=$(CURDIR)/debian/build/tmpinst/usr
	dh_install -p$(serpack) -p$(package)-dev  \
		--sourcedir=debian/build/tmpinst


install-doc: build-indep
	dh_testdir
	dh_testroot
	dh_install -p$(package)-doc -X Dependencies -X Makefile.in


binary-indep: install-doc
	dh_testdir
	dh_testroot
	dh_installdocs -i
	dh_compress -i -X.pdf
	dh_fixperms -i
	dh_installdeb -i
	dh_lintian -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: prep install-lib
	dh_testdir
	dh_testroot 
	dh_installdocs $(ARCH_FLAG)
	dh_installman $(ARCH_FLAG)
	dh_lintian $(ARCH_FLAG)
	dh_link $(ARCH_FLAG)
	dh_compress $(ARCH_FLAG)
	dh_fixperms $(ARCH_FLAG)
	dh_makeshlibs -p$(serpack) -V "$(serpack) | $(virtpack)"
	dh_installdeb $(ARCH_FLAG)
	dh_shlibdeps -p$(serpack) -L$(serpack) -ldebian/$(serpack)/usr/lib:debian/build/test/.libs
	dh_gencontrol $(ARCH_FLAG)
	dh_md5sums $(ARCH_FLAG)
	dh_builddeb $(ARCH_FLAG)

binary: binary-indep binary-arch


.PHONY: build clean binary-arch binary install configure install-doc install-lib  prep
	
.NOTPARALLEL: configure-stamp
