#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2008-2010, Jonas Smedegaard <dr@jones.dk>
# Copyright © 2010, Stefan Hornburg (Racke) <racke@linuxia.de>
# Description: Main Debian packaging script for SYMPA
#
# 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, 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, see <http://www.gnu.org/licenses/>.

include /usr/share/cdbs/1/rules/upstream-tarball.mk
include /usr/share/cdbs/1/rules/utils.mk
include /usr/share/cdbs/1/class/autotools.mk
include /usr/share/cdbs/1/rules/debhelper.mk

GENCAT=`which gencat || echo '/usr/bin/gencat'`
PERL=/usr/bin/perl

SCRIPTS = p12topem.pl testldap.pl load_subscribers.pl orphan_killer.pl \
	crypt_passwd.pl init_comment.pl arc2webarc.pl

DEB_UPSTREAM_URL = http://www.sympa.org/distribution/old
DEB_UPSTREAM_TARBALL_MD5 = 418fc8f851b4dcaae67785a7b12c4cf5

# TODO: drop before next upstream release
DEB_UPSTREAM_REPACKAGE_DELIMITER = +

# non-DFSG license limited to "authorized use only"
DEB_UPSTREAM_REPACKAGE_EXCLUDES += ./src/etc/ca-bundle.crt
# copyright-protected but inlicensed
DEB_UPSTREAM_REPACKAGE_EXCLUDES += ./po/it.po ./po/oc.po
# unused sources cluttering authorship/licensing tracking
DEB_UPSTREAM_REPACKAGE_EXCLUDES += ./po*.gmo

DEB_COPYRIGHT_CHECK_IGNORE_REGEX = ^\./po.*\.gmo|\./debian/(changelog|copyright(|_hints|_newhints))$

DEB_CONFIGURE_PREFIX = /usr/lib/sympa
DEB_CONFIGURE_EXTRA_FLAGS = \
	--enable-fhs \
	--sysconfdir=/etc/sympa \
	--with-cgidir=/usr/lib/cgi-bin/sympa \
	--datadir=/usr/share \
	--mandir=/usr/share/man \
	--with-initdir=/etc/init.d \
	--with-docdir=/usr/share/doc/sympa \
	--with-perl=$(PERL) \
	--with-openssl=/usr/bin/openssl \
	--with-gencat=$(GENCAT) \
	--with-user=root --with-group=root \
	--with-sendmail_aliases=/etc/mail/sympa.aliases \
	--with-virtual_aliases=/etc/mail/sympa.virtual

# We cannot configure --with-expldir: upstream use uppercase X in subdir
DEB_MAKE_INSTALL_TARGET = install \
	DESTDIR="$(DEB_DESTDIR)" \
	SSLCERTDIR=/var/lib/sympa/x509-user-certs

DEB_INSTALL_EXAMPLES_sympa = debian/examples/tulp2sympa

man8basenames = alias_manager archived bounced sympa
DEB_INSTALL_MANPAGES_sympa = $(man8basenames:%=doc/man8/%.8)

DEB_UPDATE_RCD_PARAMS = defaults 50
DEB_FIXPERMS_EXCLUDE = /usr/lib/sympa/bin/queue /usr/lib/sympa/bin/bouncequeue

# “maintainer-clean” target will also remove po{,-wwsympa}/{stamp-po,*.gmo} files
DEB_MAKE_CLEAN_TARGET = maintainer-clean

localenames = af ar bg br ca cs de el en_US es et eu fi fr hu id it ja ko
localenames +=  ml nb_NO nl nn_NO oc pl pt pt_BR ro ru sv tr vi zh_CN zh_TW
upstreamtmpfiles += $(localenames:%=po/%.gmo)
upstreamtmpfiles += $(localenames:%=po-wwsympa/%.gmo)
upstreamtmpfiles += src/etc/list_aliases.tt2 src/lib/Sympa/Constants.pm sympa.spec
pre-build:: debian/stamp-upstreamtmpstuff
debian/stamp-upstreamtmpstuff:
	for file in $(upstreamtmpfiles); do \
		[ ! -e $$file ] || [ -e $$file.upstream ] || mv $$file $$file.upstream; \
	done
	touch $@
clean::
	for file in $(upstreamtmpfiles); do \
		[ ! -e $$file.upstream ] || mv -f $$file.upstream $$file; \
	done
	rm -f debian/stamp-upstreamtmpstuff
	rm -f po/stamp-po po-wwsympa/stamp-po

clean::
	debconf-updatepo
	rm -f data_structure.version

# FIXME: test if SYMPA actually work with a symlink here
pre-build::
	touch src/etc/ca-bundle.crt
binary-post-install/sympa::
	rm -f $(DEB_DESTDIR)/usr/share/sympa/default/ca-bundle.crt
	ln -s -T /etc/ssl/certs/ca-certificates.crt $(DEB_DESTDIR)/usr/share/sympa/default/ca-bundle.crt
