#!/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

# Tell Autoconf the correct system types.  Needed for cross builds.
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
    SYSTEM = --build $(DEB_HOST_GNU_TYPE)
    CACHE  =
else
    SYSTEM = --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
    CACHE  = --cache-file=$(DEB_HOST_GNU_TYPE).cache
endif

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

ifneq (,$(filter i486-linux-gnu x86_64-linux-gnu,$(DEB_HOST_GNU_TYPE)))
    CCOPTS +=-D_FORTIFY_SOURCE=2 -fstack-protector
    endif

LINTIAN_PACKAGES = krb5-user krb5-kdc libkadm55 libkrb53

# 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 -Xgss-server \
	-Xsclient -Xsim_client -Xuuclient -Xgss-client

# We touch each configure and Autoconf-related file so that we do not attempt
# to use Autoconf.  The cache is used by the Embdebian project for cross
# compiles.
configure: configure-stamp
configure-stamp:
	dh_testdir
	mkdir -p build
	find src -name configure -print | xargs touch
	find src \( -name \*hin -o -name \*.h.in -o -name \*.stmp \) -print \
		| xargs touch
	[ ! -f $(DEB_HOST_GNU_TYPE).cache ] \
		|| cp $(DEB_HOST_GNU_TYPE).cache build/
	cd build && ../src/configure CFLAGS="$(CCOPTS) -D_REENTRANT" \
		--prefix=/usr --localstatedir=/etc --mandir=/usr/share/man \
		--with-system-et --with-system-ss --enable-fakeka \
		--enable-shared --with-ldap --without-tcl \
		$(SYSTEM) $(CACHE)
	touch configure-stamp

# Build the info pages in a separate directory, since otherwise we'll
# overwrite the info pages provided upstream and then debian/rules clean won't
# get back to a virgin copy of the package.
build: build-stamp
build-stamp: configure-stamp
	cd build && $(MAKE) all
	-mkdir build/info
	cd build/info && makeinfo ../../doc/admin.texinfo
	cd build/info && makeinfo ../../doc/install.texinfo
	cd build/info && makeinfo ../../doc/user-guide.texinfo
	cd build/info && makeinfo ../../doc/krb425.texinfo
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	-rm -rf build
	dh_clean build-stamp configure-stamp

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	cd build && $(MAKE) install DESTDIR=`pwd`/../debian/tmp
	install -d $(CURDIR)/debian/tmp/usr/lib/krb5
	mv $(CURDIR)/debian/tmp/usr/lib/libkdb_ldap* \
	    $(CURDIR)/debian/tmp/usr/lib/krb5/
	rm -f $(CURDIR)/debian/tmp/usr/lib/krb5/libkdb_ldap*.so

	install -m644 src/util/ac_check_krb5.m4 \
		debian/libkrb5-dev/usr/share/aclocal
	mv debian/tmp/usr/bin/telnet debian/tmp/usr/bin/telnet.krb5
	mv debian/tmp/usr/share/man/man1/telnet.1 \
		debian/tmp/usr/share/man/man1/telnet.krb5.1
	for foo in rlogin rsh ftp rcp ; do \
		mv debian/tmp/usr/bin/$$foo debian/tmp/usr/bin/krb5-$$foo; \
		mv debian/tmp/usr/share/man/man1/$$foo.1 \
			debian/tmp/usr/share/man/man1/krb5-$$foo.1 ; \
	done

	dh_install --sourcedir=debian/tmp --fail-missing $(EXCLUDE)

	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
	for foo in $(LINTIAN_PACKAGES) ; do \
		install -d debian/$$foo/usr/share/lintian/overrides; \
		cp debian/$$foo.lintian \
			debian/$$foo/usr/share/lintian/overrides/$$foo; \
	done

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: DH_OPTIONS=-i
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs  doc/CHANGES
	dh_installdocs
	dh_installinfo
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
# Pass -a to all debhelper commands in this target to reduce clutter.  Strip
# libkrb53 and libkadm55 separately and save the debug information for the
# libkrb5-dbg package.  This method strips the libraries in those packages
# twice, but that should be harmless and all other ways of doing this seem
# uglier.
binary-arch: DH_OPTIONS=-a
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installdebconf
	dh_installinit -- defaults 18 18
	DH_OPTIONS="" dh_strip -plibkrb53 --dbg-package=libkrb5-dbg
	DH_OPTIONS="" dh_strip -plibkadm55 --dbg-package=libkrb5-dbg
	dh_strip
	dh_link
	dh_compress
	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
	DH_OPTIONS="" dh_makeshlibs -plibkrb53 '-Vlibkrb53 (>= 1.6.dfsg.2)'
	DH_OPTIONS="" dh_makeshlibs -plibkadm55
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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