#!/usr/bin/make -f
#
# $Id: rules 504 2010-02-02 11:40:24Z hector $
#
# Originally by Henrique M. Holschuh <hmh@debian.org>, and based on
# previous work by Paul Haggard <phaggart@debian.org>, and in a
# debmake-created rules file.
#
# Special package build-time options:
#   POP2, RPA, NTLM, SDPS, OPIE, KRB4, KRB5, GSSAPI
#   NOPOP3, NOIMAP, NOETRN, NOODMR, IPV6, IPV6SEC
#
# To use them, add the ones you need to the environment variable 
# DEB_FETCHMAIL_BUILD_OPTIONS before building the package.
#
# e.g.
# export DEB_FETCHMAIL_BUILD_OPTIONS="KRB4,NOIMAP,NOETRN,NOODMR" 
# dpkg-buildpackage -rfakeroot -uc -us
#
# If DEB_FETCHMAIL_BUILD_OPTIONS is undefined, SSl, NTLM and SDPS will
# be enabled by default.
#
# The targets KRB4, KRB5, GSSAPI and OPIE require the proper libraries 
# to be installed in the system.
#
# IPV6 and IPV6SEC support is untested, and breaks the 'interface'
# keyword (which is why they will not be enabled by default).

export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
PYVER=$(shell pyversions -vd)

# quilt support
include /usr/share/quilt/quilt.make

# for autoconf 2.52 and newer only
CONFFLAGS =
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

# Defaults for official debian package
ifeq (,$(DEB_FETCHMAIL_BUILD_OPTIONS))
	DEB_FETCHMAIL_BUILD_OPTIONS="SSL,NTLM,SDPS,KRB5,GSSAPI"
endif

# Process build-time options
FETCHCONFOPT =
ifneq (,$(findstring SSL,$(DEB_FETCHMAIL_BUILD_OPTIONS)))
	FETCHCONFOPT += --with-ssl=/usr
endif
ifneq (,$(findstring POP2,$(DEB_FETCHMAIL_BUILD_OPTIONS)))
	FETCHCONFOPT += --enable-POP2
endif
ifneq (,$(findstring RPA,$(DEB_FETCHMAIL_BUILD_OPTIONS)))
	FETCHCONFOPT += --enable-RPA
endif
ifneq (,$(findstring NTLM,$(DEB_FETCHMAIL_BUILD_OPTIONS)))
	FETCHCONFOPT += --enable-NTLM
endif
ifneq (,$(findstring SDPS,$(DEB_FETCHMAIL_BUILD_OPTIONS)))
	FETCHCONFOPT += --enable-SDPS
endif
ifneq (,$(findstring OPIE,$(DEB_FETCHMAIL_BUILD_OPTIONS)))
	FETCHCONFOPT += --enable-opie
endif
ifneq (,$(findstring KRB4,$(DEB_FETCHMAIL_BUILD_OPTIONS)))
	FETCHCONFOPT += --with-kerberos=/usr
endif
ifneq (,$(findstring KRB5,$(DEB_FETCHMAIL_BUILD_OPTIONS)))
	FETCHCONFOPT += --with-kerberos5
endif
ifneq (,$(findstring GSSAPI,$(DEB_FETCHMAIL_BUILD_OPTIONS)))
	FETCHCONFOPT += --with-gssapi=/usr
endif
ifneq (,$(findstring NOPOP3,$(DEB_FETCHMAIL_BUILD_OPTIONS)))
	FETCHCONFOPT += --disable-POP3
endif
ifneq (,$(findstring NOIMAP,$(DEB_FETCHMAIL_BUILD_OPTIONS)))
	FETCHCONFOPT += --disable-IMAP
endif
ifneq (,$(findstring NOETRN,$(DEB_FETCHMAIL_BUILD_OPTIONS)))
	FETCHCONFOPT += --disable-ETRN
endif
ifneq (,$(findstring NOODMR,$(DEB_FETCHMAIL_BUILD_OPTIONS)))
	FETCHCONFOPT += --disable-ODMR
endif
ifneq (,$(findstring IPV6,$(DEB_FETCHMAIL_BUILD_OPTIONS)))
	FETCHCONFOPT += --enable-inet6
