#!/usr/bin/make -f

# NOTE: installed with tr/_/-/ in the source tree since _ is not a valid
# character in in source names
PLUGINS = authres_status \
	compose_addressbook \
	contextmenu \
	dovecot_impersonate \
	fail2ban \
	html5_notifier \
	keyboard_shortcuts \
	listcommands \
	message_highlight \
	sauserprefs \
	thunderbird_labels
component_name = $(subst _,-,$(1))
topdir = $(firstword $(subst /, ,$(1)))

# source path of the given destination plugin path (relative to usr/share/roundcube/plugins/)
sourcepath = $(patsubst $(call topdir,$(1))/%,$(call component_name,$(call topdir,$(1)))/%,$(1))

update:
	debian/scripts/plugins.py

%:
	dh $@

GENERATED_JSFILES = thunderbird-labels/tb_label.js
GENERATED_CSSFILES = contextmenu/skins/elastic/contextmenu.css \
	sauserprefs/skins/elastic/sauserprefs.css \
	sauserprefs/skins/elastic/tabstyles.css
SRC_FILES := $(filter-out $(GENERATED_JSFILES) $(GENERATED_CSSFILES),$(shell find -P -- $(call component_name,$(PLUGINS)) -type f))
CONF_DIST_NAMES = config.inc.php.dist config.inc.php-dist
CHANGELOG_NAMES = changelog Changelog CHANGELOG changelog.txt
DOC_NAMES = README readme.md README.md MANUAL.md docs

GENERATED_FILES := $(GENERATED_JSFILES) $(GENERATED_CSSFILES)
JS_MINFILES = $(patsubst %.js,%.min.js,$(filter-out %.min.js,$(filter %.js,$(SRC_FILES) $(GENERATED_FILES))))
CSS_MINFILES = $(patsubst %.css,%.min.css,$(filter-out %.min.css,$(filter %.css,$(SRC_FILES) $(GENERATED_FILES))))
GENERATED_FILES += $(JS_MINFILES) $(CSS_MINFILES)

$(JS_MINFILES): %.min.js: %.js
	uglifyjs --compress --mangle --source-map "base='$(dir $@)',url='$(notdir $@).map'" \
		-o $@ -- $<

$(CSS_MINFILES): %.min.css: %.css
	cd $(dir $@) && cleancss --source-map -o $(notdir $@) -- $(notdir $<)

# Generate thunderbird-labels/tb_label.js from its .coffee sources
thunderbird-labels/tb_label.js: $(wildcard thunderbird-labels/coffeescripts/*.coffee)
	cd thunderbird-labels && ./coffee2js.sh

# Generate CSS from LESS sources
# We run the command in the Roundcube tree where can import
# ../../../../skins/elastic/styles/{variables,mixins}.
$(GENERATED_CSSFILES): %.css: %.less
	cd /usr/share/roundcube/plugins/jqueryui/themes/elastic && \
		lessc --source-map --rewrite-urls $(CURDIR)/$< $(CURDIR)/$@

GENERATED_MAPFILES = $(addsuffix .map,$(filter %.css %.js,$(GENERATED_FILES)))
GENERATED_FILES += $(GENERATED_MAPFILES)
$(GENERATED_MAPFILES): %.map: | % ;

# Pre-compress minified files
COMPRESSED_FILES = $(JS_MINFILES) $(CSS_MINFILES)
COMPRESSED_FILES := $(addsuffix .gz,$(COMPRESSED_FILES))
GENERATED_FILES += $(COMPRESSED_FILES)
$(COMPRESSED_FILES): %.gz: %
	pigz -11 -mnk -- $<

PLUGINS_CONFIGURABLE := $(foreach plugin,$(PLUGINS), $(addprefix $(plugin)/, \
	$(shell find -P -- $(call component_name,$(plugin)) \( -name "config.inc.php" \
		$(foreach confdist,$(CONF_DIST_NAMES),-o -name "$(confdist)") \) -type f -printf "%P\\n")))
PLUGINS_CONF_DIST = $(filter $(addprefix %/,$(CONF_DIST_NAMES)),$(PLUGINS_CONFIGURABLE))

# For each plugin with a config.inc.php[.-]dist, we symlink a config.inc.php in
# /etc/roundcube/plugins with some dummy content.  (If config.inc.php already
# exists,such as for keyboard_shortcuts, then it used instead.)
# NOTE: We assume that the plugin loads its configuration from a
# 'config.inc.php' file under the same directory holding the default
# configuration file (normally config.inc.php.dist but message-highlight calls
# it config.inc.php-dist).  If that file is not directly under the plugin
# directory, such as for html5_notifier, then we assume the plugin loads its
# configuration with $this->load_config('path/to/config.inc.php').
define genconf_target
GENERATED_FILES += $$(addsuffix config.inc.php,$$(dir $(1)))
$$(addsuffix config.inc.php,$$(dir $(1))): $(1)
	$$(file >$$@,<?php)
	$$(file >>$$@,// Empty configuration for $$(call topdir,$(2)))
	$$(file >>$$@,// See /usr/share/roundcube/plugins/$(2) for instructions)
	$$(file >>$$@,?>)
endef
$(foreach confdist,$(PLUGINS_CONF_DIST), $(eval $(call genconf_target, \
	$(call sourcepath,$(confdist)),$(confdist))))

execute_after_dh_auto_clean:
	@rm -f -- $(GENERATED_FILES)

override_dh_auto_build: $(GENERATED_FILES) ;
.SECONDARY:

execute_after_dh_auto_test:
	debian/scripts/check-long-description

execute_after_dh_install:
	# install plugins
	$(foreach plugin,$(PLUGINS), \
		dh_install --exclude=LICENSE --exclude=INSTALL \
			$(foreach x,$(CHANGELOG_NAMES) $(DOC_NAMES),--exclude=$(x)) \
			$(call component_name,$(plugin))/* usr/share/roundcube/plugins/$(plugin) && \
		) true

	# install plugin configuration
	$(foreach conffile,$(addsuffix config.inc.php,$(dir $(PLUGINS_CONFIGURABLE))), \
		dh_install $(call sourcepath,$(conffile)) etc/roundcube/plugins/$(call topdir,$(conffile)) && \
		) true

plugin_docs = $(wildcard $(addprefix $(call component_name,$(1))/,$(CHANGELOG_NAMES) $(DOC_NAMES)))
execute_after_dh_installdocs:
	$(foreach plugin,$(PLUGINS), \
		$(if $(call plugin_docs,$(plugin)), \
			dh_install $(call plugin_docs,$(plugin)) usr/share/doc/roundcube-plugins-extra/$(plugin),) && \
		) true

execute_after_dh_link:
	# symlink plugin configuration (and delete existing usr/share/roundcube/plugins/*/config.inc.php
	# file which we installed as etc/roundcube/plugins/*/config.inc.php instead)
	$(foreach conffile,$(addsuffix config.inc.php,$(dir $(PLUGINS_CONFIGURABLE))), \
		dh_link etc/roundcube/plugins/$(call topdir,$(conffile))/config.inc.php \
			usr/share/roundcube/plugins/$(conffile) && \
		) true

	# symlink plugin directories to var/lib/roundcube/plugins
	$(foreach plugin,$(PLUGINS), \
		dh_link usr/share/roundcube/plugins/$(plugin) var/lib/roundcube/plugins/$(plugin) && \
		) true

execute_after_dh_fixperms:
	find debian/roundcube-plugins-extra/usr/share/roundcube/plugins \
		\! -name "*.sh" -type f -execdir chmod -x -- {} +
