#!/usr/bin/make -f
# debian/rules file - for OSSO-GNUPG (1.4.2)
# Based on sample debian/rules file - for GNU Hello (1.3).
# Copyright 1994,1995 by Ian Jackson.
# Copyright 1998-2005 by James Troup.
# 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

STRIP=strip --remove-section=.comment --remove-section=.note

DEB_BUILD_GNU_TYPE = $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE = $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
CONFARGS = --host=$(DEB_HOST_GNU_TYPE)
endif

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

VERSION := $(shell dpkg-parsechangelog | grep ^Version: | cut -d' ' -f2)
DEB_BUILD_ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)

include /usr/share/dpatch/dpatch.make

configure: autogen.sh
	./autogen.sh

build-deb-stamp: patch-stamp configure
	$(checkdir)
	mkdir -p build-deb
	cd build-deb && ../configure --prefix=/usr --libexecdir=/usr/lib/ --without-readline --without-libusb --without-ldap --without-libcurl --disable-mailto --enable-minimal $(CONFARGS)
	$(MAKE) -C build-deb
	touch $@

build: build-deb-stamp

test: build
	$(checkdir)
	make -C build-deb/checks check || exit 127
	touch test

clean: unpatch
	$(checkdir)
	-rm -f build
	-$(MAKE) -C build-deb -i distclean || $(MAKE) -C build-deb -f Makefile.in distclean
	rm -rf build-deb
	-rm -rf debian/tmp debian/files* debian/*substvars debian/patched
	find . -name \*~ | xargs rm -vf

binary-indep:

binary-arch:	checkroot build # test
	$(checkdir)
	-rm -rf debian/tmp
	$(install_dir) debian/tmp/DEBIAN/
	$(install_script) debian/preinst debian/tmp/DEBIAN/
	$(MAKE) -C build-deb prefix=`pwd`/debian/tmp/usr mandir=`pwd`/debian/tmp/usr/share/man libexecdir=`pwd`/debian/tmp/usr/lib/ mkinstalldirs=`pwd`/scripts/mkinstalldirs install
	: # copies of the manpage which can't be grokked by install-info
	rm debian/tmp/usr/info/*
	: # shouldn't be here; it's a symlink to /etc/locale.alias in Debian
	rm -f debian/tmp/usr/share/locale/locale.alias
	find debian/tmp/ -type f | xargs file | grep ELF | cut -d: -f 1 | xargs $(STRIP)
	chmod 4755 debian/tmp/usr/bin/gpg
	sed -e "s#../g10/gpg#gpg#" < tools/lspgpot > debian/tmp/usr/bin/lspgpot
	sed -e "s#../g10/gpg#gpg#" < tools/convert-from-106 > debian/tmp/usr/bin/gpg-convert-from-106
	chmod 755 debian/tmp/usr/bin/lspgpot debian/tmp/usr/bin/gpg-convert-from-106
	sed -e "s#/usr/local/#/usr/#" < debian/tmp/usr/share/man/man1/gpg.1 \
	    > debian/tmp/usr/share/man/man1/gpg.1.new
	mv debian/tmp/usr/share/man/man1/gpg.1.new debian/tmp/usr/share/man/man1/gpg.1
	gzip -9v debian/tmp/usr/share/man/man*/*
	: # Move localized manpages to FHS compliant locations
	$(install_dir) debian/tmp/usr/share/man/ru/man1/
	mv debian/tmp/usr/share/man/man1/gpg.ru.1.gz debian/tmp/usr/share/man/ru/man1/gpg.1.gz
	: # Remove from /usr/share/gnupg what we install into /usr/share/doc/gnupg/
	rm debian/tmp/usr/share/gnupg/FAQ debian/tmp/usr/share/gnupg/faq.html
	$(install_dir) debian/tmp/usr/share/doc/gnupg/
	$(install_file) debian/changelog debian/tmp/usr/share/doc/gnupg/changelog.Debian
	$(install_file) debian/README.Debian README NEWS THANKS TODO doc/DETAILS \
		doc/FAQ doc/faq.html doc/OpenPGP debian/Upgrading_From_PGP.txt \
		doc/highlights-1.4.txt debian/tmp/usr/share/doc/gnupg/
	for i in po util mpi cipher tools g10 checks include; do \
		$(install_file) $$i/ChangeLog debian/tmp/usr/share/doc/gnupg/changelog.$$i; done
	$(install_file) ChangeLog debian/tmp/usr/share/doc/gnupg/changelog.toplevel
	gzip -9v debian/tmp/usr/share/doc/gnupg/*
	ln -s changelog.g10.gz debian/tmp/usr/share/doc/gnupg/changelog.gz
	$(install_file) debian/copyright debian/tmp/usr/share/doc/gnupg/
	$(install_dir) debian/tmp/usr/share/lintian/overrides/
	$(install_file) debian/lintian.override debian/tmp/usr/share/lintian/overrides/gnupg

	find debian/tmp/ -type d -empty | xargs rmdir
	rm -rf debian/tmp/usr/share/doc 
	rm -rf debian/tmp/usr/share/locale
	rm -rf debian/tmp/usr/share/man

	find debian/tmp/ -type f | xargs file | grep ELF | cut -d: -f 1 | xargs dpkg-shlibdeps
	dpkg-gencontrol -posso-gnupg -isp
	chown -R root.root debian/tmp
	chmod -R go=rX debian/tmp
	dpkg --build debian/tmp ..

define checkdir
	test -f g10/g10.c && test -f debian/rules
endef

# Below here is fairly generic really

binary: 	binary-indep binary-arch

checkroot:
	$(checkdir)
	test root = "`whoami`"

.PHONY: build binary binary-arch binary-indep clean checkroot
