#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

#
# $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)

# 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

tmpdir = $(CURDIR)/debian/tmp

override_dh_auto_configure:
	dh_auto_configure -- --enable-nls --disable-fallback $(FETCHCONFOPT)

override_dh_auto_install:
	dh_auto_install
	mkdir -p $(tmpdir)/usr/share/doc/fetchmail/contrib
	xargs -r -i install "{}" $(tmpdir)/usr/share/doc/fetchmail/contrib \
		< $(CURDIR)/debian/contrib.files
	-install -m 644 debian/README.contrib \
	install -D -m 755 debian/ip-up $(tmpdir)/etc/ppp/ip-up.d/fetchmail
	install -D -m 755 debian/ip-down $(tmpdir)/etc/ppp/ip-down.d/fetchmail
	install -D -m 755 debian/resolvconf \
		$(tmpdir)/etc/resolvconf/update-libc.d/fetchmail
	install -D -m 644 debian/workstation \
		$(tmpdir)/etc/logcheck/ignore.d.workstation/fetchmail
	install -D -m 644 debian/server \
		$(tmpdir)/etc/logcheck/ignore.d.server/fetchmail

override_dh_auto_test:

%:
	dh $@ --parallel --with autoreconf,python2

.PHONY: override_dh_auto_configure override_dh_auto_install \
	override_dh_auto_test
