#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2012, 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 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/perl-makemaker.mk
include /usr/share/cdbs/1/rules/debhelper.mk

pkg = ciderwebmail

# suppress optional build-dependencies
CDBS_BUILD_DEPENDS_rules_upstream-tarball =
CDBS_BUILD_DEPENDS_rules_utils_copyright-check =

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
# TODO: when tagged upstream change to v$(DEB_UPSTREAM_TARBALL_VERSION)
DEB_UPSTREAM_TARBALL_BASENAME = master
DEB_UPSTREAM_TARBALL_MD5 = f0767bda767dee2f170eec2a7d23e416

# Needed by upstream build and (always) at runtime
#  * recent libcatalyst-modules-perl needed for versioned dependency on
#    Catalyst::Action::RenderView
perl-deps = parent moose catalyst-authentication-credential-http yaml
perl-deps += yaml-libyaml catalyst-view-petal petal-utils
perl-deps += datetime datetime-format-mail mime-tools try-tiny
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 dbd-sqlite3
deps = $(patsubst %,$(comma) lib%-perl,$(perl-deps))
deps += , libmoosex-role-withoverloading-perl (>= 0.09)
deps += , libcatalyst-perl (>= 5.80030), libcatalyst-modules-perl (>= 43)
deps += , libparse-recdescent-perl (>= 1.967009)
deps += , libmail-imapclient-perl (>= 3.31)
deps += , libhtml-defang-perl (>= 1.04)
CDBS_BUILD_DEPENDS += , $(deps), $(deps-test)
CDBS_DEPENDS_$(pkg) = $(deps)

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

# Needed for upstream build
CDBS_BUILD_DEPENDS += , libmodule-install-perl

# Needed for our l10n regeneration
CDBS_BUILD_DEPENDS += , gettext

# Needed (always/often/seldom) at runtime
# * libauthen-sasl-perl needed for secure Managesieve connections
CDBS_DEPENDS_$(pkg) +=, adduser
CDBS_RECOMMENDS_$(pkg) = libjs-mootools, libauthen-sasl-perl
CDBS_RECOMMENDS_$(pkg) += , default-mta | mail-transport-agent
CDBS_SUGGESTS_$(pkg) = libfcgi-perl, libfcgi-procmanager-perl
CDBS_SUGGESTS_$(pkg) += , dovecot | imap-server, imapproxy

# silence graphics files (in addition to defaults)
DEB_COPYRIGHT_CHECK_IGNORE_REGEX = ^(root/static/(images/.*|favicon\.ico)|debian/(changelog|copyright(|_hints|_newhints)))$

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

# put aside upstream-shipped temp files during build
# TODO: just use DEB_UPSTREAM_CRUFT_MOVE when cdbs 0.4.106 is in stable
upstreamtmpfiles = $(foreach lang,de en,root/templates/$(lang) root/locale/$(lang)/LC_MESSAGES/CiderWebmail.mo)
pre-build:: debian/stamp-upstream-cruft
debian/stamp-upstream-cruft: debian/stamp-copyright-check
	mkdir -p debian/upstream-cruft
	@for orig in $(upstreamtmpfiles); do \
		backup="debian/upstream-cruft/$$orig"; \
		[ ! -e "$$orig" ] || [ -e "$$backup" ] || { \
			mkdir -p "$$(dirname "$$backup")"; \
			echo mv "$$orig" "$$backup"; \
			mv "$$orig" "$$backup"; \
		}; \
	done
	touch $@
cleanbuilddir::
	@for orig in $(upstreamtmpfiles); do \
		backup="debian/upstream-cruft/$$orig"; \
		if [ -e "$$backup" ]; then \
			if [ -e "$$orig" ]; then \
				echo "rm -rf" "$$orig"; \
				rm -rf "$$orig"; \
			fi; \
			echo mv "$$backup" "$$orig"; \
			mv "$$backup" "$$orig"; \
		fi; \
	done
	rm -rf debian/upstream-cruft
clean::
	rm -f debian/stamp-upstream-cruft

# (Re)generate localized templates
configure/$(pkg):: debian/stamp-mktemplates
debian/stamp-mktemplates: pre-build
	for lang in da de en; do \
		stem=root/locale/$$lang/LC_MESSAGES/CiderWebmail; \
		msgfmt -o $$stem.mo $$stem.po; \
	done
	cd root/templates && perl ./template.pl
	touch $@
clean::
	rm -f root/locale/da/LC_MESSAGES/CiderWebmail.mo root/templates/da
	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 and separately shipped parts with symlinks
binary-post-install/$(pkg)::
	rm -f $(cdbs_curdestdir)usr/share/$(pkg)/root/static/scripts/mootools-?ore.js
	ln -sT ../../../../javascript/mootools/mootools-core.min.js \
		$(cdbs_curdestdir)usr/share/$(pkg)/root/static/scripts/mootools-core.js
	ln -sT ../../../../javascript/mootools/mootools-more.min.js \
		$(cdbs_curdestdir)usr/share/$(pkg)/root/static/scripts/mootools-more.js
	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
