#!/usr/bin/make -f

export DH_VERBOSE = 1
# Respect container/caller build concurrency, particularly on small ARM boards.
export CARGO_BUILD_JOBS ?= 2
# Absent outside Debian; the install step is then staged as a native build.
-include /usr/share/dpkg/architecture.mk
ifneq ($(filter nogui,$(DEB_BUILD_PROFILES)),)
CARGO_PACKAGES = -p mps-worker -p mps-helper
else
CARGO_PACKAGES = --workspace
endif

# Cross build (dpkg-buildpackage -a<arch>): target the host triplet, link with
# the GNU cross compiler and resolve libraries through the host architecture's
# multiarch pkg-config directory. Tests cannot run on the build machine then.
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
# dpkg older than 1.22 has no DEB_HOST_RUST_TYPE; Debian armhf is ARMv7.
DEB_HOST_RUST_TYPE ?= $(patsubst arm-%,armv7-%,$(DEB_HOST_GNU_CPU)-unknown-$(DEB_HOST_GNU_SYSTEM))
CARGO_TARGET = --target $(DEB_HOST_RUST_TYPE) --config 'target.$(DEB_HOST_RUST_TYPE).linker="$(DEB_HOST_GNU_TYPE)-gcc"'
BUILD_DIR = target/$(DEB_HOST_RUST_TYPE)/release
export PKG_CONFIG_ALLOW_CROSS = 1
export PKG_CONFIG_LIBDIR = /usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig
else
CARGO_TARGET =
BUILD_DIR = target/release
endif

%:
	dh $@

override_dh_auto_build:
	cargo build --locked --release $(CARGO_TARGET) $(CARGO_PACKAGES)

override_dh_auto_install:
	install -d debian/magic-packet-server/usr/libexec/magic-packet-server
	install -m 755 $(BUILD_DIR)/magic-packet-worker $(BUILD_DIR)/magic-packet-helper debian/magic-packet-server/usr/libexec/magic-packet-server/
	install -m 644 packaging/systemd/magic-packet-server.service debian/magic-packet-server/usr/libexec/magic-packet-server/
	# Hand the unit to dh_installsystemd instead of installing it by path:
	# it picks the right systemd directory for its debhelper version and
	# generates the enable/start/stop snippets in the maintainer scripts.
	cp packaging/systemd/magic-packet-server.service debian/magic-packet-server.service
	install -D -m 644 packaging/config.toml debian/magic-packet-server/etc/magic-packet-server/config.toml
ifeq ($(filter nogui,$(DEB_BUILD_PROFILES)),)
	install -d debian/magic-packet-server-gui/usr/libexec/magic-packet-server
	install -m 755 $(BUILD_DIR)/magic-packet-tray debian/magic-packet-server-gui/usr/libexec/magic-packet-server/
	install -D -m 644 packaging/magic-packet-tray.desktop debian/magic-packet-server-gui/usr/share/applications/magic-packet-tray.desktop
	install -D -m 644 packaging/polkit/ru.somenkov.magicpacket.server.policy debian/magic-packet-server-gui/usr/share/polkit-1/actions/ru.somenkov.magicpacket.server.policy
	for size in 16 32 48 64 128 256; do \
		install -d debian/magic-packet-server-gui/usr/share/icons/hicolor/$${size}x$${size}/apps; \
		install -m 644 packaging/icons/hicolor/$${size}x$${size}/apps/*.png debian/magic-packet-server-gui/usr/share/icons/hicolor/$${size}x$${size}/apps/; \
	done
	for lang in en ru de es fr it ja ko pt_BR zh_CN; do \
		install -d debian/magic-packet-server-gui/usr/share/locale/$$lang/LC_MESSAGES; \
		msgfmt -o debian/magic-packet-server-gui/usr/share/locale/$$lang/LC_MESSAGES/magic-packet-tray.mo crates/mps-tray/po/$$lang.po; \
	done
endif

override_dh_auto_test:
ifeq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
	cargo test --locked $(CARGO_PACKAGES)
endif

override_dh_auto_clean:
	# Keep the Cargo cache between package builds.
	rm -f debian/magic-packet-server.service
