#!/usr/bin/make -f
#
# Copyright 2000-2012 by Stefan Hornburg (Racke) <racke@linuxia.de>
# Copyright 2015 Ondřej Surý <ondrej@debian.org>
# Copyright 2017-2021 Markus Wanner <markus@bluegap.ch>
# Copyright 2025 Soren Stoutner <soren@debian.org>
#
# 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 3 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., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA.

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

BUILD=$(CURDIR)/debian/tmp

# Get the version numbers of the subprojects from their configure.ac files.
sed_AC_INIT = 's/.*\[PACKAGE_NAME\],\[\([.0-9]*\)\].*/\1/p'
get_version = $(shell sed -ne $(subst PACKAGE_NAME,$(1),$(sed_AC_INIT)) $(2))
IMAPVER=$(call get_version,courier-imap,libs/imap/configure.ac)
SQWEBMAILVER=$(call get_version,sqwebmail,libs/sqwebmail/configure.ac)
MAILDROPVER=$(call get_version,maildrop,libs/maildrop/configure.ac)

# This is required to use ${courier-base:Version} in debian/control.
include /usr/share/dpkg/default.mk

# Enable all build hardening flags.
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Export the DPKG default build flags.
DPKG_EXPORT_BUILDFLAGS = 1

# Include the default build flags.  <https://wiki.debian.org/Hardening#dpkg-buildflags>
include /usr/share/dpkg/buildflags.mk

# Append the CPPFLAGS to the standard CFLAGS and CXXFLAGS variables, which is how CMake likes it.  <https://wiki.debian.org/Hardening#Notes_for_packages_using_CMake>
CFLAGS += $(CPPFLAGS)
CXXFLAGS += $(CPPFLAGS)

# see ENVIRONMENT in dpkg-buildflags(1)
# package maintainers to append LDFLAGS  TODO: this may no longer be needed.
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

%:
	dh $@ --with apache2

override_dh_autoreconf:
	find $(CURDIR) -name Makefile.in -delete
	dh_autoreconf --as-needed

# Common configuration options
COMMON_CONFIGURE = \
		MKDIR_P="mkdir -p" \
		--cache-file=$(CURDIR)/config.cache \
		--with-piddir=/run/courier \
		--sysconfdir=/etc/courier \
		--libexecdir=\$${prefix}/lib/courier \
		--datadir=\$${prefix}/lib/courier \
		--localstatedir=/var/lib/courier \
		--with-mailuser=courier \
		--with-mailgroup=courier \
		--with-mailuid=0 \
		--with-mailgid=0 \
		--without-socks \
		--with-gnutls \
		--enable-workarounds-for-imap-client-bugs \
		--with-authdaemonvar=/run/courier/authdaemon \
		--with-db=gdbm \
		--without-fcgi \
		--with-htmllibdir=/usr/share/sqwebmail \
		--with-ispell=/usr/bin/ispell \
		--enable-imageurl=/sqwebmail \
		--with-mailer="/usr/sbin/sendmail" \
		--enable-sendmail="/usr/sbin/sendmail" \
		--with-cachedir=/var/cache/sqwebmail \
		--with-calendardir=/run/courier/calendar \
		--with-certsdir=/etc/courier \
		--with-certsdb=/etc/ssl/certs/ca-certificates \
		--with-postgresql-includes=/usr/include/postgresql \
		--with-webadmindir=/usr/share/courier/webadmin \
		--with-notice=unicode \
		--enable-authlib \
		--enable-userdb \
		--enable-syslog=1 \
		--enable-unicode \
		--disable-root-check

override_dh_auto_configure:
	CONFIG_SHELL=/bin/sh dh_auto_configure -- $(COMMON_CONFIGURE)

override_dh_auto_install:
	INSTALL_IGNORE_UMASK=1 dh_auto_install -- install

override_dh_installdocs-arch:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	dh_installdocs -a
	install -m 644 debian/sqwebmail.html debian/sqwebmail/usr/share/doc/sqwebmail
	install -m 644 libs/gpglib/README.html debian/sqwebmail/usr/share/doc/sqwebmail/GPG.html
	install -m 644 libs/pcp/README.html debian/sqwebmail/usr/share/doc/sqwebmail/PCP.html
