#!/usr/bin/make -f
# Debian repack of the upstream localsend .deb (binary re-distributor, same
# payload policy as the RPM spec: usr/share copy, /opt/localsend_app
# payload, launcher wrapper since upstream ships no /usr/bin entry).
# No network at build time: the orig tarball holds the upstream .deb,
# opened here with binutils ar + tar (data.tar may be .xz or .zst,
# same as the RPM %prep).
%:
	dh $@

override_dh_auto_configure:
	:

override_dh_auto_build:
	:

# Prebuilt upstream binaries are already stripped and carry no
# usable debuginfo -- dwz aborts on them (exit 25). Skip it; same
# class as the RPM-side debuginfo exclusion for repackaged binaries.
# Binary repack: the payload is prebuilt upstream, so automatic
# shlibdeps analysis cannot run (buildroot holds none of the
# runtime libs) and its ${shlib:Depends} output is unused -- the
# manually maintained Depends above (derived from the payload's
# ELF DT_NEEDED union) IS the dependency declaration.
override_dh_shlibdeps:
	:

override_dh_dwz:
	:

override_dh_auto_test:
	:

override_dh_auto_install:
	mkdir -p payload
	cd payload && ar x "$$(ls ../*.deb)" data.tar.xz data.tar.zst 2>/dev/null; cd ..
	if [ -f payload/data.tar.xz ]; then \
	    tar -xf payload/data.tar.xz -C payload; \
	elif [ -f payload/data.tar.zst ]; then \
	    tar --zstd -xf payload/data.tar.zst -C payload; \
	else \
	    echo "ERROR: unexpected data.tar compression in upstream DEB"; exit 1; \
	fi
	mkdir -p debian/localsend/usr/bin debian/localsend/usr/share debian/localsend/opt
	cp -a payload/usr/share/* debian/localsend/usr/share/
	cp -a payload/opt/localsend_app debian/localsend/opt/
	printf '#!/bin/sh\nexec /opt/localsend_app/localsend_app "$$@"\n' > debian/localsend/usr/bin/localsend_app
	chmod 0755 debian/localsend/usr/bin/localsend_app
