#!/usr/bin/make -f
# This file is PUBLIC DOMAIN.
#
# Copyright © 2001,2003, 2009, 2010 Jeff Bailey, Jordi Mallach.

include /usr/share/cdbs/1/class/autotools.mk
include /usr/share/cdbs/1/rules/autoreconf.mk
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/rules/utils.mk

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
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))
	CONFIGURE_FLAGS += --build=$(DEB_BUILD_GNU_TYPE)
else
	CONFIGURE_FLAGS += --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
endif

# Work-around for upstream rules that try to use $(HOME), which should
# never be touched during the package build
export HOME=$(CURDIR)/junkhome

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
   RUNTESTFLAGS = -v -v -v --debug
endif

DEB_CONFIGURE_USER_FLAGS += \
	--with-lispdir=\$${prefix}/share/emacs/site-lisp \
	--enable-virtual-domains --enable-mh \
	--with-gnutls --with-gdbm --with-gssapi --with-gsasl \
	--with-mysql --disable-rpath 

ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
DEB_MAKE_CHECK_TARGET =	check RUNTESTFLAGS="$(RUNTESTFLAGS)"
else
DEB_MAKE_CHECK_TARGET =
endif

clean::
	rm -f *.1 *.8

install/mailutils::
	# These binaries conflict with elm-me. We need to rename them, as
	# we're using alternatives for them.
	cd debian/tmp; \
	for i in dotlock frm from messages mail movemail readmsg; do \
	   mv usr/bin/$$i usr/bin/$$i.mailutils; \
	done

	# Generate manpages, but remove the s[ug]id bits from binaries
	# so LD_LIBRARY_PATH works.
	chmod 755 $(CURDIR)/debian/tmp/usr/bin/dotlock.mailutils
	sh debian/mangen.sh $(CURDIR)/debian/tmp/usr
	# Restore correct perms to the binaries that need it.
	chmod 2755 $(CURDIR)/debian/tmp/usr/bin/dotlock.mailutils

binary-install/python-mailutils::
	dh_python2 -p$(cdbs_curpkg) --no-guessing-versions

DEB_FIXPERMS_EXCLUDE = dotlock.mailutils

DEB_DH_INSTALL_ARGS += --list-missing

DEB_INSTALL_DOCS_mailutils = AUTHORS NEWS README THANKS TODO
DEB_INSTALL_DOCS_mailutils-mh = mh/TODO

DEB_SHLIBDEPS_INCLUDE := $(CURDIR)/debian/libmailutils2/usr/lib

ORIG_VERSION := $(shell dpkg-parsechangelog | awk '/^Version:/ { sub(".*:", "", $$2); sub("\+.*", "", $$2); print $$2 }')
get-orig-source::
	uscan -verbose --force-download --no-symlink --destdir ../tarballs
	tar -C ../tarballs -jxf ../tarballs/mailutils-$(ORIG_VERSION).tar.bz2
	rm -rf ../tarballs/mailutils-$(ORIG_VERSION)/doc/texinfo
	sed -i -e's/ texinfo//g' ../tarballs/mailutils-$(ORIG_VERSION)/doc/Makefile.*
	sed -i -e'/ doc\/texinfo\/Makefile/d' ../tarballs/mailutils-$(ORIG_VERSION)/configure.ac
	sed -i -e'/CONFIG_FILES doc\/texinfo\/Makefile/d' ../tarballs/mailutils-$(ORIG_VERSION)/configure
	sed -i -e's/ doc\/texinfo\/Makefile//' ../tarballs/mailutils-$(ORIG_VERSION)/configure
	cd ../tarballs && tar jcf mailutils_$(ORIG_VERSION)+dfsg1.orig.tar.bz2 mailutils-$(ORIG_VERSION)
	rm -rf ../tarballs/mailutils-$(ORIG_VERSION) ../tarballs/mailutils-$(ORIG_VERSION).tar.bz2
