#!/usr/bin/make -f

DESTDIR = $(CURDIR)/debian/penpot-desktop
APPDIR = $(DESTDIR)/usr/lib/penpot-desktop

%:
	dh $@

override_dh_auto_clean:
	# Keep upstream's immutable package-lock.json and controller-installed tree.
	dh_clean

override_dh_auto_build:
	# Upstream runs these ESM/CSS/HTML sources directly; no compilation is needed.
	test -f src/process/index.js
	test -d node_modules/electron-tabs

override_dh_auto_install:
	install -d $(APPDIR) $(DESTDIR)/usr/bin \
		$(DESTDIR)/usr/share/doc/penpot-desktop/third-party-licenses
	cp -a src node_modules $(APPDIR)/
	install -m 0644 package.json package-lock.json $(APPDIR)/
	install -D -m 0644 build/icon.png \
		$(DESTDIR)/usr/share/icons/hicolor/512x512/apps/penpot-desktop.png
	install -D -m 0644 debian/penpot-desktop.desktop \
		$(DESTDIR)/usr/share/applications/penpot-desktop.desktop
	install -D -m 0644 build/flatpak/com.authormore.penpotdesktop-mime.xml \
		$(DESTDIR)/usr/share/mime/packages/penpot-desktop.xml
	# Preserve disclosure texts for every installed lockfile dependency.
	find node_modules -type f \( -iname 'license*' -o -iname 'licence*' \
		-o -iname 'notice*' -o -iname 'copying*' \) -exec cp --parents '{}' \
		$(DESTDIR)/usr/share/doc/penpot-desktop/third-party-licenses/ \;
	test -n "$$(find $(DESTDIR)/usr/share/doc/penpot-desktop/third-party-licenses \
		-type f -print -quit)"
	printf '%s\n' \
		'#!/bin/sh' \
		'set -eu' \
		'unset ELECTRON_RUN_AS_NODE' \
		'exec /usr/lib/electron-runtime-44/electron /usr/lib/penpot-desktop "$$@"' \
		> $(DESTDIR)/usr/bin/penpot-desktop
	chmod 0755 $(DESTDIR)/usr/bin/penpot-desktop
	# Enforce app/runtime ownership and the audited no-native-module result.
	rm -rf $(APPDIR)/node_modules/electron $(APPDIR)/node_modules/electron-builder
	! find $(APPDIR) -type f \( -name '*.node' -o -name '*.so' -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-penpot-desktop.sh.
