#!/usr/bin/make -f
DEST=$(CURDIR)/debian/mattermost-desktop
UPSTREAM=$(CURDIR)/upstream-root
ROOT=$(UPSTREAM)/opt/Mattermost
%:
	dh $@
override_dh_auto_build:
override_dh_auto_install:
	install -d $(DEST)/usr/lib/mattermost-desktop $(DEST)/usr/bin $(DEST)/usr/share/icons
	install -m 0644 $(ROOT)/resources/app.asar $(DEST)/usr/lib/mattermost-desktop/app.asar
	# Electron resolves unpacked modules from app.asar.unpacked beside app.asar.
	cp -a $(ROOT)/resources/app.asar.unpacked $(DEST)/usr/lib/mattermost-desktop/app.asar.unpacked
	# --disableDevMode: the app is not an electron-builder bundle, so
	# electron-is-dev reports development and would otherwise fetch React and
	# Redux DevTools extensions from the network on every start.
	printf '%s\n' '#!/bin/sh' 'set -eu' 'unset ELECTRON_RUN_AS_NODE' 'exec /usr/lib/electron-runtime-43/electron /usr/lib/mattermost-desktop/app.asar --disableDevMode "$$@"' > $(DEST)/usr/bin/mattermost-desktop
	chmod 0755 $(DEST)/usr/bin/mattermost-desktop
	install -D -m 0644 debian/mattermost-desktop.desktop $(DEST)/usr/share/applications/mattermost-desktop.desktop
	cp -a $(UPSTREAM)/usr/share/icons/hicolor $(DEST)/usr/share/icons/
	test -s $(DEST)/usr/share/icons/hicolor/256x256/apps/mattermost-desktop.png
	test ! -e $(DEST)/usr/lib/mattermost-desktop/app-update.yml
	# The Linux amd64 Koffi addon is the only native code that ships; it is a
	# Node-API v8 module, so it is not bound to one Electron ABI. The runtime
	# line is pinned to 43 by app.isUnityRunning, which Electron 44 removed.
	test -s $(DEST)/usr/lib/mattermost-desktop/app.asar.unpacked/node_modules/@koromix/koffi-linux-x64/linux_x64/koffi.node
	! find $(DEST)/usr/lib/mattermost-desktop -type f -name '*.node' -not -path '*/@koromix/koffi-linux-x64/*' -print -quit | grep -q .
	! find $(DEST)/usr/lib/mattermost-desktop -type f \( -name electron -o -name chrome-sandbox -o -name chrome_crashpad_handler -o -name resources.pak -o -name libffmpeg.so -o -name 'libvk_swiftshader.so' -o -name 'v8_context_snapshot.bin' \) -print -quit | grep -q .
override_dh_auto_test:
	# Covered by test/test-shared-electron-mattermost-desktop.sh.
