#!/usr/bin/make -f
# -*- makefile -*-
#
# debian/rules for CMU Cyrus SASL version 2.1
#
# Copyright (c) 2011 by Ondřej Surý - migrated to dh7
# Based on previous work (c) 2006 by Fabian Fagerholm.
# Based on previous work by Dima Barsky.
# Based on cyrus-imapd-2.2 packages by Henrique de Moraes Holshuch.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Note that Cyrus SASL itself is published under a different license.

# Debhelper control.
export DH_ALWAYS_EXCLUDE=CVS
#export DH_VERBOSE=1 # uncomment this to turn on verbose mode

# Enable package hardening through dpkg-buildflags
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk
-include /usr/share/dpkg/buildtools.mk
export CC

ifneq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
# Debian's GSSAPI is known to support SPNEGO.
export ac_cv_gssapi_supports_spnego=yes
endif

# see FEATURE AREAS in dpkg-buildflags(1)
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# see ENVIRONMENT in dpkg-buildflags(1)
# package maintainers to append CFLAGS
export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# package maintainers to append LDFLAGS
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-z,defs

# Save Berkeley DB used for building the package
BDB_VERSION ?= $(shell LC_ALL=C dpkg-query -l 'libdb[45].[0-9]-dev' | grep ^ii | sed -e 's|.*\s\libdb\([45]\.[0-9]\)-dev\s.*|\1|')

# SQL support may be turned off during the build, but is on by default.
ifeq (,$(findstring no-sql,$(DEB_BUILD_OPTIONS))$(findstring pkg.cyrus-sasl2.nosql,$(DEB_BUILD_PROFILES)))
	CONFIGURE_SQL=--enable-sql
else
	CONFIGURE_SQL=--disable-sql
	DH_PACKAGE_EXCLUDES += -Nlibsasl2-modules-sql
endif

# LDAP support may be turned off during the build, but is on by default.
ifeq (,$(findstring no-ldap,$(DEB_BUILD_OPTIONS))$(findstring pkg.cyrus-sasl2.noldap,$(DEB_BUILD_PROFILES)))
	CONFIGURE_LDAP=--with-ldap
	CONFIGURE_LDAPDB=--enable-ldapdb
else
	CONFIGURE_LDAP=--without-ldap
	CONFIGURE_LDAPDB=--disable-ldapdb
	DH_PACKAGE_EXCLUDES += -Nlibsasl2-modules-ldap
endif

# GSSAPI support may be turned off during the build, but is on by default
ifeq (,$(findstring no-gssapi,$(DEB_BUILD_OPTIONS))$(findstring pkg.cyrus-sasl2.nogssapi,$(DEB_BUILD_PROFILES)))
	CONFIGURE_GSSAPI=--enable-gssapi
else
	CONFIGURE_GSSAPI=--disable-gssapi
	DH_PACKAGE_EXCLUDES += -Nlibsasl2-modules-gssapi-mit \
		-Nlibsasl2-modules-gssapi-heimdal
endif

CONFIGURE_COMMON_OPTIONS= \
	--build=$(DEB_BUILD_GNU_TYPE) \
	--prefix=/usr \
	--mandir=\$${prefix}/share/man \
	--infodir=\$${prefix}/share/info \
	--enable-static \
	--enable-shared \
	--enable-alwaystrue \
	--enable-checkapop \
	--enable-cram \
	--enable-digest \
	--enable-otp \
	--disable-srp \
	--disable-srp-setpass \
	--disable-krb4 \
	$(CONFIGURE_GSSAPI) \
	--enable-gss_mutexes \
	--enable-auth-sasldb \
	--enable-plain \
	--enable-anon \
	--enable-login \
	--enable-ntlm \
	--disable-passdss \
	$(CONFIGURE_SQL) \
	--with-sqlite3=/usr \
	--with-mysql=/usr \
	--with-pgsql=/usr \
	$(CONFIGURE_LDAPDB) \
	--disable-macos-framework \
	--with-pam=/usr \
	--with-saslauthd=/var/run/saslauthd \
	$(CONFIGURE_LDAP) \
	--with-configdir=/etc/sasl2:/etc/sasl:/usr/lib/$(DEB_HOST_MULTIARCH)/sasl2:/usr/lib/sasl2 \
	--with-plugindir=/usr/lib/$(DEB_HOST_MULTIARCH)/sasl2 \
	--sysconfdir=/etc \
	--with-devrandom=/dev/urandom \
	--with-sphinx-build

