#!/usr/bin/make -f

export DEB_CARGO_PACKAGE=cosmic-app-library
export CARGO_HOME=$(CURDIR)/debian/cargo-home

%:
	dh $@

override_dh_auto_configure:
	mkdir -p .cargo
	# Full vendor configuration including all Git overrides for offline build
	( \
		echo '[source.crates-io]'; \
		echo 'replace-with = "vendored-sources"'; \
		echo '[source.vendored-sources]'; \
		echo 'directory = "vendor"'; \
		echo ''; \
		echo '[source."git+https://github.com/jackpot51/rust-atomicwrites"]'; \
		echo 'git = "https://github.com/jackpot51/rust-atomicwrites"'; \
		echo 'replace-with = "vendored-sources"'; \
		echo '[source."git+https://github.com/pop-os/cosmic-applets"]'; \
		echo 'git = "https://github.com/pop-os/cosmic-applets"'; \
		echo 'replace-with = "vendored-sources"'; \
		echo '[source."git+https://github.com/pop-os/cosmic-protocols?rev=d0e95be"]'; \
		echo 'git = "https://github.com/pop-os/cosmic-protocols"'; \
		echo 'rev = "d0e95be"'; \
		echo 'replace-with = "vendored-sources"'; \
		echo '[source."git+https://github.com/pop-os/cosmic-settings-daemon"]'; \
		echo 'git = "https://github.com/pop-os/cosmic-settings-daemon"'; \
		echo 'replace-with = "vendored-sources"'; \
		echo '[source."git+https://github.com/pop-os/cosmic-text.git"]'; \
		echo 'git = "https://github.com/pop-os/cosmic-text.git"'; \
		echo 'replace-with = "vendored-sources"'; \
		echo '[source."git+https://github.com/pop-os/dbus-settings-bindings"]'; \
		echo 'git = "https://github.com/pop-os/dbus-settings-bindings"'; \
		echo 'replace-with = "vendored-sources"'; \
		echo '[source."git+https://github.com/pop-os/freedesktop-icons"]'; \
		echo 'git = "https://github.com/pop-os/freedesktop-icons"'; \
		echo 'replace-with = "vendored-sources"'; \
		echo '[source."git+https://github.com/pop-os/glyphon.git?tag=iced-0.14-dev"]'; \
		echo 'git = "https://github.com/pop-os/glyphon.git"'; \
		echo 'tag = "iced-0.14-dev"'; \
		echo 'replace-with = "vendored-sources"'; \
		echo '[source."git+https://github.com/pop-os/libcosmic/"]'; \
		echo 'git = "https://github.com/pop-os/libcosmic/"'; \
		echo 'replace-with = "vendored-sources"'; \
		echo '[source."git+https://github.com/pop-os/smithay-clipboard?tag=pop-dnd-5"]'; \
		echo 'git = "https://github.com/pop-os/smithay-clipboard"'; \
		echo 'tag = "pop-dnd-5"'; \
		echo 'replace-with = "vendored-sources"'; \
		echo '[source."git+https://github.com/pop-os/softbuffer?tag=cosmic-4.0#a3f77e25"]'; \
		echo 'git = "https://github.com/pop-os/softbuffer"'; \
		echo 'tag = "cosmic-4.0"'; \
		echo 'replace-with = "vendored-sources"'; \
		echo '[source."git+https://github.com/pop-os/window_clipboard.git?tag=pop-0.13-2"]'; \
		echo 'git = "https://github.com/pop-os/window_clipboard.git"'; \
		echo 'tag = "pop-0.13-2"'; \
		echo 'replace-with = "vendored-sources"'; \
		echo '[source."git+https://github.com/pop-os/winit.git?tag=iced-xdg-surface-0.13-rc"]'; \
		echo 'git = "https://github.com/pop-os/winit.git"'; \
		echo 'tag = "iced-xdg-surface-0.13-rc"'; \
		echo 'replace-with = "vendored-sources"'; \
		echo '[source."git+https://github.com/wash2/accesskit?tag=iced-xdg-surface-0.13-rc"]'; \
		echo 'git = "https://github.com/wash2/accesskit"'; \
		echo 'tag = "iced-xdg-surface-0.13-rc"'; \
		echo 'replace-with = "vendored-sources"'; \
		echo '[source."git+https://github.com/pop-os/cosmic-panel#6a783a18"]'; \
		echo 'git = "https://github.com/pop-os/cosmic-panel"'; \
		echo 'replace-with = "vendored-sources"'; \
		echo '[source."git+https://github.com/smithay/smithay.git?rev=da42569"]'; \
		echo 'git = "https://github.com/smithay/smithay.git"'; \
		echo 'rev = "da42569"'; \
		echo 'replace-with = "vendored-sources"'; \
	) > .cargo/config.toml

override_dh_auto_build:
	cargo build --release --offline

override_dh_auto_test:
	:

override_dh_auto_install:
	# Install binary
	install -Dm755 target/release/cosmic-app-library $(CURDIR)/debian/tmp/usr/bin/cosmic-app-library
	# Install desktop file and metainfo
	mkdir -p $(CURDIR)/debian/tmp/usr/share/applications $(CURDIR)/debian/tmp/usr/share/metainfo
	install -m644 data/*.desktop $(CURDIR)/debian/tmp/usr/share/applications/
	install -m644 data/*.xml $(CURDIR)/debian/tmp/usr/share/metainfo/
	# Install icons
	mkdir -p $(CURDIR)/debian/tmp/usr/share/icons/hicolor/scalable/apps
	cp data/icons/*.svg $(CURDIR)/debian/tmp/usr/share/icons/hicolor/scalable/apps/
	# Install registry for dev package
	mkdir -p $(CURDIR)/debian/tmp/usr/share/cargo/registry/cosmic-app-library-1.0.2
	cp -r src Cargo.toml $(CURDIR)/debian/tmp/usr/share/cargo/registry/cosmic-app-library-1.0.2/

override_dh_install:
	dh_install --sourcedir=debian/tmp
