#!/usr/bin/make -f
# Debian source build of upstream main (same env recipe as the RPM spec
# and upstream's own Linux CI: BtbN FFmpeg tree via FFMPEG_DIR, Microsoft
# ONNX build via ORT_LIB_LOCATION + dynamic preference, vendored crates
# offline, rpath $ORIGIN/lib with old-style DT_RPATH). The unified orig
# tarball carries everything (top dir concat-<commit>/): source tree with
# src/vendor + src/.cargo, plus ffmpeg-dev/ and onnxlib/ beside it.
# shlibdeps is skipped (bundled FFmpeg/ONNX have no system providers);
# Depends are hand-mapped like all other repo recipes and re-verified by
# docker-sweep ldd on the built .deb.
# NOTE (2026-10-09): dpkg-source -x strips the single top-level dir
# (repo convention, cf. lazyvim-git/matugen rules) -- the payload lands
# at the build root, so NEVER glob for concat-*/ here (it matches
# nothing post-strip and failed the first Debian_Testing build).
# SKIA RESOLVED (2026-10-09): same prebuilt-binary mechanism as the RPM
# spec (archive prefetched into skia-cache/ by update.sh step 4b,
# key read from the OBS log, never guessed).
%:
	dh $@

override_dh_auto_configure:
	:

override_dh_auto_build:
	cd src && \
	CARGO_NET_OFFLINE=true ORT_SKIP_DOWNLOAD=1 \
	FFMPEG_DIR="$$PWD/../ffmpeg-dev" ORT_LIB_LOCATION="$$PWD/../onnxlib" \
	ORT_PREFER_DYNAMIC_LINK=1 \
	SKIA_BINARIES_URL="file://$$PWD/../skia-cache/skia-binaries-b7f043e0b1e2a850e702-x86_64-unknown-linux-gnu-ganesh-gl-jpegd-jpege-pdf-vulkan.tar.gz" \
	SHERPA_ONNX_ARCHIVE_DIR="$$PWD/../sherpa-cache" \
	RUSTFLAGS="-C link-arg=-Wl,-rpath,\$$ORIGIN/lib -C link-arg=-Wl,--disable-new-dtags" \
	cargo build --offline --frozen --release --bin concat

override_dh_auto_test:
	:

override_dh_shlibdeps:
	: # bundled FFmpeg/ONNX have no system providers; hand-mapped Depends instead

override_dh_auto_install:
	mkdir -p debian/concat-unstable/opt/concat-unstable/lib debian/concat-unstable/usr/bin debian/concat-unstable/usr/share/applications
	install -m0755 src/target/release/concat debian/concat-unstable/opt/concat-unstable/concat-unstable
	cp -a ffmpeg-dev/lib/lib*.so* debian/concat-unstable/opt/concat-unstable/lib/
	cp -a onnxlib/libonnxruntime.so* debian/concat-unstable/opt/concat-unstable/lib/
	printf '#!/bin/sh\nexec /opt/concat-unstable/concat-unstable "$$@"\n' > debian/concat-unstable/usr/bin/concat-unstable
	chmod 0755 debian/concat-unstable/usr/bin/concat-unstable
	printf '[Desktop Entry]\nType=Application\nName=Concat (Unstable)\nComment=Video editor - main branch snapshot\nExec=/usr/bin/concat-unstable %%U\nIcon=concat-unstable\nTerminal=false\nCategories=AudioVideo;Video;\n' > debian/concat-unstable/usr/share/applications/concat-unstable.desktop
	install -Dm644 assets/icons/concat_logo_256.png debian/concat-unstable/usr/share/icons/hicolor/256x256/apps/concat-unstable.png

override_dh_clean:
	dh_clean -X.orig
# NOTE (2026-10-09): dh_clean deletes every *.orig as "detritus", but
# cargo verifies the sha256 of each vendor/*/Cargo.toml.orig recorded in
# .cargo-checksum.json -- deleting them breaks the offline build
# ("failed to calculate checksum of .../vendor/slint-build/Cargo.toml.orig",
# Debian_Testing 17:29 UTC build). Same fix as matugen's rules.
