#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

include /usr/share/dpkg/default.mk

UPSTREAM_VERSION := $(shell dpkg-parsechangelog -S Version | sed -e 's/-[^-]*$$//')

CARGO_HOME := $(CURDIR)/debian/cargo-home

export RELEASE_CHANNEL := stable
export RELEASE_VERSION := $(UPSTREAM_VERSION)
export ZED_UPDATE_EXPLANATION := Updates are handled by the Parrot package manager.
export CARGO_HOME := $(CARGO_HOME)

%:
	dh $@

override_dh_auto_clean:
	dh_clean
	rm -rf $(CURDIR)/target $(CURDIR)/debian/cargo-home
	rm -f $(CURDIR)/rust-toolchain.toml

override_dh_auto_configure:
	# nothing to configure, cargo handles it

override_dh_auto_build:
	# use the distro rustc, not the rustup-managed one requested upstream
	rm -f $(CURDIR)/rust-toolchain.toml
	cargo build --locked --release --package zed --package cli

override_dh_auto_test:

override_dh_dwz:

override_dh_auto_install:
	install -Dm755 $(CURDIR)/target/release/cli \
		$(CURDIR)/debian/zed/usr/bin/zed
	install -Dm755 $(CURDIR)/target/release/zed \
		$(CURDIR)/debian/zed/usr/libexec/zed-editor
	mkdir -p $(CURDIR)/debian/zed/usr/share/applications
	APP_ICON=zed envsubst '$$APP_ID $$APP_NAME $$DO_STARTUP_NOTIFY $$APP_CLI $$APP_ICON $$APP_ARGS' \
		< $(CURDIR)/crates/zed/resources/zed.desktop.in \
		> $(CURDIR)/debian/zed/usr/share/applications/dev.zed.Zed.desktop
	install -Dm644 $(CURDIR)/crates/zed/resources/app-icon.png \
		$(CURDIR)/debian/zed/usr/share/icons/hicolor/512x512/apps/zed.png
	install -Dm644 "$(CURDIR)/crates/zed/resources/app-icon@2x.png" \
		$(CURDIR)/debian/zed/usr/share/icons/hicolor/1024x1024/apps/zed.png