else
	rm -rf usr/lib/courier/htmldoc/*.html
endif

override_dh_installsystemd:
	dh_installsystemd -p courier-mta  --name=courier
	dh_installsystemd -p courier-mta  --name=courierfilter
	dh_installsystemd -p courier-mta  --name=courier-mta
	dh_installsystemd -p courier-mta  --name=courier-mta-ssl
	dh_installsystemd -p courier-mta  --name=courier-msa
	dh_installsystemd -p courier-imap --name=courier-imap
	dh_installsystemd -p courier-imap --name=courier-imap-ssl
	dh_installsystemd -p courier-pop  --name=courier-pop
	dh_installsystemd -p courier-pop  --name=courier-pop-ssl
	dh_installsystemd --remaining-packages

override_dh_installinit:
	dh_installinit -p courier-mta --name=courier
	dh_installinit -p courier-mta --name=courierfilter
	dh_installinit -p courier-mta
	dh_installinit -p courier-mta --name=courier-mta-ssl
	dh_installinit -p courier-mta --name=courier-msa
	dh_installinit -p courier-imap
	dh_installinit -p courier-imap --name=courier-imap-ssl
	dh_installinit -p courier-pop
	dh_installinit -p courier-pop --name=courier-pop-ssl
	dh_installinit --remaining-packages

POD2MAN := pod2man --center='Debian GNU/Linux Documentation' --release='Debian GNU/Linux '`cat /etc/debian_version`

override_dh_install:
	${MAKE} install-perms
	cd $(CURDIR)/courier && ./perms.sh

# Install init-d helper script
	install -m 644 debian/init-d-script-courier $(BUILD)/usr/lib/courier/

# Create additional manpages
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	# Generate the man1 pages.
	for man1 in addcr courier-config; do \
	    $(POD2MAN) --section=1 debian/$${man1}.pod > $(BUILD)/usr/share/man/man1/$${man1}.1; \
	done

	# Generate the man8 pages.
	for man8 in sharedindexinstall sharedindexsplit webgpg webmaild; do \
	    $(POD2MAN) --section=8 debian/$${man8}.pod > $(BUILD)/usr/share/man/man8/$${man8}.8; \
	done

	# Remove any undefined "an-trap" macros from the man pages.  <https://github.com/svarshavchik/courier/issues/58>
	sed -i '/an-trap/d' $(BUILD)/usr/share/man/man1/*
	sed -i '/an-trap/d' $(BUILD)/usr/share/man/man5/*
	sed -i '/an-trap/d' $(BUILD)/usr/share/man/man8/*
else
# Remove manpages for nodoc builds
	rm -f $(BUILD)/usr/share/man/man?/*.[1234578]
endif

# Ispell dictionary configuration will be produced by postinst
	rm -f $(BUILD)/usr/lib/courier/sqwebmail/html/en-us/ISPELLDICT

# Remove examples
	rm -f $(BUILD)/usr/lib/courier/perlfilter-example.pl
	rm -f $(BUILD)/etc/courier/quotawarnmsg.example

# Remove showmodules so we get rid of the libmysqlclient dependency
	rm -f $(BUILD)/usr/sbin/showmodules

# Remove *.dist configuration files
	for f in $(BUILD)/etc/courier/*.dist; do \
	  mv $${f} $${f%.dist}; \
	done

# Replace PAM files with our versions
	rm $(BUILD)/etc/courier/*.authpam
	install -d -m 755 $(BUILD)/etc/pam.d
	install -m 644 debian/common-session.pam $(BUILD)/etc/pam.d/esmtp
	install -m 644 debian/common-session.pam $(BUILD)/etc/pam.d/imap
	install -m 644 debian/common-session.pam $(BUILD)/etc/pam.d/pop3
	install -m 644 debian/common-password.pam $(BUILD)/etc/pam.d/webmail
	install -m 644 debian/common-password.pam $(BUILD)/etc/pam.d/calendar

# faxmail init is just a shell snippet
	chmod 644 $(BUILD)/usr/lib/courier/faxmail/init

# kill duplicate maildrop package
	rm -f \
	  $(BUILD)/etc/courier/maildrop \
	  $(BUILD)/usr/bin/mailbot \
	  $(BUILD)/usr/bin/maildrop \
	  $(BUILD)/usr/bin/makemime \
	  $(BUILD)/usr/bin/reformail \
	  $(BUILD)/usr/bin/reformime \
	  $(BUILD)/usr/share/man/man1/mailbot.1 \
	  $(BUILD)/usr/share/man/man1/maildrop.1 \
	  $(BUILD)/usr/share/man/man1/makemime.1 \
	  $(BUILD)/usr/share/man/man1/reformail.1 \
	  $(BUILD)/usr/share/man/man1/reformime.1 \
	  $(BUILD)/usr/share/man/man7/maildropex.7 \
	  $(BUILD)/usr/share/man/man7/maildropfilter.7 \
	  $(BUILD)/usr/share/man/man7/maildropgdbm.7

	rm -f \
	  $(BUILD)/usr/lib/courier/mkpop3dcert \
	  $(BUILD)/usr/lib/courier/mkimapdcert

# run dh_install then check for missing files
	dh_install
	dh_missing --fail-missing

override_dh_installchangelogs-indep:
	dh_installchangelogs -p courier-doc

override_dh_installchangelogs-arch:
	dh_installchangelogs -p courier-base ChangeLog
	dh_installchangelogs -p courier-imap libs/imap/ChangeLog
	dh_installchangelogs -p sqwebmail libs/sqwebmail/ChangeLog
	dh_installchangelogs -a --remaining-packages

override_dh_gencontrol-arch:
	echo "courier-base:Version=$(DEB_VERSION)" >> debian/sqwebmail.substvars
	dh_gencontrol -psqwebmail -- -v$(SQWEBMAILVER)+$(DEB_VERSION_UPSTREAM_REVISION)

	echo "courier-base:Version=$(DEB_VERSION)" >> debian/courier-imap.substvars
	dh_gencontrol -pcourier-imap -- -v$(IMAPVER)+$(DEB_VERSION_UPSTREAM_REVISION)

	dh_gencontrol -a --remaining-packages

override_dh_auto_clean:
	dh_auto_clean

# Update translation files.
	debconf-updatepo