#!/usr/bin/make -f

DESTDIR = $(CURDIR)/debian/balena-etcher
APPDIR = $(DESTDIR)/usr/lib/balena-etcher
UPSTREAM = $(CURDIR)/upstream-root/usr/lib/balena-etcher

%:
	dh $@

override_dh_auto_build:
	# Upstream application resources are already built for this exact ref.

override_dh_auto_install:
	install -d $(APPDIR)/resources $(DESTDIR)/usr/bin
	install -m 0644 $(UPSTREAM)/resources/app.asar $(APPDIR)/resources/app.asar
	cp -a $(UPSTREAM)/resources/app.asar.unpacked $(APPDIR)/resources/
	install -m 0755 $(UPSTREAM)/resources/etcher-util $(APPDIR)/resources/etcher-util
	install -D -m 0644 debian/balena-etcher.desktop \
		$(DESTDIR)/usr/share/applications/balena-etcher.desktop
	install -D -m 0644 upstream-root/usr/share/pixmaps/balena-etcher.png \
		$(DESTDIR)/usr/share/pixmaps/balena-etcher.png
	printf '%s\n' \
		'#!/bin/sh' \
		'set -eu' \
		'unset ELECTRON_RUN_AS_NODE' \
		'exec /usr/lib/electron-runtime-44/electron /usr/lib/balena-etcher/resources/app.asar "$$@"' \
		> $(DESTDIR)/usr/bin/balena-etcher
	chmod 0755 $(DESTDIR)/usr/bin/balena-etcher
	# Enforce the application/runtime ownership boundary and updater policy.
	test ! -e $(APPDIR)/resources/app-update.yml
	test -x $(APPDIR)/resources/etcher-util
	! find $(APPDIR) -type f \( -name electron -o -name chrome-sandbox \
		-o -name chrome_crashpad_handler -o -name resources.pak \
		-o -name icudtl.dat -o -name libffmpeg.so -o -name libvulkan.so.1 \
		-o -name libvk_swiftshader.so -o -name libGLESv2.so -o -name libEGL.so \
		-o -name v8_context_snapshot.bin -o -name snapshot_blob.bin \
		-o -name vk_swiftshader_icd.json -o -name version \) -print -quit | grep -q .

override_dh_auto_test:
	# Packaging assertions covered by test/test-shared-electron-balena-etcher.sh;
	# the real write/verify acceptance test needs a genuine block device and
	# runs separately in a disposable VM (scripts/test-balena-etcher-vm-write.sh).

override_dh_dwz:
	# Preserve the upstream-built etcher-util helper byte-for-byte: rewriting
	# it (even just re-linked debug sections) makes the pkg-produced ELF exit
	# with "Pkg: Error reading from file".

override_dh_strip:
	# Same reason as dh_dwz above; it is also already stripped by upstream.

override_dh_shlibdeps:
	dh_shlibdeps --exclude=app.asar.unpacked
