#!/usr/bin/make -f

CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

CFLAGS += -g -Wall
LDFLAGS += -Wl,--as-needed

DESTDIR=$(CURDIR)/debian/tmp
export PYTHON=$(shell which `pyversions -d`)
export PYTHON_CONFIG=$(PYTHON)-config

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

%:
	dh $* --with python2

override_dh_auto_configure:
	PYTHON="$(PYTHON)" PYTHON_CONFIG="$(PYTHON_CONFIG)" CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
		   ./configure --prefix=/usr --disable-rpath-install --libdir=/usr/lib/$(DEB_HOST_MULTIARCH)

get-packaged-orig-source:
	./debian/build-orig.sh

override_dh_python2:
	dh_python2 --no-guessing-versions

override_dh_auto_build:
	$(MAKE) all

override_dh_auto_test:
	echo "Testsuite disabled, since it currently requires developer mode."
#ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
#	$(MAKE) check
#endif

override_dh_auto_clean:
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -rf build
	# Waf should be doing this; see
	# https://bugzilla.samba.org/show_bug.cgi?id=8854
	rm -f .lock-wscript
	# Waf should be doing this; see
	# https://bugzilla.samba.org/show_bug.cgi?id=8855
	rm -f buildtools/wafsamba/*.pyc \
		  buildtools/wafadmin/*.pyc \
		  buildtools/wafadmin/Tools/*.pyc

override_dh_auto_install:
	$(MAKE) install DESTDIR=$(DESTDIR)
	# Waf should be doing this..
	ar cr libntdb.a bin/default/*.o bin/default/lib/ccan/*/*.o
	mv libntdb.a $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)

override_dh_strip:
	dh_strip -p libntdb1 --dbg-package=libntdb1-dbg
	dh_strip -p python-ntdb --dbg-package=python-ntdb-dbg
	dh_strip -p ntdb-tools

override_dh_makeshlibs:
	dh_makeshlibs -- -c4
