#!/usr/bin/make -f
%:
	dh $@ --with python3 \
		--builddirectory=build

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

CFLAGS = $(shell dpkg-buildflags --get CFLAGS)
CFLAGS += -I/usr/include/samba-4.0

export CK_DEFAULT_TIMEOUT=30

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

APIDOCDIR = /usr/share/sssd
DISTRIBUTION = $(shell lsb_release -i | sed 's/.*:\t//')
INIT = init
PKGDATE = $(shell dpkg-parsechangelog | \
	awk -F" " '/^Date/ { print $$4 "/" $$3 "/" $$5 }' | \
	sed 's/Jan/01/;s/Feb/02/;s/Mar/03/;s/Apr/04/;s/May/05/;s/Jun/06/;s/Jul/07/;s/Aug/08/;s/Sep/09/;s/Oct/10/;s/Nov/11/;s/Dec/12/;s/\//\\\//g')
CURDATE = $(shell date +%m/%d/%Y | sed 's/\//\\\//g')
export CK_VERBOSITY=verbose

override_dh_auto_configure:
	dh_auto_configure -- --enable-krb5-locator-plugin \
	--datadir=/usr/share/ \
	--with-environment-file=/etc/default/sssd \
	--with-ldb-lib-dir=/usr/lib/$(DEB_HOST_MULTIARCH)/ldb/modules/ldb \
	--with-krb5-plugin-path=/usr/lib/$(DEB_HOST_MULTIARCH)/krb5/plugins/libkrb5 \
	--enable-nsslibdir=/lib/$(DEB_HOST_MULTIARCH) \
	--enable-pammoddir=/lib/$(DEB_HOST_MULTIARCH)/security \
	--enable-systemtap \
	--disable-static \
	--disable-rpath \
	--with-autofs \
	--with-crypto=libcrypto \
	--with-ssh \
	--with-initscript=systemd \
	--with-systemdunitdir=/lib/systemd/system \
	--disable-files-domain \
	--with-smb-idmap-interface-version=6 \
	--without-python2-bindings \
	--with-syslog=journald \
	--with-pid-path=/run \
	--with-libwbclient=yes \
	--with-sudo

override_dh_auto_test:
ifeq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),)
	export CK_TIMEOUT_MULTIPLIER=10
	dh_auto_test -- VERBOSE=yes
	unset CK_TIMEOUT_MULTIPLIER
endif

override_dh_auto_install:
	dh_auto_install --max-parallel=1

override_dh_fixperms:
	dh_fixperms -Xkrb5_child -Xldap_child -Xselinux_child

override_dh_install:
	install -D -m755 $(CURDIR)/debian/generate-config \
		$(CURDIR)/debian/tmp/usr/share/sssd/generate-config

	mkdir -p $(CURDIR)/debian/libpam-sss/usr/share/pam-configs
	install -m644 debian/libpam-sss.pam-auth-update \
		$(CURDIR)/debian/libpam-sss/usr/share/pam-configs/sss
	install -m644 -D $(CURDIR)/debian/apparmor-profile \
		$(CURDIR)/debian/sssd-common/etc/apparmor.d/usr.sbin.sssd

	cat $(CURDIR)/debian/sssd-common.sssd.$(INIT).in > $(CURDIR)/debian/sssd-common.sssd.$(INIT)

	# remove files we don't want to install
	find $(CURDIR)/debian/tmp/ -name '*.la' -exec rm '{}' ';'
	find $(CURDIR)/debian/tmp/ -name '*.pyc' -exec rm '{}' ';'
	find $(CURDIR)/debian/tmp/ -name '*.egg-info' -exec rm '{}' ';'
	rm -f $(CURDIR)/debian/tmp/etc/rc.d/init.d/sssd

	# match nn/nn/nnnn, replace with the date from changelog
	sed -i 's/[0-1][0-9]\/[0-3][0-9]\/[0-9][0-9][0-9][0-9]/${PKGDATE}/g' $(CURDIR)/debian/tmp/usr/share/man/man*/*

	# change the shebang
	sed -i -e 's:/usr/bin/python:/usr/bin/python3:' $(CURDIR)/debian/tmp/usr/sbin/sss_obfuscate

	dh_install

override_dh_installman:

override_dh_missing:
	dh_missing --fail-missing

override_dh_installinit:
	dh_apparmor -psssd-common --profile-name=usr.sbin.sssd
	dh_installinit --name sssd --error-handler=invoke_failure

override_dh_installdeb:
	dh_installdeb
	for pkg in sssd-common sssd-ipa sssd-krb5-common sssd-proxy; do \
		sed -i 's/@TRIPLET@/${DEB_HOST_MULTIARCH}/' \
		$(CURDIR)/debian/$$pkg/DEBIAN/postinst; \
	done

override_dh_auto_clean:
	dh_auto_clean
	rm -f $(CURDIR)/debian/sssd-common.$(INIT)
	rm -f $(CURDIR)/po/*.gmo
	rm -f $(CURDIR)/src/config/*.pyc
	rm -f $(CURDIR)/po/stamp-po
	rm -f $(CURDIR)/src/sbus/codegen/__pycache__/*.pyc
