#!/usr/bin/make -f

export CARGO_HOME = $(CURDIR)/debian/cargo_home

%:
	dh $@

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 -p rustconn-cli

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