endif

# enable hardening
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

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

clean: unpatch
	dh_testdir
	dh_testroot
	-rm -f build-stamp configure-stamp
	[ ! -f Makefile ] || $(MAKE) distclean
	-rm -f po/*.gmo config.sub config.guess config.status
	dh_clean -X.orig -X.rej

configure: configure-stamp
configure-stamp: patch
	set -e
	dh_testdir

	cp /usr/share/misc/config.sub config.sub
	cp /usr/share/misc/config.guess config.guess

	./configure $(CONFFLAGS) --prefix=/usr --enable-nls \
		--disable-fallback $(FETCHCONFOPT)

	sed -e "s/-lcrypt//; s/-lk5crypto//g;"  < Makefile > Makefile.tmp
	mv Makefile.tmp Makefile

	touch configure-stamp

build: configure-stamp build-arch
build-arch: configure-stamp build-stamp
build-indep: configure-stamp build-stamp

build-stamp:
	set -e
	dh_testdir

	$(MAKE)

	# recreate gmo-files as workaround
	(cd po; $(MAKE) update-gmo)
#	sed -e '/fetchmail-5.3.3/ { s/.*/=== file truncated, see source package for complete changelog ===/; q; }' < NEWS > NEWS.truncated
	
	touch build-stamp

tmpdir = $(CURDIR)/debian/tmp
pckdir = $(CURDIR)/debian/fetchmail

install: build-stamp
	dh_testdir
	dh_testroot
	dh_prep -X.orig -X.rej
	dh_installdirs
	$(MAKE) install prefix=$(tmpdir)/usr mandir=$(tmpdir)/usr/share/man
	-install -D -m 644 debian/fetchmailconf.xpm \
		$(tmpdir)/usr/share/pixmaps/fetchmailconf.xpm
	gzip -9 $(tmpdir)/usr/share/man/man1/fetchmail.1
#	not needed we set a symlink
#	gzip -9 $(tmpdir)/usr/share/man/man1/fetchmailconf.1
#	mkdir -p debian/fetchmailconf/usr/lib/python$(PYVER)/site-packages/
#	mv debian/tmp/usr/lib/python$(PYVER)/site-packages/fetchmailconf.py \
#	debian/fetchmailconf/usr/lib/python$(PYVER)/site-packages/

# Build architecture-independent files here. [ fetchmailconf ]
binary-indep: build-stamp install
	dh_testdir
	dh_testroot
	dh_install -i --sourcedir=$(tmpdir)
	dh_installdocs -i
	dh_installmenu -i
	dh_installman -i 
	dh_installchangelogs -i
	dh_link -i usr/share/man/man1/fetchmail.1.gz usr/share/man/man1/fetchmailconf.1.gz
	dh_python2 -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.  [ fetchmail ]
binary-arch: build-stamp install
	dh_testdir
	dh_testroot
	mkdir -p $(pckdir)/usr/share/doc/fetchmail/contrib
	xargs -r -i install "{}" $(pckdir)/usr/share/doc/fetchmail/contrib \
		< debian/contrib.files
	-install -m 644 debian/README.contrib \
		$(pckdir)/usr/share/doc/fetchmail/contrib/README.Debian
	install -D -m 755 debian/ip-up $(pckdir)/etc/ppp/ip-up.d/fetchmail
	install -D -m 755 debian/ip-down $(pckdir)/etc/ppp/ip-down.d/fetchmail
	install -D -m 755 debian/resolvconf \
		$(pckdir)/etc/resolvconf/update-libc.d/fetchmail
	install -D -m 644 debian/workstation \
		$(pckdir)/etc/logcheck/ignore.d.workstation/fetchmail
	install -D -m 644 debian/server \
		$(pckdir)/etc/logcheck/ignore.d.server/fetchmail
	dh_install -a --sourcedir=$(tmpdir)
	dh_installdocs -a NOTES README fetchmail-features.html \
		design-notes.html todo.html fetchmail-FAQ.html README.SSL OLDNEWS
	dh_installexamples -a
	dh_installinit -a -n
	dh_installchangelogs -a NEWS
	dh_installman 
	dh_strip -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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