#!/usr/bin/make -f

DESTDIR = $(CURDIR)/debian/heroic
APPDIR  = $(DESTDIR)/usr/lib/heroic
BINDIR  = $(APPDIR)/app.asar.unpacked/build/bin/x64
HELPER  = $(CURDIR)/vulkan-helper-src
ICONSIZES = 16x16 32x32 48x48 64x64 128x128 256x256 512x512 1024x1024

export CARGO_HOME = $(CURDIR)/debian/cargo-home

%:
	dh $@

override_dh_auto_build:
	# The application resources come from the checksum-pinned outer build. The
	# only thing compiled here is the Vulkan capability helper, built from the
	# commit-pinned MIT sources staged by build.sh against their own lockfile.
	# Upstream ships this one as a prebuilt blob committed to its repository in
	# 2023 with no release behind it, which is the single artifact in the
	# payload that could not otherwise be traced to a source revision.
	cargo build --release --locked --manifest-path $(HELPER)/Cargo.toml
	test -x $(HELPER)/target/release/vulkan-helper

override_dh_auto_install:
	install -d $(APPDIR) $(DESTDIR)/usr/bin
	install -m 0644 app-payload/app.asar $(APPDIR)/app.asar
	# Electron resolves unpacked resources from app.asar.unpacked beside
	# app.asar, and Heroic reaches every one of them through fixAsarPath().
	cp -a app-payload/app.asar.unpacked $(APPDIR)/app.asar.unpacked
	install -m 0755 $(HELPER)/target/release/vulkan-helper $(BINDIR)/linux/vulkan-helper
	install -D -m 0644 $(HELPER)/LICENSE $(APPDIR)/third-party-licenses/vulkan-helper-rs.LICENSE
	printf '%s\n' \
		'#!/bin/sh' \
		'set -eu' \
		'unset ELECTRON_RUN_AS_NODE' \
		'exec /usr/lib/electron-runtime-43/electron /usr/lib/heroic/app.asar "$$@"' \
		> $(DESTDIR)/usr/bin/heroic
	chmod 0755 $(DESTDIR)/usr/bin/heroic
	install -D -m 0644 debian/heroic.desktop \
		$(DESTDIR)/usr/share/applications/heroic.desktop
	set -e; for size in $(ICONSIZES); do \
		install -D -m 0644 icons/$$size.png \
			$(DESTDIR)/usr/share/icons/hicolor/$$size/apps/heroic.png; \
	done
	# Enforce the application/runtime ownership boundary: the shared runtime owns
	# every Electron and Chromium object, and APT owns updates.
	test ! -e $(APPDIR)/app-update.yml
	test ! -e $(APPDIR)/package-type
	! find $(APPDIR) -type f \( -name electron -o -name chrome-sandbox \
		-o -name chrome_crashpad_handler -o -name resources.pak \
		-o -name chrome_100_percent.pak -o -name chrome_200_percent.pak \
		-o -name icudtl.dat -o -name snapshot_blob.bin \
		-o -name v8_context_snapshot.bin -o -name libffmpeg.so \
		-o -name libvk_swiftshader.so -o -name libvulkan.so.1 \
		-o -name '*.so' -o -name '*.so.*' -o -name '*.node' \) -print -quit | grep -q .
	# The five Linux store/Vulkan helpers and the two Wine-prefix shims, and
	# nothing else, are the executables in the payload.
	test "$$(find $(BINDIR)/linux -type f | wc -l)" = 5
	test "$$(find $(BINDIR)/win32 -type f | wc -l)" = 2
	# The Vulkan helper must be our own build, not the unprovenanced 2023 blob.
	file -b $(BINDIR)/linux/vulkan-helper | grep -q 'ELF 64-bit LSB pie executable'
	! echo '1c355996ba14a4b2ea3f34d868f5fcfccbd804f0e5a0b04ac37bc8bbc7077447  $(BINDIR)/linux/vulkan-helper' | sha256sum -c - 2>/dev/null
	# It loads Vulkan through dlopen, so no Vulkan loader may be linked in.
	! objdump -p $(BINDIR)/linux/vulkan-helper | grep -q 'NEEDED.*libvulkan'

# The retained store clients must reach the user exactly as their own upstreams
# published them, so they are excluded from everything that rewrites a binary.
# dh_strip_nondeterminism is not optional here: legendary and gogdl are Python
# zipapps, and its ZIP handler normalises the archive members' timestamps, which
# silently rewrote legendary (14022624 -> 14022574 bytes) and broke the
# byte-for-byte identity this package's redistribution claim depends on. The
# inputs are already checksum-pinned, so normalising them buys no
# reproducibility and costs the provenance chain.
override_dh_strip:
	dh_strip -Xapp.asar.unpacked
	strip --strip-unneeded $(BINDIR)/linux/vulkan-helper

override_dh_dwz:
	dh_dwz -Xapp.asar.unpacked

override_dh_strip_nondeterminism:
	dh_strip_nondeterminism -Xapp.asar.unpacked

# Verified last, after every debhelper step that can rewrite a file, so that a
# tool which quietly modifies a vendored artifact fails the build instead of
# shipping a binary that no longer matches the release named in
# debian/copyright. This is the check that would have caught the
# dh_strip_nondeterminism regression above.
override_dh_builddeb:
	echo 'c83d1595a9e2cbae4e66b69ecaa1f8649da99b617a72f93312d342e6a5a799c7  $(BINDIR)/linux/legendary' | sha256sum -c -
	echo 'cba013d42767c808237c437335ab1d56f58405d07e8f37b3324d264ea5c49655  $(BINDIR)/linux/gogdl' | sha256sum -c -
	echo 'd73519514df9d52c50c6feec629373cc3a0d9504e087b6c7372a8ca8a95e7bfe  $(BINDIR)/linux/nile' | sha256sum -c -
	echo 'cf9a0e44dbedd0fea283ac6398e1277df7516711b486c21629103c873b0a6a7d  $(BINDIR)/linux/comet' | sha256sum -c -
	echo 'abc208076a778ee738cae8451c9be7ab33c9787b0b69b2e7e4ffc70becc39e1e  $(BINDIR)/win32/GalaxyCommunication.exe' | sha256sum -c -
	echo '1feb21e21e19cbb34e881791c8f2557769f59231e46850915d49a6d0c1ce4583  $(BINDIR)/win32/EpicGamesLauncher.exe' | sha256sum -c -
	dh_builddeb

override_dh_auto_clean:
	rm -rf $(HELPER)/target debian/cargo-home

override_dh_auto_test:
	# Runtime behavior is covered by test/test-shared-electron-heroic.sh.