clean::
	rm -f src/etc/ca-bundle.crt

# Prepare SQL scripts for use with dbconfig
#  * Strip initial commands to create and connect to database
#  * Strip commands to drop stuff at install time
build/sympa::
	mkdir -p debian/dbconfig-common/install
	perl -n \
		-e '/^CREATE\s+TABLE\s+\w+\s+\($$/ and $$ok++;' \
		-e 'print if $$ok;' \
		src/etc/script/create_db.mysql \
		> debian/dbconfig-common/install/mysql
	perl -n \
		-e '/^CREATE\s+TABLE\s+\w+\s+\($$/ and $$ok++;' \
		-e '/^DROP\s+\w+\s+\w+;$$/ and undef $$ok;' \
		-e 'print if $$ok;' \
		src/etc/script/create_db.Pg \
		> debian/dbconfig-common/install/postgres
	install -m 0644 src/etc/script/create_db.SQLite \
		debian/dbconfig-common/install/sqlite
	ln -sf sqlite debian/dbconfig-common/install/sqlite3
# Safety check, as above makes assumptions on SQL coding style
	if egrep -i '(\bcreate[[:space:]]database|\bdrop\b)' debian/dbconfig-common/install/*; then \
		echo "ERROR: SQL install script possibly contains bad commands!"; \
		echo "       Please inspect debian/dbconfig-common/install/*"; \
		echo "       and adjust debian/rules."; \
		exit 1; \
	fi
clean::
# Be cautious: we might add other subdirs here by hand later
	rm -f debian/dbconfig-common/install/*
	[ ! -e debian/dbconfig-common ] || \
		rmdir --ignore-fail-on-non-empty debian/dbconfig-common/install debian/dbconfig-common

# Remove unnecessary files
binary-post-install/sympa::
	rm -f "$(DEB_DESTDIR)"/usr/share/doc/sympa/examples/sample/*.conf \
		"$(DEB_DESTDIR)"/usr/lib/cgi-bin/sympa/mime.types

# SOAP client script not necessary for operation (#496515)
	rm "$(DEB_DESTDIR)"/usr/share/sympa/bin/sympa_soap_client.pl

# Remove run directory as the init script deals with that
	rmdir "$(DEB_DESTDIR)"/var/run/sympa "$(DEB_DESTDIR)"/var/run

# Remove configuration files shipped with sympa
	rm "$(DEB_DESTDIR)"/etc/sympa/sympa.conf
	rm "$(DEB_DESTDIR)"/etc/sympa/wwsympa.conf

# Move samples which end up for some reason in the filesystem root
	mv "$(DEB_DESTDIR)"/sample/* "$(DEB_DESTDIR)"/usr/share/doc/${cdbs_curpkg}/examples/
	rmdir "$(DEB_DESTDIR)"/sample

# Rename example file topics.conf -> topics.us.conf
binary-post-install/sympa::
	mv "$(DEB_DESTDIR)"/usr/share/doc/$(cdbs_curpkg)/examples/config/topics.conf \
		"$(DEB_DESTDIR)"/usr/share/doc/$(cdbs_curpkg)/examples/config/topics.us.conf

# Needed both by upstream build (or test) process and at runtime
common-depends = perl-suid, libdbi-perl (>= 1.06), libarchive-zip-perl (>= 1.05), libmailtools-perl (>= 1.51), libmsgcat-perl (>= 1.0), libmime-tools-perl | libmime-perl (>= 5.209), perl (>= 5.8), libio-stringy-perl, libnet-ldap-perl (>= 0.27), libcgi-fast-perl, libcrypt-ciphersaber-perl, libintl-perl, libmime-charset-perl, libmime-encwords-perl, libtemplate-perl, libxml-libxml-perl, libcgi-pm-perl (>= 3.35), libhtml-stripscripts-parser-perl (>= 1.0), libhtml-tree-perl, libhtml-format-perl, mhonarc, libregexp-common-perl
CDBS_DEPENDS = $(common-depends)
CDBS_BUILD_DEPENDS += , $(common-depends)

# Needed by packaging routines
CDBS_BUILD_DEPENDS += , debconf-utils, po-debconf

# Needed at runtime
CDBS_DEPENDS += , sendmail (>= 8.9) | mail-transport-agent, sysklogd (>= 1.3-27) | system-log-daemon, lsb-base, adduser, debconf | debconf-2.0, dbconfig-common
CDBS_DEPENDS += , libfile-copy-recursive-perl, libnet-netmask-perl, libterm-progressbar-perl, libmime-lite-html-perl
CDBS_DEPENDS += , libdbd-mysql-perl, libdbd-pg-perl, libdbd-sqlite3-perl | libdbd-sqlite2-perl, sqlite3 | sqlite
