#!/usr/bin/make -f
DEST=$(CURDIR)/debian/mailspring
PAYLOAD=$(CURDIR)/app-payload
LIBDIR=$(DEST)/usr/lib/mailspring
BS3=$(LIBDIR)/app.asar.unpacked/node_modules/better-sqlite3/build/Release/better_sqlite3.node
%:
	dh $@
override_dh_auto_build:
override_dh_auto_install:
	install -d $(LIBDIR) $(DEST)/usr/bin $(DEST)/usr/share/icons
	install -m 0644 $(PAYLOAD)/app.asar $(LIBDIR)/app.asar
	# Electron resolves unpacked members from app.asar.unpacked beside app.asar,
	# and Mailspring resolves its own sync engine the same way: mailsync-process.js
	# joins resourcePath with "mailsync" and rewrites app.asar to app.asar.unpacked.
	cp -a $(PAYLOAD)/app.asar.unpacked $(LIBDIR)/app.asar.unpacked
	install -m 0644 $(PAYLOAD)/LICENSE.upstream $(LIBDIR)/LICENSE
	printf '%s\n' '#!/bin/sh' 'set -eu' 'unset ELECTRON_RUN_AS_NODE' 'exec /usr/lib/electron-runtime-44/electron /usr/lib/mailspring/app.asar "$$@"' > $(DEST)/usr/bin/mailspring
	chmod 0755 $(DEST)/usr/bin/mailspring
	install -D -m 0644 debian/mailspring.desktop $(DEST)/usr/share/applications/mailspring.desktop
	cp -a $(CURDIR)/icons/hicolor $(DEST)/usr/share/icons/
	test -s $(DEST)/usr/share/icons/hicolor/256x256/apps/mailspring.png
	# No private Electron/Chromium runtime travels with the application: upstream's
	# 206 MiB `mailspring` binary, its sandbox and crashpad helpers, the .pak files,
	# the V8 snapshots and the Chromium shared objects all stay behind.
	! find $(LIBDIR) -type f \( -name electron -o -name mailspring -o -name chrome-sandbox \
	    -o -name chrome_crashpad_handler -o -name resources.pak -o -name '*.pak' \
	    -o -name icudtl.dat -o -name v8_context_snapshot.bin -o -name snapshot_blob.bin \
	    -o -name 'lib*.so' -o -name 'lib*.so.*' \) -print -quit | grep -q .
	# Exactly one native addon ships, and it is the better-sqlite3 object rebuilt
	# against this runtime's ABI. Upstream's copy is bound to NODE_MODULE_VERSION
	# 145 (Electron 41) and would refuse to load here; the Windows-only
	# windows-focus-assist objects are dropped, their JavaScript wrapper retained.
	test $$(find $(LIBDIR) -type f -name '*.node' | wc -l) -eq 1
	test -s $(BS3)
	readelf --dyn-syms -W $(BS3) | grep -q node_register_module_v149
	! readelf --dyn-syms -W $(BS3) | grep -q node_register_module_v145
	# The native sync engine and its wrapper must be present and executable.
	test -x $(LIBDIR)/app.asar.unpacked/mailsync.bin
	test -x $(LIBDIR)/app.asar.unpacked/mailsync
	! grep -q LD_LIBRARY_PATH $(LIBDIR)/app.asar.unpacked/mailsync
	# APT owns updates: the release poller is neutralised inside the ASAR. The
	# anchor is the SNAP-only guard, which only the Linux implementation carried;
	# a whole-archive grep for the query itself would also match Squirrel's
	# autoupdate-impl-win32.js, which is dead code on Linux.
	grep -aq 'APT is the only update authority' $(LIBDIR)/app.asar
	! grep -aq 'if (process.env.SNAP) {' $(LIBDIR)/app.asar
override_dh_dwz:
	# mailsync.bin ships with debug_info that dh_strip discards below, so there is
	# nothing for dwz to compress and no reason to spend minutes on a 48 MiB object.
override_dh_strip:
	# Strip, but do not emit an automatic -dbgsym package: this repository's
	# release registry tracks one binary package per source package.
	dh_strip --no-automatic-dbgsym
execute_after_dh_fixperms:
	chmod 0755 $(LIBDIR)/app.asar.unpacked/mailsync $(LIBDIR)/app.asar.unpacked/mailsync.bin
override_dh_auto_test:
	# Covered by test/test-shared-electron-mailspring.sh.
