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

# Needed by upstream build and (always/often) at runtime
perl-deps = 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 clone html-scrubber 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-plugin-unicode-perl | libcatalyst-modules-perl (<< 48~)
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.31)
deps-recommend = libdbd-sqlite3-perl

# Needed by upstream build
bdeps = libmodule-install-perl

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

# Needed (always/often/seldom) at runtime
depends = adduser
recommends = http-icons, libjs-mootools
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)

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

# 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-core.js
	ln -sT ../../../../javascript/mootools/mootools-core.min.js \
		$(cdbs_curdestdir)usr/share/$(pkg)/root/static/scripts/mootools-core.js
	rm -rf $(cdbs_curdestdir)usr/share/$(pkg)/root/static/images/mimeicons
	ln -sT ../../../../images/http-icons \
		$(cdbs_curdestdir)usr/share/$(pkg)/root/static/images/mimeicons
	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 unneeded noise
binary-post-install/$(pkg)::
	find $(cdbs_curdestdir)usr/share/$(pkg) -name '*.mo' -delete
