#!/usr/bin/make -f

export CARGO_HOME = $(CURDIR)/debian/cargo_home

%:
	dh $@

# NOT the file that builds any released package. Checked in 0.21.5: no workflow
# and no script invokes this — the release `.deb` is assembled by hand with
# `dpkg-deb --build` in .github/workflows/release.yml, and OBS uses
# packaging/obs/debian.rules. `vendor.tar.zst`, which the build below unpacks, is
# not even in the repository, so this cannot run from a clean checkout.
#
# It is kept for a local `dpkg-buildpackage` against an OBS-style source tree, and
# deliberately left without the pkg-config feature detection the live paths grew in
# 0.21.5: adding it here would be untestable code in a dormant file. If this is ever
# revived, copy the `override_dh_auto_build` recipe from packaging/obs/debian.rules
# wholesale — including its warning about comments inside the continuation chain,
# which silently emptied the feature variables there for several releases.
override_dh_auto_build:
	tar --zstd -xf vendor.tar.zst
	mkdir -p .cargo
	cp cargo_config .cargo/config.toml
	cargo build --release --offline -p rustconn
	cargo build --release --offline -p rustconn-cli --features full

override_dh_auto_install:
	# Binaries
	install -Dm755 target/release/rustconn debian/rustconn/usr/bin/rustconn
	install -Dm755 target/release/rustconn-cli debian/rustconn/usr/bin/rustconn-cli
	# Desktop file
	install -Dm644 rustconn/assets/io.github.totoshko88.RustConn.desktop \
		debian/rustconn/usr/share/applications/io.github.totoshko88.RustConn.desktop
	# MIME type for .rdp files
	install -Dm644 rustconn/assets/io.github.totoshko88.RustConn-rdp.xml \
		debian/rustconn/usr/share/mime/packages/io.github.totoshko88.RustConn-rdp.xml
	# MIME type for .vv files (virt-viewer)
	install -Dm644 rustconn/assets/io.github.totoshko88.RustConn-vv.xml \
		debian/rustconn/usr/share/mime/packages/io.github.totoshko88.RustConn-vv.xml
	# Icons - hicolor theme (for desktop/launcher)
	install -Dm644 rustconn/assets/icons/hicolor/scalable/apps/io.github.totoshko88.RustConn.svg \
		debian/rustconn/usr/share/icons/hicolor/scalable/apps/io.github.totoshko88.RustConn.svg
	install -Dm644 rustconn/assets/icons/hicolor/128x128/apps/io.github.totoshko88.RustConn.png \
		debian/rustconn/usr/share/icons/hicolor/128x128/apps/io.github.totoshko88.RustConn.png
	install -Dm644 rustconn/assets/icons/hicolor/256x256/apps/io.github.totoshko88.RustConn.png \
		debian/rustconn/usr/share/icons/hicolor/256x256/apps/io.github.totoshko88.RustConn.png
	# AppStream cached icons (for GNOME Software)
	install -d debian/rustconn/usr/share/app-info/icons/hicolor/128x128/apps
	install -Dm644 rustconn/assets/icons/hicolor/128x128/apps/io.github.totoshko88.RustConn.png \
		debian/rustconn/usr/share/app-info/icons/hicolor/128x128/apps/io.github.totoshko88.RustConn.png
	# AppStream metainfo
	install -Dm644 rustconn/assets/io.github.totoshko88.RustConn.metainfo.xml \
		debian/rustconn/usr/share/metainfo/io.github.totoshko88.RustConn.metainfo.xml
	# Screenshots in app-info for GNOME Software
	install -d debian/rustconn/usr/share/app-info/screenshots/io.github.totoshko88.RustConn
	for screenshot in rustconn/assets/screenshots/*.png; do \
		[ -f "$$screenshot" ] || continue; \
		install -Dm644 "$$screenshot" \
			"debian/rustconn/usr/share/app-info/screenshots/io.github.totoshko88.RustConn/$$(basename $$screenshot)"; \
	done
	# Locale files (compile .po to .mo)
	for po_file in po/*.po; do \
		[ -f "$$po_file" ] || continue; \
		lang=$$(basename "$$po_file" .po); \
		mkdir -p debian/rustconn/usr/share/locale/$$lang/LC_MESSAGES; \
		msgfmt -o debian/rustconn/usr/share/locale/$$lang/LC_MESSAGES/rustconn.mo "$$po_file"; \
	done
	# Cleanup
	rm -rf vendor .cargo

override_dh_auto_test:
	true

override_dh_auto_clean:
	cargo clean || true
	rm -rf $(CARGO_HOME) vendor .cargo
