#!/usr/bin/make -f
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
include /usr/share/rustc/architecture.mk

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export CARGO_PROFILE_RELEASE_DEBUG = true
export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
# dpkg-buildflags already maps CURDIR to "." in CFLAGS via -ffile-prefix-map
# (part of hardening=+all's fixfilepath); rustc has no dpkg-buildflags
# integration for this yet, so do the same mapping manually to avoid
# embedding the build path in the binary (lintian:
# file-references-package-build-path).
export RUSTFLAGS = --remap-path-prefix=$(CURDIR)=.
export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE
export CARGO_HOME = $(CURDIR)/debian/cargo_home
export PATH := /usr/share/cargo/bin:$(PATH)
# Where the binary looks for its translations outside flatpak.
export STREMIO_LOCALEDIR = /usr/share/locale

%:
	dh $@ --buildsystem cargo

override_dh_auto_build:
	cargo build --release --features offline-build

override_dh_auto_install:
	install -D -m755 target/$(DEB_HOST_RUST_TYPE)/release/stremio-linux-shell $(CURDIR)/debian/stremio/usr/libexec/stremio/stremio-bin
	install -D -m755 debian/stremio-wrapper $(CURDIR)/debian/stremio/usr/bin/stremio
	for po in po/*.po; do \
	  lang=$$(basename $$po .po); \
	  mkdir -p debian/stremio/usr/share/locale/$$lang/LC_MESSAGES; \
	  msgfmt -o debian/stremio/usr/share/locale/$$lang/LC_MESSAGES/stremio.mo $$po; \
	done
	install -d debian/stremio/usr/share/dbus-1/services
	sed 's|/app/bin/stremio|/usr/bin/stremio|' data/com.stremio.Stremio.service \
	  > debian/stremio/usr/share/dbus-1/services/com.stremio.Stremio.service
	# overriding dh_auto_install bypasses cargo buildsystem's own install
	# step, which is what normally calls this to populate Built-Using
	dh-cargo-built-using stremio

# The crate ships no tests at all: no tests/ directory, no #[test], no [[test]]
# target and no dev-dependencies. dh_auto_test would still run `cargo test`,
# which recompiles the whole binary in test mode for nothing, so skip it.
override_dh_auto_test:

override_dh_auto_clean:
	rm -rf target debian/cargo_registry debian/cargo_home .cargo-checksum.json Cargo.lock
