#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2012-2014 Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for CiderWebmail
#
# 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, 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/perl-makemaker.mk
include /usr/share/cdbs/1/rules/debhelper.mk

pkg = ciderwebmail

DEB_UPSTREAM_PACKAGE = CiderWebmail
# TODO: Use this and drop hints below when source again released there
#DEB_UPSTREAM_URL = http://ciderwebmail.org/downloads
DEB_UPSTREAM_URL = https://github.com/$(DEB_UPSTREAM_PACKAGE)/$(DEB_UPSTREAM_PACKAGE)/archive
DEB_UPSTREAM_TARBALL_BASENAME = release-$(DEB_UPSTREAM_TARBALL_VERSION)

# Needed by upstream build and (always/often) at runtime
perl-deps = moose catalyst-authentication-credential-http log-dispatch
perl-deps += log-dispatch-config catalyst-plugin-log-dispatch yaml
perl-deps += yaml-libyaml json-xs catalyst-view-petal petal-utils
perl-deps += datetime datetime-format-mail mime-tools try-tiny try-tiny-smartcatch
perl-deps += email-simple email-sender email-valid email-address
perl-deps += regexp-common-email-address regexp-common text-iconv
perl-deps += text-autoformat io-socket-ssl net-managesieve clone html-tidy
perl-deps += uri locale-maketext-gettext list-moreutils data-ical
perl-deps += datetime-format-iso8601 crypt-util crypt-cbc crypt-rijndael
perl-deps += crypt-random-source mime-base64-urlsafe
deps = $(patsubst %,$(comma) lib%-perl,$(perl-deps))
deps +=, libmoosex-role-withoverloading-perl (>= 0.09)
deps +=, libcatalyst-perl (>= 5.80030)
deps +=, libcatalyst-plugin-configloader-perl | libcatalyst-modules-perl (<< 48~)
deps +=, libcatalyst-plugin-static-simple-perl | libcatalyst-modules-perl (<< 48~)
deps +=, libcatalyst-perl (>= 5.90042-2~) | libcatalyst-plugin-unicode-encoding-perl
deps +=, libcatalyst-plugin-stacktrace-perl | libcatalyst-modules-perl (<< 48~)
deps +=, libcatalyst-plugin-authentication-perl | libcatalyst-modules-perl (<< 48~)
deps +=, libcatalyst-plugin-session-store-fastmmap-perl | libcatalyst-modules-perl (<< 48~)
deps +=, libcatalyst-plugin-session-state-cookie-perl | libcatalyst-modules-perl (<< 48~)
deps +=, libcatalyst-model-dbic-schema-perl (>= 0.41) | libcatalyst-modules-perl (<< 48~)
deps +=, libcatalyst-action-renderview-perl (>= 0.16) | libcatalyst-modules-perl (>= 43~)
deps +=, libcatalyst-action-renderview-perl (>= 0.16) | libcatalyst-modules-perl (<< 48~)
deps +=, libparse-recdescent-perl (>= 1.967009)
deps +=, libmail-imapclient-perl (>= 3.35)
deps +=, libhtml-defang-perl (>= 1.04)
deps-recommend = libdbd-sqlite3-perl

# Needed by upstream testsuite (undeclared in Makefile.PL)
deps-test = libtest-xpath-perl

# Needed by upstream build
#  * Drop libcatalyst-devel-perl when upstream does proper packaging
bdeps = libmodule-install-perl, libcatalyst-devel-perl

# Needed (always/often/seldom) at runtime
# * libauthen-sasl-perl needed for secure Managesieve connections
depends = adduser
recommends = libauthen-sasl-perl
recommends +=, default-mta | mail-transport-agent
suggests = libfcgi-perl, libfcgi-procmanager-perl
suggests +=, dovecot | imap-server, imapproxy

CDBS_BUILD_DEPENDS +=, $(deps), $(deps-recommend), $(bdeps), $(deps-test)
CDBS_DEPENDS_$(pkg) = $(deps), $(depends)
CDBS_RECOMMENDS_$(pkg) = $(deps-recommend), $(recommends)
CDBS_SUGGESTS_$(pkg) = $(suggests)

