#!/usr/bin/make -f

include /usr/share/cdbs/1/class/perl-makemaker.mk
include /usr/share/cdbs/1/rules/debhelper.mk

pkg = ciderwebmail

# Needed by upstream build and (always/often) at runtime
perl-deps = moose log-dispatch parse-recdescent
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-xs
perl-deps += 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 html-defang
perl-deps += moosex-role-withoverloading catalyst
catalyst-deps = authentication-credential-http plugin-configloader
catalyst-deps += plugin-static-simple plugin-stacktrace
catalyst-deps += plugin-authentication plugin-session-store-fastmmap
catalyst-deps += plugin-session-state-cookie model-dbic-schema
catalyst-deps += action-renderview action-renderview
deps = $(patsubst %,$(comma) lib%-perl,$(perl-deps))
deps += $(patsubst %,$(comma) libcatalyst-%-perl,$(catalyst-deps))
deps +=, libmail-imapclient-perl (>= 3.41)
deps-recommend = libdbd-sqlite3-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_DEPENDS_$(pkg) = $(deps), $(depends)
CDBS_RECOMMENDS_$(pkg) = $(deps-recommend), $(recommends)
CDBS_SUGGESTS_$(pkg) = $(suggests)

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