#!/usr/bin/make -f
# Debian repack of the upstream artcraft-launcher .deb (binary
# re-distributor, same file policy as the RPM spec: /usr hierarchy
# installed verbatim - upstream already ships correct FHS layout).
# Upstream's own Depends verified correct (t64-era names need no
# adjustment), with the vulkan/egl alternative spelled out explicitly.
# No network at build time: the orig tarball holds the upstream .deb,
# opened here with binutils ar + tar (data.tar may be .gz/.xz/.zst).
%:
	dh $@

override_dh_auto_configure:
	:

override_dh_auto_build:
	:

# Upstream binaries are prebuilt/pre-stripped with no usable debuginfo
# (dh_dwz warns 'no debug information at all'; strip fails on them).
# Skip both, same pattern as obsidian/openchamber/stoat-desktop rules.
override_dh_dwz:
	:

override_dh_strip:
	:

override_dh_auto_test:
	: # no-op: validating debian/<pkg>/... here runs BEFORE auto_install
	: # stages the payload (same ordering trap cadcraft documents); the
	: # desktop entry is validated by the RPM %check instead.

override_dh_auto_install:
	mkdir -p payload
	UPDEB=$$(ls *-linux-x86_64.deb 2>/dev/null | head -n1); \
	if [ -z "$$UPDEB" ] && [ "$$(ls *.deb 2>/dev/null | wc -l)" = 1 ]; then UPDEB=$$(ls *.deb); fi; \
	if [ -z "$$UPDEB" ]; then echo "ERROR: no x86_64 upstream .deb in orig wrapper (found: $$(ls *.deb 2>/dev/null | tr \n \ ))"; exit 1; fi; \
	echo "upstream payload: $$UPDEB"; \
	HAVE=$$(dpkg-deb -f "$$UPDEB" Architecture 2>/dev/null); \
	if [ "$$HAVE" != "amd64" ] && [ "$$HAVE" != "all" ]; then echo "ERROR: payload arch $$HAVE != amd64 ($$UPDEB)"; exit 1; fi; \
	cd payload && ar x "../$$UPDEB" 2>/dev/null; cd ..
	if [ -f payload/data.tar.gz ]; then \
	    tar -xzf payload/data.tar.gz -C payload; \
	elif [ -f payload/data.tar.xz ]; then \
	    tar -xJf 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/craft-launcher/usr/bin debian/craft-launcher/usr/share
	cp -a payload/usr/bin/* debian/craft-launcher/usr/bin/
	cp -a payload/usr/share/applications debian/craft-launcher/usr/share/
	cp -a payload/usr/share/icons debian/craft-launcher/usr/share/
	cp -a payload/usr/share/metainfo debian/craft-launcher/usr/share/
	cp -a payload/usr/share/doc debian/craft-launcher/usr/share/