# Some convenience variables
export TMPBUILD_MIT := build-mit
export TMPBUILD_HEIMDAL := build-heimdal
export TMP_MIT := debian/tmp
export TMP_HEIMDAL := debian/tmp-heimdal
export MIT_LDFLAGS := $(shell krb5-config.mit --libs gssapi | sed -e 's/ -l.*//')
export MIT_CFLAGS := $(shell krb5-config.mit --cflags gssapi)
export HEIMDAL_LDFLAGS := $(shell krb5-config.heimdal --libs gssapi | sed -e 's/ -l.*//')
export HEIMDAL_CFLAGS := $(shell krb5-config.heimdal --cflags gssapi)

AUTOFILES=acinclude.m4 aclocal.m4 config/config.sub config/config.guess \
		  config/ltmain.sh config/libtool.m4

### The Makefile targets begin. ###

%:
	dh $@ $(DH_PACKAGE_EXCLUDES)

override_dh_auto_clean:
	dh_auto_clean -B$(TMPBUILD_MIT)
	dh_auto_clean -B$(TMPBUILD_HEIMDAL)

	rm -f sample/sample-client \
		sample/sample-server
	[ ! -f Makefile ] || $(MAKE) distclean
	-rm -f config.h config.log autom4ate.cache

	# Remove symlinks that the CMU build sets up but never removes.
	# They can be found by running find . -lname '*' -print in the
	# source tree before and after a build and comparing the differences.
	rm -f lib/sasldb.c lib/db_berkeley.c lib/allockey.c lib/cram.c \
			lib/digestmd5.c lib/otp.c lib/gssapi.c lib/plain.c \
			lib/anonymous.c lib/login.c lib/ntlm.c lib/sql.c lib/ldapdb.c

	# Remove generated man pages
	-rm -f sasl-sample-client.8 sasl-sample-server.8 gen-auth.1

	# Remove build directories
	rm -rf $(TMP_MIT) $(TMP_HEIMDAL)

override_dh_auto_configure:
	LDFLAGS="$(LDFLAGS) $(HEIMDAL_LDFLAGS)" \
	CFLAGS="$(CFLAGS) $(HEIMDAL_CFLAGS)" \
	dh_auto_configure -B$(TMPBUILD_HEIMDAL) -- $(CONFIGURE_COMMON_OPTIONS) --with-gss_impl=heimdal

	LDFLAGS="$(LDFLAGS) $(MIT_LDFLAGS)" \
	CFLAGS="$(CFLAGS) $(MIT_CFLAGS)" \
	dh_auto_configure -B$(TMPBUILD_MIT) -- $(CONFIGURE_COMMON_OPTIONS) --with-gss_impl=mit

override_dh_auto_build:
	-mkdir docsrc/exts/themes
	-ln -s /usr/share/sphinx_rtd_theme docsrc/exts/themes/cyrus
	dh_auto_build -B$(TMPBUILD_HEIMDAL) -- sasldir=/usr/lib/$(DEB_HOST_MULTIARCH)/sasl2
	dh_auto_build -B$(TMPBUILD_MIT) -- sasldir=/usr/lib/$(DEB_HOST_MULTIARCH)/sasl2 all doc-html

	# Build sample-{client,server}
	$(MAKE) -f ../debian/sample/Makefile -C sample T=../$(TMPBUILD_MIT)

	# Build the sasl-sample-client and sasl-sample-server man pages.
	/usr/bin/docbook-to-man debian/sasl-sample-client.sgml \
		> sasl-sample-client.8
	/usr/bin/docbook-to-man debian/sasl-sample-server.sgml \
		> sasl-sample-server.8
	# Build the gen-auth man page
	/usr/bin/pod2man --stderr debian/gen-auth/gen-auth >gen-auth.1

