#!/usr/bin/make -f

PYVERS=$(shell pyversions -vr)
DB2MAN=/usr/share/sgml/docbook/stylesheet/xsl/nwalsh/manpages/docbook.xsl
XP=xsltproc -''-nonet

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

%:
	dh $@ --with python2,autoreconf

CFLAGS = -Wall -g

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

CONFIGURE=../configure --host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--enable-debug-code \
		--disable-ruby \
		CFLAGS="$(CFLAGS)" \
		LDFLAGS="-Wl,--as-needed -ldl"

# shared library versions, option 1
#version=1.0.0
#major=1
# option 2, assuming the library is created as src/.libs/libfoo.so.2.0.5 or so
version=`ls src/.libs/lib*.so.* | \
 awk '{if (match($$0,/[0-9]+\.[0-9]+\.[0-9]+$$/)) print substr($$0,RSTART)}'`
major=`ls src/.libs/lib*.so.* | \
 awk '{if (match($$0,/\.so\.[0-9]+$$/)) print substr($$0,RSTART+4)}'`

override_dh_auto_configure:
	mkdir build-nopy
	cd build-nopy; \
		$(CONFIGURE) --disable-python; \
		cd ..
	set -e && for x in $(PYVERS); do \
		mkdir build-$$x; \
		cd build-$$x; \
		$(CONFIGURE) PYTHON=/usr/bin/python$$x; \
		cd ..; \
	done


license.1: debian/manpage.xml
	$(XP) $(DB2MAN) $<

override_dh_auto_build: license.1
	$(MAKE) -C build-nopy
	set -e && for x in $(PYVERS); do \
		$(MAKE) -C build-$$x; \
	done

override_dh_auto_install:
	set -e && for x in $(PYVERS); do \
		$(MAKE) -C build-$$x DESTDIR=$(CURDIR)/debian/tmp install; \
	done
	$(MAKE) -C build-nopy DESTDIR=$(CURDIR)/debian/tmp install

override_dh_auto_clean:
	rm -rf build-nopy
	set -e && for x in $(PYVERS); do \
		rm -rf build-$$x; \
	done
	rm -fv license.1
	dh_auto_clean
