#!/usr/bin/make -f
# By Jelmer Vernooij <jelmer@samba.org>
#
DESTDIR = $(CURDIR)/debian/tmp

export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

PYVERS=$(shell pyversions -vr)

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
LDB_VERSION = $(shell pkg-config --modversion ldb)
LDB_NEXT_VERSION = $(shell python -c "x = '$(LDB_VERSION)'.split('.'); x[-1] = str(int(x[-1])+1); print '.'.join(x)")
LDB_DEPENDS = "libldb1 (<< 1:$(LDB_NEXT_VERSION)~), libldb1 (>> 1:$(LDB_VERSION)~)"
export PYTHON=$(shell which `pyversions -d`)
export PYTHON_CONFIG="$(PYTHON)-config"
WAF = $(PYTHON) ./buildtools/bin/waf

# turn DEB_BUILD_OPTIONS='foo,bar' into DEB_BUILD_OPT_FOO and DEB_BUILD_OPT_BAR
d_b_o:=$(shell echo "$$DEB_BUILD_OPTIONS"|sed 's/[^-[:alnum:]]/ /g'|tr a-z A-Z)
$(foreach o, $(d_b_o), $(eval DEB_BUILD_OPT_$o := 1))

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

%:
	dh $* --with python2

override_dh_auto_configure:
	$(shell dpkg-buildflags --export=configure) $(WAF) configure \
		--prefix=/usr \
		--mandir=/usr/share/man \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--with-piddir=/var/run \
		--with-modulesdir=/usr/lib/$(DEB_HOST_MULTIARCH)/samba/ \
		--disable-rpath \
		--disable-ntdb \
		--disable-rpath-install \
		--bundled-libraries=NONE,pytevent,iniparser \
		--builtin-libraries=replace,ccan \
		--private-libraries=smbclient \
		--minimum-library-version="$(shell ./debian/autodeps.py --minimum-library-version)" \
		--libdir=/usr/lib/$(DEB_HOST_MULTIARCH) \
		--enable-fhs

override_dh_auto_clean:
	-$(WAF) clean
	rm -f debian/*.upstart
	find . -name "*.pyc" | xargs rm -f
	rm -rf buildtools/bin/.waf-*

override_dh_auto_install:
	DESTDIR="$(DESTDIR)" $(WAF) install

override_dh_auto_build:
	DESTDIR="$(DESTDIR)" $(WAF)

override_dh_auto_test:
	# Running make test requires configuration with --enable-selftest, which
	# we don't want to do for production systems.

override_dh_install:
	# Alternatives are used
	# Already provided by libparse-yapp-perl
	rm $(DESTDIR)/usr/share/perl5/Parse/Yapp/Driver.pm
	# Already shipped in samba-common
	rm $(DESTDIR)/usr/share/samba/codepages/*.dat
	# Binaries not ready for installation
	rm $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/mit_samba.so
	# Included in python-tevent
	rm $(DESTDIR)/usr/lib/python*/*-packages/_tevent.so
	rm $(DESTDIR)/usr/lib/python*/*-packages/tevent.py
	# Already included in various system packages
	rm -r $(DESTDIR)/usr/lib/python*/*-packages/samba/external
	# Already documented in debian/copyright
	rm $(DESTDIR)/usr/share/samba/setup/ad-schema/licence.txt
	# System ldb loads its modules from a different path
	mkdir -p $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/ldb/modules/ldb
	ln -sf ../../../samba/ldb $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/ldb/modules/ldb/samba
	mkdir -p $(DESTDIR)/lib/$(DEB_HOST_MULTIARCH)/security
	mkdir -p $(DESTDIR)/lib/$(DEB_HOST_MULTIARCH)
	mv $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/security/pam_winbind.so $(DESTDIR)/lib/$(DEB_HOST_MULTIARCH)/security/pam_winbind.so
	mv $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/libnss_winbind.so* $(DESTDIR)/lib/$(DEB_HOST_MULTIARCH)
	rm $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/samba/share/ldb.so
	rm -r $(DESTDIR)/usr/share/samba/codepages/ru
	rm $(DESTDIR)/usr/share/samba/codepages/*.msg
	mkdir -p $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/plugin/krb5
	mv $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/winbind_krb5_locator.so \
	   $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/plugin/krb5
	install -m 0755 debian/setoption.py $(DESTDIR)/usr/share/samba
	install -m 0755 debian/addshare.py $(DESTDIR)/usr/share/samba
	# Remove Samba 3 files for now
	while read line; \
	do \
		rm -r $(DESTDIR)$$line; \
	done < debian/samba3-files
	rm -f $(DESTDIR)/usr/share/man/man8/vfs_*.8
	while read line; \
	do \
		rm -r $(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/$$line; \
	done < debian/samba3-files-lib
	dh_install --sourcedir=$(DESTDIR) --list-missing --fail-missing

override_dh_python2:
	dh_python2 --no-guessing-versions

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

override_dh_installdocs:
	dh_installdocs source4/NEWS

override_dh_shlibdeps:
	LD_LIBRARY_PATH=$(DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/samba:$$LD_LIBRARY_PATH dh_shlibdeps -a

override_dh_gencontrol:
	dh_gencontrol -a -- -Vldb:Depends=$(LDB_DEPENDS)

override_dh_makeshlibs:
	dh_makeshlibs -X/usr/lib/$(DEB_HOST_MULTIARCH)/samba -X/usr/lib/$(DEB_HOST_MULTIARCH)/plugin

override_dh_installinit:
	if dpkg-vendor --derives-from ubuntu; then \
		cp debian/samba4.upstart.in debian/samba4.upstart; \
		dh_installinit --upstart-only; \
	else \
		dh_installinit; \
	fi;
