#!/usr/bin/make -f

CFLAGS = -g -Wall

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

LDFLAGS += -Wl,--as-needed

DESTDIR=$(CURDIR)/debian/tmp

export PYTHON=$(shell which `pyversions -d`)
export PYTHON_CONFIG="$(PYTHON)-config"
export WAF=$(PYTHON) ./buildtools/bin/waf

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

%:
	dh $* --with python2

override_dh_auto_configure:
	PYTHON="$(PYTHON)" PYTHON_CONFIG="$(PYTHON_CONFIG)" CFLAGS="$(CFLAGS)"\
		$(WAF) configure --prefix=/usr --disable-rpath-install \
		--builtin-libraries=ccan,replace,tdb_compat \
		--bundled-libraries=NONE \
		--minimum-library-version="$(shell ./debian/autodeps.py --minimum-library-version)" \
		--libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
		--with-modulesdir=/usr/lib/$(DEB_HOST_MULTIARCH)/ldb/modules

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

override_dh_auto_clean:
	-$(WAF) clean
	rm -rf buildtools/bin/.waf-*
	rm -f buildtools/wafsamba/*.pyc

override_dh_auto_build:
	$(WAF) build

override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	$(WAF) test
endif

override_dh_python2:
	dh_python2 --no-guessing-versions

override_dh_auto_install:
	DESTDIR=$(DESTDIR) $(WAF) install
	ar cr $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/libldb.a bin/default/common/*.o
	rm $(DESTDIR)/usr/lib/python*/*/_tevent.so

override_dh_strip:
	dh_strip -pldb-tools
	dh_strip -ppython-ldb --dbg-package=python-ldb-dbg
	dh_strip -plibldb1 --dbg-package=libldb1-dbg

override_dh_makeshlibs:
	dh_makeshlibs -Xldb.so -ppython-ldb -- -c4
	dh_makeshlibs -X/usr/lib/$(DEB_HOST_MULTIARCH)/ldb -plibldb1 -- -c4