# copyright check
#  * extract metadata from images
#  * skip favicons and gettext MO files
CDBS_BUILD_DEPENDS +=, libimage-exiftool-perl, libregexp-assemble-perl
CDBS_BUILD_DEPENDS +=, libipc-system-simple-perl, lcdf-typetools
local_inspection_regex = png|jpg|jpeg|gif|ttf|otf
DEB_COPYRIGHT_CHECK_IGNORE_REGEX = ^((.*/)?[^/]+\.($(local_inspection_regex)|ico)|debian/(changelog|copyright(|_hints|_newhints)))$$
debian/stamp-copyright-check: debian/stamp-extract-copyright
debian/stamp-extract-copyright:
	find * -type f -regextype posix-extended \
		-regex '.*\.($(local_inspection_regex))' \
		-print0 | perl -0 debian/print-metadata.pl
	touch $@
pre-build:: clean-extracted-copyright-during-build
clean-extracted-copyright-during-build: debian/stamp-copyright-check
	find -type f -name '*.metadata_dump' -delete
clean::
	find -type f -name '*.metadata_dump' -delete
	rm -f debian/stamp-extract-copyright

DEB_INSTALL_EXAMPLES_$(pkg) = ciderwebmail.yml script/update_schema.sh

# Avoid virtually empty TODO file
DEB_INSTALL_DOCS_ALL := $(filter-out TODO,$(DEB_INSTALL_DOCS_ALL))

# (Re)generate localized templates
CDBS_BUILD_DEPENDS +=, gettext
langs := $(shell \
	find root/locale -mindepth 1 -maxdepth 1 -type d -printf '%f\n')
configure/$(pkg):: debian/stamp-mktemplates
debian/stamp-mktemplates: pre-build
	for lang in $(langs); do \
		stem=root/locale/$$lang/LC_MESSAGES/CiderWebmail; \
		msgfmt -o $$stem.mo $$stem.po; \
	done
	cd root/templates && perl ./template.pl
	touch $@
clean::
	find root/locale -type f -name '*.mo' -delete
	rm -f debian/stamp-mktemplates

# Install config files
binary-install/$(pkg)::
	mkdir -p $(cdbs_curdestdir)etc/$(pkg)
	touch $(cdbs_curdestdir)etc/$(pkg)/custom.css

# Install binaries in project-specific dir and drop their manpages
binary-post-install/$(pkg)::
	mkdir -p $(cdbs_curdestdir)usr/share/$(pkg)/bin
	mv $(cdbs_curdestdir)usr/bin $(cdbs_curdestdir)usr/share/$(pkg)/
	rm -rf $(cdbs_curdestdir)usr/share/man/man1

# Replace config with symlinks
binary-post-install/$(pkg)::
	rm -rf $(cdbs_curdestdir)usr/share/$(pkg)/root/static/css/custom.css
	ln -sT /etc/ciderwebmail/custom.css $(cdbs_curdestdir)usr/share/$(pkg)/root/static/css/custom.css

# Drop excess files bogusly handled by upstream install routines
binary-post-install/$(pkg)::
	rm -rf $(cdbs_curdestdir)usr/share/perl5/CiderWebmail/.pc
	rm -rf $(cdbs_curdestdir)usr/share/perl5/CiderWebmail/root
	rm -f $(cdbs_curdestdir)usr/share/perl5/CiderWebmail/.gitignore
	rm -f $(cdbs_curdestdir)usr/share/perl5/CiderWebmail/LICENSE
	rm -f $(cdbs_curdestdir)usr/share/perl5/CiderWebmail/TODO
	rm -f $(cdbs_curdestdir)usr/share/perl5/CiderWebmail/ciderwebmail.yml
	rm -f $(cdbs_curdestdir)usr/share/perl5/CiderWebmail/root/templates/template.pl
	rm -f $(cdbs_curdestdir)usr/share/ciderwebmail/root/templates/template.pl
