#!/usr/bin/make -f
DEST=$(CURDIR)/debian/ueli
LIB=$(DEST)/usr/lib/ueli
UNPACKED=$(LIB)/app.asar.unpacked/node_modules
BS3=$(UNPACKED)/better-sqlite3/build/Release/better_sqlite3.node
SHARP=$(UNPACKED)/@img/sharp-linux-x64/lib/sharp-linux-x64.node
LIBVIPS=$(UNPACKED)/@img/sharp-libvips-linux-x64/lib/libvips-cpp.so.8.17.3

%:
	dh $@

override_dh_auto_build:
	# Application resources are produced by the checksum-pinned outer build.

override_dh_auto_install:
	install -d $(LIB) $(DEST)/usr/bin
	cp -a app-payload/. $(LIB)/
	printf '%s\n' '#!/bin/sh' 'set -eu' 'unset ELECTRON_RUN_AS_NODE' 'exec /usr/lib/electron-runtime-44/electron /usr/lib/ueli/app.asar "$$@"' > $(DEST)/usr/bin/ueli
	chmod 0755 $(DEST)/usr/bin/ueli
	install -D -m 0644 debian/ueli.desktop $(DEST)/usr/share/applications/ueli.desktop
	install -D -m 0644 app-payload/icon.png $(DEST)/usr/share/icons/hicolor/512x512/apps/ueli.png
	rm -f $(LIB)/icon.png
	# Electron resolves unpacked modules from app.asar.unpacked beside app.asar.
	test -s $(LIB)/app.asar
	test -s $(BS3)
	test -s $(SHARP)
	test -s $(LIBVIPS)
	# Exactly two native addons ship, and each is the one it claims to be.
	# better-sqlite3 is a V8-ABI addon rebuilt for the Electron 44 line; sharp
	# is Node-API and therefore stable across majors. Anything else is a bug.
	test "$$(find $(LIB) -type f -name '*.node' | wc -l)" = 2
	! readelf --dyn-syms -W $(BS3) | grep -Fq napi_register_module_v1
	readelf --dyn-syms -W $(BS3) | awk '$$7 == "UND" { print $$8 }' | grep -qE '^_ZN2v8|^_ZN4node'
	# A .node records its ABI as a binary field, so the number cannot be read
	# here; the outer build checks the Electron headers it compiled against and
	# the container test loads the object against the installed runtime.
	readelf --dyn-syms -W $(SHARP) | grep -Fq napi_register_module_v1
	! readelf --dyn-syms -W $(SHARP) | awk '$$7 == "UND" { print $$8 }' | grep -qE '^_ZN2v8|^_ZN4node'
	# The one shared object is sharp's private libvips. It is not replaceable by
	# Ubuntu's libvips42t64: every codec is statically linked into it and its
	# soname is upstream's own. See shared-electron/evaluations/ueli.html.
	test "$$(find $(LIB) -type f \( -name '*.so' -o -name '*.so.*' \) | wc -l)" = 1
	readelf -d -W $(LIBVIPS) | grep -Fq 'Library soname: [libvips-cpp.so.8.17.3]'
	! readelf -d -W $(LIBVIPS) | grep -E 'NEEDED' | grep -qE 'libjpeg|libpng|libwebp|libtiff|libvips'
	# The musl builds of sharp cannot load against glibc and do not ship; their
	# JavaScript wrappers must remain, because Electron serves an unpacked
	# module's whole directory from disk.
	test -s $(UNPACKED)/@img/sharp-linuxmusl-x64/package.json
	! test -e $(UNPACKED)/@img/sharp-linuxmusl-x64/lib/sharp-linuxmusl-x64.node
	! test -e $(UNPACKED)/@img/sharp-libvips-linuxmusl-x64/lib/libvips-cpp.so.8.17.3
	# Rollup is a build tool that reached the payload as an optionalDependency of
	# tabster. Its binary goes; its manifest stays, for the same reason.
	! test -e $(UNPACKED)/@rollup/rollup-linux-x64-gnu/rollup.linux-x64-gnu.node
	test -s $(UNPACKED)/@rollup/rollup-linux-x64-gnu/package.json
	# APT owns updates: no updater code and no electron-builder feed descriptor.
	! grep -aq 'electron-updater' $(LIB)/app.asar
	! grep -aq 'autoUpdater' $(LIB)/app.asar
	! grep -aq 'setFeedURL' $(LIB)/app.asar
	! test -e $(LIB)/app-update.yml
	# No private Electron/Chromium runtime travels with the application.
	! find $(LIB) -type f \( -name electron -o -name chrome-sandbox \
	    -o -name chrome_crashpad_handler -o -name resources.pak -o -name icudtl.dat \
	    -o -name '*.pak' -o -name '*.bin' \) -print -quit | grep -q .
	! find $(LIB) -type f \( -name '*.so' -o -name '*.so.*' \) \
	    -not -path '*/sharp-libvips-linux-x64/lib/*' -print -quit | grep -q .
	! find $(LIB) -type f -name '*.node' \
	    -not -path '*/better-sqlite3/build/Release/*' \
	    -not -path '*/sharp-linux-x64/lib/*' -print -quit | grep -q .

override_dh_makeshlibs:
	# The one shared object is sharp's private libvips, reached only through the
	# addon's RPATH. It is never on the system linker path, so the package must
	# not activate the ldconfig trigger.
	dh_makeshlibs -n

override_dh_shlibdeps:
	# sharp's addon links its private libvips by soname through an RPATH, and no
	# Ubuntu package provides libvips-cpp.so.8.17.3, so dh_shlibdeps has to be
	# told where the shipped copy lives. Without -l it fails outright rather
	# than falling back. Everything else still resolves to Ubuntu sonames.
	dh_shlibdeps -l$(CURDIR)/debian/ueli/usr/lib/ueli/app.asar.unpacked/node_modules/@img/sharp-libvips-linux-x64/lib

override_dh_auto_test:
	# Runtime behaviour is covered by test/test-shared-electron-ueli.sh.

override_dh_strip_nondeterminism:
	# The ASAR is a checksum-pinned application archive; do not rewrite it.

override_dh_dwz:
	# The prebuilt sharp addon and its private libvips carry no usable debug
	# info to compress; dwz must not touch upstream's published binaries.
