File debian.rules of Package guam

#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

DESTDIR   = $(CURDIR)/debian/guam
LIBDIR    = $(CURDIR)/debian/guam-libs
BUNDLEDIR = $(CURDIR)/debian/guam-bundled-libs

# The versions of all dependencies are hardcoded by relx (which honestly sucks).
# The following magic tries to ensure that the Debian package's dependencies are
# pinned to the versions present on the build machine.
# Usually, we pull in the package "guam-libs", which declares all the regular
# dependencies and creates an appropriate "lib" symlink.
# However, if the dependencies cannot be satisfied on the end user's machine -
# for instance because a  different Erlang version was installed from backports
# - we make the package manager pull in an extra package "guam-bundled-libs"
# that contains all the dependencies in the exact versions expected by relx.
#
# For maintainers, the bottom line is this: please revision-bump this package
# every time one of its dependencies gets version-bumped.
currver = $(shell dpkg-query -f '$${Version}' -W $(1) | sed 's/[+-].*$$//')
nextver = $(shell echo $(call currver,$(1)) | perl -pe 's/((\d+\.)*)(\d+)$$/$$1.($$3+1)/e')
pinvermin = $(1) (>= $(call currver,$(1)))
pinvermax = $(1) (<< $(call nextver,$(1)))
pinver    = $(call pinvermin,$(1)), $(call pinvermax,$(1))
pinvererlmin = $(1) (>= $(call currver,erlang))
pinvererlmax = $(1) (<< $(call nextver,erlang))

# We ship these packages ourselves, so they don't need bundling
KOLAB_PROVIDED_DEPS := $(call pinver,erlang-eimap), $(call pinver,erlang-lager-syslog)
EIMAP_DIR        := eimap-$(call currver,erlang-eimap)
LAGER_SYSLOG_DIR := lager_syslog-$(call currver,erlang-lager-syslog)

SUBSTVARS += -Vdist:GuamDepends="guam-libs (= $(DEB_VERSION)) | guam-bundled-libs (= $(DEB_VERSION))"
SUBSTVARS += -Vdist:GuamLibDepends="$(call pinvererlmin,erlang-base) | $(call pinvererlmin,erlang-base-hipe), $(call pinvererlmax,erlang-base) | $(call pinvererlmax,erlang-base-hipe), $(KOLAB_PROVIDED_DEPS), $(call pinver,erlang-asn1), $(call pinver,erlang-goldrush), $(call pinver,erlang-inets), $(call pinver,erlang-lager), $(call pinver,erlang-public-key)"
SUBSTVARS += -Vdist:GuamBundleDepends="$(KOLAB_PROVIDED_DEPS)"

%:
	dh $@ --with=systemd

override_dh_auto_build:
	sed -i -e 's/include_erts,\s*false/include_erts, true/' rebar.config
	export DEBUG=1
	export HEX_OFFLINE=1
	./rebar3 release \
		--dev-mode false \
		--relname guam \
		--relvsn $(DEB_VERSION_UPSTREAM) \
		--verbose


override_dh_auto_clean:
	./rebar3 clean || :

override_dh_auto_test:
	./rebar3 eunit -v || :

override_dh_auto_install:
	mkdir -p $(DESTDIR)/usr/sbin $(DESTDIR)/etc/guam \
		$(DESTDIR)/lib/systemd/system $(DESTDIR)/var/log/ \
		$(DESTDIR)/usr/lib/erlang/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)/ \
		$(DESTDIR)/usr/lib/erlang/bin $(DESTDIR)/var/log/guam
	chmod o= $(DESTDIR)/var/log/guam

	install -p -m 644 contrib/guam.service \
	        $(DESTDIR)/lib/systemd/system/guam.service

	cp -a _build/default/rel/guam/bin \
		$(DESTDIR)/usr/lib/erlang/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)/

	cp -a _build/default/rel/guam/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)/ebin \
		$(DESTDIR)/usr/lib/erlang/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)/

	rm _build/default/rel/guam/releases/$(DEB_VERSION_UPSTREAM)/sys.config
	rm _build/default/rel/guam/releases/$(DEB_VERSION_UPSTREAM)/vm.args

	cp -a _build/default/rel/guam/releases \
		$(DESTDIR)/usr/lib/erlang/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)/

	if [ -f "/etc/plesk-release" ]; then \
		cp ../SOURCES/plesk.sys.config \
			$(DESTDIR)/etc/guam/sys.config ; \
	else \
		sed -i -e 's/pki\/tls\/private\/localhost\.pem/ssl\/certs\/ssl-cert-snakeoil.pem/' \
			-e 's/pki\/tls\/private\/localhost\.ca\.pem/ssl\/certs\/ssl-cert-snakeoil.pem/' \
			-e 's/pki\/tls\/private\/localhost\.key/ssl\/private\/ssl-cert-snakeoil.key/' \
			_build/default/rel/guam/etc/sys.config ; \
		cp _build/default/rel/guam/etc/sys.config \
			$(DESTDIR)/etc/guam/sys.config ; \
	fi

	ln -s ../../../../../../../etc/guam/sys.config \
		$(DESTDIR)/usr/lib/erlang/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)/releases/$(DEB_VERSION_UPSTREAM)/sys.config

	cp _build/default/rel/guam/vm.args $(DESTDIR)/etc/guam/vm.args

	ln -s ../../../../../../../etc/guam/vm.args \
		$(DESTDIR)/usr/lib/erlang/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)/releases/$(DEB_VERSION_UPSTREAM)/vm.args

	ln -s ../../../../../var/log/guam $(DESTDIR)/usr/lib/erlang/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)/log

	echo '#!/bin/bash' > $(DESTDIR)/usr/sbin/guam
	echo 'exec /usr/lib/erlang/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)/bin/guam $$*' \
	     >> $(DESTDIR)/usr/sbin/guam
	chmod +x $(DESTDIR)/usr/sbin/guam

	ln -s ../lib/kolab_guam-$(DEB_VERSION_UPSTREAM)/bin/guam \
		$(DESTDIR)/usr/lib/erlang/bin/kolab_guam

	# Symlink for system-installed libraries
	mkdir -p $(LIBDIR)/usr/lib/erlang/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)
	ln -s ../../lib $(LIBDIR)/usr/lib/erlang/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)/lib

	# Bundled runtime system and libraries
	mkdir -p $(BUNDLEDIR)/usr/lib/erlang/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)
	cp -a _build/default/rel/guam/erts-* \
		$(BUNDLEDIR)/usr/lib/erlang/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)/
	cp -a _build/default/rel/guam/lib \
		$(BUNDLEDIR)/usr/lib/erlang/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)/
	rm -r $(BUNDLEDIR)/usr/lib/erlang/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)
	ln -s .. $(BUNDLEDIR)/usr/lib/erlang/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)
	for dir in $(EIMAP_DIR) $(LAGER_SYSLOG_DIR); do \
		rm -r $(BUNDLEDIR)/usr/lib/erlang/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)/lib/$$dir; \
		ln -s ../../$$dir $(BUNDLEDIR)/usr/lib/erlang/lib/kolab_guam-$(DEB_VERSION_UPSTREAM)/lib/$$dir; \
	done

override_dh_gencontrol:
	dh_gencontrol -- $(SUBSTVARS)