#! /usr/bin/make -f
#
# rules for mailcrypt
#
# Copyright (C) 1997, 98, 99, 2000, 01, 02, 03, 04, 05, 06, 07
# by Davide G. M. Salvetti.
#
# 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.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to: The Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
# On Debian GNU/Linux System you can find a copy of the GNU General Public
# License in "/usr/share/common-licenses/GPL".

checkdir=test -f debian/rules
checkroot=test root = "$$(whoami)"
fixperms=chmod -R a+rX .

tmp := $(shell pwd)/debian/tmp

SHELL := /bin/bash
INSTDIR := install -m 755 -d
INSTPROG := install -m 755 -s
INSTDATA := install -m 644
INSTSCRIPT := install -m 755
STRIP := strip -R .note -R .comment

DFILES := debian/README debian/copyright \
debian/config debian/postinst debian/prerm debian/postrm \
debian/docbase debian/lintian \
debian/bug.presubj debian/bug.script \
debian/install debian/remove

DCSOURCES := $(shell cat debian/po/POTFILES.in \
		| perl -pe 's|^\[[^]]+\] +(.*)$$|debian/$${1}|')
DFILES += $(patsubst %templates, %templates.dcobj, $(DCSOURCES))

SCRIPTS := configure install-sh
SOURCES := *.in *.el load-path.hack
MAKEFILEHACKS := tests/Makefile.in tests/remailer/Makefile.in

.PHONY: pre-build
pre-build:
	autoconf || true

debian/rules: debian/variables

%:: %.in
	eperl -d DEBIAN_DIR="$(shell pwd)/debian/" \
		-I "$(shell pwd)/debian/" -P -o $@ $<

debian/po/templates.pot: $(DCSOURCES)
	umask 0022 && debconf-updatepo --verbose --podir=debian/po/
	@touch $@

%.dcobj: % debian/po/templates.pot
	po2debconf --verbose --podir debian/po $< > $@

build: $(DFILES) debian/po/templates.pot
	$(checkdir)
	chmod a+x ./configure;		./configure --infodir=/usr/share/info EMACS=echo
	$(MAKE) MAKEINFO="makeinfo --verbose" \
		TEXI2HTML="texi2html --Verbose" \
		info html
	touch build

.PHONY: clean
clean: debian/po/templates.pot
	$(checkdir)
	-rm -rf build core $(tmp) debian/{files*,substvars}
	-rm -f debian/po/updatepo
	[ ! -f Makefile ] || $(MAKE) distclean
	-rm -f $(DFILES)
	$(fixperms)

.PHONY: binary
binary: binary-arch binary-indep

.PHONY: binary-indep
binary-indep: build
	$(checkdir)
	$(checkroot)
	-rm -rf $(tmp)

	$(INSTDIR) $(tmp)/usr/share/emacs/site-lisp/mailcrypt
	$(INSTDATA) debian/mailcrypt-init.el $(tmp)/usr/share/emacs/site-lisp/mailcrypt/../
	$(INSTSCRIPT) $(SCRIPTS) $(tmp)/usr/share/emacs/site-lisp/mailcrypt/
	$(INSTDATA) $(SOURCES) $(tmp)/usr/share/emacs/site-lisp/mailcrypt/
	umask 0022; for i in $(MAKEFILEHACKS); do \
		$(INSTDIR) $(tmp)/usr/share/emacs/site-lisp/mailcrypt/$${i%/*}/; \
		echo -e "clean distclean:\n\trm -f Makefile\n" \
			> $(tmp)/usr/share/emacs/site-lisp/mailcrypt/$${i}; \
	done
	$(MAKE) INSTALL_INFO="echo install-info" \
		install-info infodir=$(tmp)/usr/share/info
	gzip -9frv $(tmp)/usr/share/info

	$(INSTDIR) $(tmp)/usr/share/doc/mailcrypt
	$(INSTDATA) ANNOUNCE ChangeLog.1 NEWS ONEWS README README.gpg WARNINGS $(tmp)/usr/share/doc/mailcrypt
	$(INSTDATA) debian/README $(tmp)/usr/share/doc/mailcrypt/README.Debian
	$(INSTDATA) ChangeLog $(tmp)/usr/share/doc/mailcrypt
	$(INSTDATA) debian/changelog $(tmp)/usr/share/doc/mailcrypt/changelog.Debian
	gzip -9frv $(tmp)/usr/share/doc/mailcrypt
	ln -sf ChangeLog.gz $(tmp)/usr/share/doc/mailcrypt/changelog.gz
	$(INSTDATA) debian/copyright $(tmp)/usr/share/doc/mailcrypt
	$(INSTDIR) $(tmp)/usr/share/doc/mailcrypt/HTML
	$(INSTDATA) htmls/*.html $(tmp)/usr/share/doc/mailcrypt/HTML
	ln -snf mailcrypt_toc.html $(tmp)/usr/share/doc/mailcrypt/HTML/index.html

	$(INSTDIR) $(tmp)/usr/lib/emacsen-common/packages/install
	$(INSTDIR) $(tmp)/usr/lib/emacsen-common/packages/remove
	$(INSTSCRIPT) debian/install \
		$(tmp)/usr/lib/emacsen-common/packages/install/mailcrypt
	$(INSTSCRIPT) debian/remove \
		$(tmp)/usr/lib/emacsen-common/packages/remove/mailcrypt
	$(INSTDIR) $(tmp)/usr/share/doc-base
	$(INSTDATA) debian/docbase $(tmp)/usr/share/doc-base/mailcrypt
	$(INSTDIR) $(tmp)/usr/share/bug/mailcrypt
	$(INSTDATA) debian/bug.presubj $(tmp)/usr/share/bug/mailcrypt/presubj
	$(INSTSCRIPT) debian/bug.script $(tmp)/usr/share/bug/mailcrypt/script
	$(INSTDIR) $(tmp)/usr/share/lintian/overrides
	$(INSTDATA) debian/lintian $(tmp)/usr/share/lintian/overrides/mailcrypt

	$(INSTDIR) $(tmp)/DEBIAN
	$(INSTDATA) debian/templates.dcobj \
		$(tmp)/DEBIAN/templates
	$(INSTSCRIPT) debian/{config,postinst,postrm,prerm} \
		$(tmp)/DEBIAN

	cd $(tmp) && md5sum \
		$$(find ./ -path './DEBIAN' -prune \
		-o -type f -printf "%P\n") \
		| sort -k 2 -o $(tmp)/DEBIAN/md5sums
	dpkg-gencontrol -isp
	chown -R root.root $(tmp)
	chmod -R go=rX $(tmp)
	dpkg --build $(tmp) ..

.PHONY: binary-arch
binary-arch: build
	$(checkdir)
	$(checkroot)

.PHONY: build-package
build-package: clean
	dpkg-buildpackage -rfakeroot -tc -i

.PHONY: source diff
source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

# arch-tag: 7f6799c7-dca1-4d84-8038-1ad9c2888751-child
