#!/usr/bin/make -f
# debian/rules file - for GNUPG (1.9)
# Copyright 1994,1995 by Ian Jackson.
# Copyright 1998-2003 by James Troup.
# Copyright 2003-2004 by Matthias Urlichs.
# 
# I hereby give you perpetual unlimited permission to copy,
# modify and relicense this file, provided that you do not remove
# my name from the file itself.  (I assert my moral right of
# paternity under the Copyright, Designs and Patents Act 1988.)
# This file may have to be extensively modified

export DEB_BUILD_HARDENING=1

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
export DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	confflags += --build $(DEB_HOST_GNU_TYPE)
else
	confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

CFLAGS=-Wall -g

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

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

install_dir=install -d -m 755
install_file=install -m 644
install_script=install -m 755
install_binary=install -m 755 -s

config: config-stamp
config-stamp: configure
	dh_testdir
	chmod +x configure tests/runtest
	dh_autoreconf
	env CFLAGS="$(CFLAGS)" \
	./configure $(confflags) --enable-maintainer-mode \
		--prefix=/usr --with-included-gettext \
		--with-zlib=/usr --infodir=/usr/share/info/  \
		--mandir='$${prefix}/share/man' --libexecdir=/usr/lib/gnupg2 \
		--sysconfdir=/etc --enable-gpg --enable-symcryptrun

	touch config-stamp

build: build-stamp
build-stamp: config
	$(MAKE)

	touch build-stamp

test: test-stamp
test-stamp: build-stamp
	dh_testdir
#	$(MAKE) check
	touch test-stamp

clean:
	dh_testdir
	-rm -f config-stamp build-stamp test-stamp
	[ ! -f Makefile ] || $(MAKE) distclean

	dh_autoreconf_clean
	dh_clean

	test -r /usr/share/misc/config.sub && \
		cp -f /usr/share/misc/config.sub config.sub || \
		test -r config.sub
	test -r /usr/share/misc/config.guess && \
		cp -f /usr/share/misc/config.guess config.guess || \
		test -r config.guess

	if test -f version ; then touch version; else \
		dpkg-parsechangelog | sed -ne 's/^Version:[     ]*//p' > version; fi


binary-common:
	dh_testdir
	dh_testroot
	dh_clean -k

	dh_installdirs

	for i in */ChangeLog; do \
		$(install_file) $$i debian/gnupg2/usr/share/doc/gnupg2/changelog.$$(dirname $$i); done
	mv debian/gnupg2/usr/share/doc/gnupg2/changelog.agent debian/gnupg-agent/usr/share/doc/gnupg-agent/changelog
	mv debian/gnupg2/usr/share/doc/gnupg2/changelog.sm debian/gpgsm/usr/share/doc/gpgsm/changelog
	ln -s changelog.g10.gz debian/gnupg2/usr/share/doc/gnupg2/changelog.gz
	$(install_file) agent/ChangeLog debian/gnupg-agent/usr/share/doc/gnupg-agent/changelog
	$(install_file) sm/ChangeLog debian/gpgsm/usr/share/doc/gpgsm/changelog

binary-indep:	build binary-common
# Nothing to do

binary-arch:	build test binary-common
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
	# shouldn't be here; it's a symlink to /etc/locale.alias in Debian
	-rm -f debian/tmp/usr/share/locale/locale.alias

	dh_install -a
	find debian/tmp -type d -o -ls

	dh_installdocs -a
	dh_installexamples -a
	dh_installinfo -a
	dh_installchangelogs -a
	dh_installudev -a
	dh_lintian -a

	$(install_file) debian/gnupg-agent.xsession debian/gnupg-agent/etc/X11/Xsession.d/90gpg-agent

	dh_strip -a
	dh_compress -a
	dh_fixperms -a

	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a -Xgnupg2/gpg2keys_ldap -- \
		-dRecommends debian/tmp/usr/lib/gnupg2/gpg2keys_ldap -dDepends
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

# Below here is fairly generic really

binary: 	binary-indep binary-arch

.PHONY: binary binary-arch binary-indep clean test build binary-common