override_dh_auto_install:
	dh_auto_install -B$(TMPBUILD_HEIMDAL) --destdir=$(TMP_HEIMDAL) -- sasldir=/usr/lib/$(DEB_HOST_MULTIARCH)/sasl2
	dh_auto_install -B$(TMPBUILD_MIT) --destdir=$(TMP_MIT) -- sasldir=/usr/lib/$(DEB_HOST_MULTIARCH)/sasl2

	# Remove static plugins - they are useless
	rm $(TMP_MIT)/usr/lib/$(DEB_HOST_MULTIARCH)/sasl2/*.a
	# Remove libtool la files, they are eeevil
	rm $(TMP_MIT)/usr/lib/$(DEB_HOST_MULTIARCH)/*.la \
	   $(TMP_MIT)/usr/lib/$(DEB_HOST_MULTIARCH)/sasl2/*.la

	# Note the version of Berkeley DB used to build this package
	mkdir -p $(TMP_MIT)/usr/lib/sasl2
	echo $(BDB_VERSION) > $(TMP_MIT)/usr/lib/sasl2/berkeley_db.txt

	# Alter the default location and names of files to fit Debian
	# policy and better integrate with the Debian system.
	mv $(TMP_MIT)/usr/sbin/pluginviewer $(TMP_MIT)/usr/sbin/saslpluginviewer
	mv $(TMP_MIT)/usr/share/man/man8/pluginviewer.8 \
		$(TMP_MIT)/usr/share/man/man8/saslpluginviewer.8
	install -m 644 saslauthd/saslauthd.mdoc \
		$(TMP_MIT)/usr/share/man/man8/saslauthd.8
	mv $(TMP_MIT)/usr/sbin/dbconverter-2 $(TMP_MIT)/usr/sbin/sasldbconverter2

	# Install sample-{client,server} with Debianized names
	install -m 755 -D sample/sample-client \
		$(TMP_MIT)/usr/bin/sasl-sample-client
	install -m 755 -D sample/sample-server \
		$(TMP_MIT)/usr/sbin/sasl-sample-server

	# Alter the rpath of certain binaries and shared libraries.
	chrpath -d $(TMP_MIT)/usr/sbin/sasldblistusers2 \
		$(TMP_MIT)/usr/sbin/saslpasswd2
ifeq (,$(findstring no-gssapi,$(DEB_BUILD_OPTIONS))$(findstring pkg.cyrus-sasl2.nogssapi,$(DEB_BUILD_PROFILES)))
	chrpath -d $(TMP_HEIMDAL)/usr/lib/$(DEB_HOST_MULTIARCH)/sasl2/libgs2.so.*.*.*
	chrpath -d $(TMP_HEIMDAL)/usr/lib/$(DEB_HOST_MULTIARCH)/sasl2/libgssapiv2.so.*.*.*
endif
ifeq (,$(findstring no-sql,$(DEB_BUILD_OPTIONS))$(findstring pkg.cyrus-sasl2.nosql,$(DEB_BUILD_PROFILES)))
	chrpath -d $(TMP_MIT)/usr/lib/$(DEB_HOST_MULTIARCH)/sasl2/libsql.so.*.*.*
endif

	# Install the sasl-sample-client and -server man pages.
	dh_installman -psasl2-bin sasl-sample-client.8 sasl-sample-server.8

	# Install saslfinger
	install -m 644 -D debian/saslfinger/saslfinger.1 \
		$(TMP_MIT)/usr/share/man/man1/saslfinger.1
	install -m 755 -D debian/saslfinger/saslfinger \
		$(TMP_MIT)/usr/bin/saslfinger

	# Install gen-auth
	install -m 755 -D debian/gen-auth/gen-auth \
		$(TMP_MIT)/usr/bin/gen-auth

override_dh_install:
	dh_install -Nlibsasl2-modules-gssapi-heimdal --sourcedir=$(TMP_MIT)
	dh_install -plibsasl2-modules-gssapi-heimdal --sourcedir=$(TMP_HEIMDAL)

override_dh_missing:
	dh_missing --fail-missing

override_dh_installinit:
	dh_installinit --no-enable $(DH_PACKAGE_EXCLUDES) --name=saslauthd start 20 2 3 4 5 . stop 20 0 1 6 .

override_dh_installchangelogs:
	dh_installchangelogs -XChangeLog

override_dh_strip:
	dh_strip -O--dbgsym-migration='cyrus-sasl2-dbg (<< 2.1.26-72-g88d82a3+dfsg-1~), cyrus-sasl2-mit-dbg (<< 2.1.26-72-g88d82a3+dfsg-1~), cyrus-sasl2-heimdal-dbg (<< 2.1.26-72-g88d82a3+dfsg-1~)'

override_dh_makeshlibs:
	dh_makeshlibs -X/usr/lib/$(DEB_HOST_MULTIARCH)/sasl2 $(DH_PACKAGE_EXCLUDES)

override_dh_auto_test:
	cd $(TMPBUILD_MIT)/saslauthd && $(MAKE) testsaslauthd
	cd $(TMPBUILD_MIT)/utils && $(MAKE) testsuite
