#!/usr/bin/make -f
# Based on sample debian/rules that uses debhelper.
# GNU copyright 1997 by Joey Hess.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS

SHELL=/bin/bash
export SHELL


export DEB_HOST_MULTIARCH


# The flags to pass to dh_install specifying the upstream files to exclude.
# We use --fail-missing to be sure we catch any new upstream files, so be
# sure to update this list if upstream adds any more files we don't want.
EXCLUDE = -Xtmac.doc -Xexamples/krb5 -Xgnats/mit -Xkrb5-send-pr \
	-Xsserver -Xsim_server -Xuuserver \
	-Xsclient -Xsim_client -Xuuclient -Xpreauth/test.so

LIB_PACKAGES = libkrb5-3 libgssapi-krb5-2 libkadm5clnt-mit12 libkadm5srv-mit12 libkdb5-10 libgssrpc4  \
	libkrb5support0  libk5crypto3 libkrad0

ifneq (,$(filter stage1,$(DEB_BUILD_PROFILES)))
KRB5_SKIP_LDAP=        1
endif

ifeq (,$(KRB5_SKIP_LDAP))
CONFIGURE_LDAP=        --with-ldap
else
CONFIGURE_LDAP=        --without-ldap
endif
DH_BUILD_OPTS= --sourcedir=src --builddir=build

build: build-arch build-indep


override_dh_auto_configure:
	dh_auto_configure $(DH_BUILD_OPTS) -- --with-system-et --with-system-ss --disable-rpath  \
		--enable-shared $(CONFIGURE_LDAP) --without-tcl \
	--with-system-verto \
		--libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
		--sysconfdir=/etc  \
		--localstatedir=/etc

override_dh_auto_build:
	dh_auto_build $(DH_BUILD_OPTS)

# Build the documentation in a separate directory, since otherwise we'll
# overwrite the info pages provided upstream and then debian/rules clean won't
build-indep: build-indep-stamp
# build-indep should not need to depend on build-arch, but currently it does at least to populate build/doc
# Add the dependency until we get a chance to make it work properly
build-indep-stamp:  build-arch
ifeq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
	cd build/doc && make PYTHON=python3 substhtml substpdf
	ln -sf /usr/share/javascript/jquery/jquery.js build/doc/html_subst/_static/jquery.js
	ln -sf /usr/share/javascript/underscore/underscore.js build/doc/html_subst/_static/underscore.js
	ln -sf /usr/share/javascript/sphinxdoc/1.0/doctools.js build/doc/html_subst/_static/doctools.js
	ln -sf /usr/share/javascript/sphinxdoc/1.0/searchtools.js build/doc/html_subst/_static/searchtools.js
	touch build-indep-stamp
endif

clean:
	dh_testdir
	-rm -rf  build doc/tools/*.pyc doc/version.py
	dh_clean build-indep-stamp


override_dh_auto_install:

	dh_auto_install $(DH_BUILD_OPTS)
	install -d $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/krb5 $(CURDIR)/debian/tmp/etc/insserv/overrides
ifeq (,$(KRB5_SKIP_LDAP))
	install -m644 debian/krb5-kdc-ldap.insserv-override debian/tmp/etc/insserv/overrides/krb5-kdc
	mv $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libkdb_ldap* \
	    $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/krb5/
	rm -f $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/krb5/libkdb_ldap*.so
else
	rm -f $(CURDIR)/debian/tmp/usr/share/man/man8/kdb5_ldap_util.8
endif
	mv $(CURDIR)/debian/tmp/usr/bin/krb5-config \
	    $(CURDIR)/debian/tmp/usr/bin/krb5-config.mit
	mv $(CURDIR)/debian/tmp/usr/share/man/man1/krb5-config.1 \
	    $(CURDIR)/debian/tmp/usr/share/man/man1/krb5-config.mit.1

	install -d $(CURDIR)/debian/tmp/usr/share/aclocal
	install -m644 src/util/ac_check_krb5.m4 \
		$(CURDIR)/debian/tmp/usr/share/aclocal


override_dh_install-arch:
	dh_install   $(EXCLUDE) --sourcedir=.
	set -e ; find debian/krb5-multidev/usr/lib/$(DEB_HOST_MULTIARCH)/mit-krb5 -type l -name \*.so -print |\
		while read linkname; do \
		ln -s -f ../`readlink $$linkname` \
		$$linkname; \
	done
		rm debian/krb5-multidev/usr/include/mit-krb5/krad.h debian/krb5-multidev/usr/lib/*/mit-krb5/libkrad.so
		for dir in include lib/$(DEB_HOST_MULTIARCH) lib/$(DEB_HOST_MULTIARCH)/pkgconfig; do \
		(cd debian/krb5-multidev/usr/$$dir/mit-krb5 && \
		find . -type d -print ) | (cd debian/libkrb5-dev/usr/$$dir && \
		xargs mkdir -p); \
		(cd debian/krb5-multidev/usr/$$dir/mit-krb5 && find . \( -type f -o -type l \) -print ) | \
		(cd debian/libkrb5-dev/usr/$$dir && xargs -I+ ln -s /usr/$$dir/mit-krb5/+ +) ; \
		done
	# however we will handle libkadm5{srv,clnt.so} in dh_link
# because they actually point to the current level not one level up
	rm -f debian/krb5-multidev/usr/lib/$(DEB_HOST_MULTIARCH)/mit-krb5/libkadm5{clnt,srv}.so


	docbook-to-man debian/krb5_newrealm.sgml \
		> debian/krb5-admin-server/usr/share/man/man8/krb5_newrealm.8
	install -o root -g root -m 755 debian/krb5_newrealm \
		debian/krb5-admin-server/usr/sbin
	install -o root -g root -m 644 debian/kdc.conf \
		debian/krb5-kdc/usr/share/krb5-kdc/kdc.conf.template
	ln -s /usr/share/krb5-kdc/kdc.conf.template \
		debian/krb5-kdc/usr/share/doc/krb5-kdc/examples/kdc.conf

override_dh_installinit:
	# We don't need to override systemd because the systemd behavior is reasonable, and we're just trying to work around issues with error handling in init scripts
	DH_OPTIONS= dh_installinit -pkrb5-kdc --error-handler=init_error -- defaults 18 18
	DH_OPTIONS= dh_installinit -pkrb5-kpropd -- defaults 18 18
		DH_OPTIONS= dh_installinit -pkrb5-admin-server -- defaults 18 18

override_dh_strip:
	set -e ; for pkg in $(LIB_PACKAGES) ; do \
		DH_OPTIONS="" dh_strip -p$$pkg --dbg-package=libkrb5-dbg; \
	done
	dh_strip

override_dh_makeshlibs:
	dh_makeshlibs -Xkrb5/plugins --  -c4

override_dh_fixperms-arch:
	dh_fixperms
	chmod u+s debian/krb5-user/usr/bin/ksu
	chmod 700 debian/krb5-kdc/var/lib/krb5kdc
	chmod 700 debian/krb5-kdc/etc/krb5kdc

override_dh_auto_test: 

%:
	dh $@ 


#.PHONY: build clean configure binary-indep binary-arch binary install