#!/usr/bin/make -f

DESTDIR = $(CURDIR)/debian/effetune
APPDIR = $(DESTDIR)/usr/lib/effetune

%:
	dh $@

override_dh_auto_clean:
	dh_clean

override_dh_auto_build:
	test -f electron/main.js
	test -f plugins/dsp/effetune-dsp.wasm
	test -f plugins/dsp/effetune-dsp.simd.wasm
	test -f js/vendor/sqlite/sqlite3.wasm
	test -x out/native/openhome-sidecar/linux-x64/effetune-openhome-sidecar

override_dh_auto_install:
	install -d $(APPDIR) $(DESTDIR)/usr/bin \
		$(DESTDIR)/usr/share/doc/effetune/third-party-licenses
	cp -a electron js plugins features presets images docs node_modules $(APPDIR)/
	install -m 0644 effetune.html effetune.css effetune-mobile.css \
		effetune-library.css pipeline-analyzer.css sw.js sw-precache.js \
		manifest.json package.json package-lock.json LICENSE README.md $(APPDIR)/
	# A directly launched source tree has app.isPackaged=false, so upstream
	# resolves the helper through this development-layout path.
	install -d $(APPDIR)/out/native/openhome-sidecar
	install -m 0755 out/native/openhome-sidecar/linux-x64/effetune-openhome-sidecar \
		$(APPDIR)/out/native/openhome-sidecar/
	install -m 0644 native/openhome-sidecar/THIRD_PARTY_NOTICES.txt \
		native/openhome-sidecar/licenses/libnl-LGPL-2.1.txt \
		$(APPDIR)/out/native/openhome-sidecar/
	install -D -m 0644 build/icon.png \
		$(DESTDIR)/usr/share/icons/hicolor/512x512/apps/effetune.png
	install -D -m 0644 debian/effetune.desktop \
		$(DESTDIR)/usr/share/applications/effetune.desktop
	find node_modules -type f \( -iname 'license*' -o -iname 'licence*' \
		-o -iname 'notice*' -o -iname 'copying*' \) -exec cp --parents '{}' \
		$(DESTDIR)/usr/share/doc/effetune/third-party-licenses/ \;
	printf '%s\n' \
		'#!/bin/sh' \
		'set -eu' \
		'unset ELECTRON_RUN_AS_NODE' \
		'exec /usr/lib/electron-runtime-44/electron /usr/lib/effetune "$$@"' \
		> $(DESTDIR)/usr/bin/effetune
	chmod 0755 $(DESTDIR)/usr/bin/effetune
	# Enforce the application/runtime boundary and pure-JavaScript Node graph.
	rm -rf $(APPDIR)/node_modules/electron $(APPDIR)/node_modules/electron-builder \
		$(APPDIR)/node_modules/electron-updater
	! find $(APPDIR) -type f \( -name '*.node' -o -name electron \
		-o -name chrome-sandbox -o -name chrome_crashpad_handler \
		-o -name app-update.yml -o -name latest.yml \) -print -quit | grep -q .

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