#!/usr/bin/make -f

export LC_ALL=C.UTF-8

%:
	dh $@

get-orig-source:
	debian/get-orig-source

# The tree is copied into debian/build-tree first: three of the submodules come
# from source packages of their own, and the source package must stay clean.
# Six of the bundles Zotero loads are upstream's build of the single-file-core
# package, whose sources are not in the tarball: they are in
# debian/missing-sources and are rebuilt here from them.  XRegExp comes from
# node-xregexp for the same reason.
SF := resource/SingleFile
SF_CORE := $(SF)/node_modules/single-file-core
SF_ROLLUP := rollup --format umd -p node-resolve

override_dh_auto_build:
	node debian/build-citeproc.js debian/missing-sources/citeproc-js/src 		chrome/content/zotero/xpcom/citeproc.js
	rm -rf debian/build-alfaaz
	cp -a debian/missing-sources/alfaaz debian/build-alfaaz
	cd debian/build-alfaaz && tsc -p tsconfig.json --rootDir src --outDir out --module es2020 --declaration false
	rollup debian/build-alfaaz/out/index.js --format es -p node-resolve --file resource/allfaz.mjs
	rm -rf debian/build-alfaaz
	mkdir -p $(SF)/node_modules
	ln -sfn $(CURDIR)/debian/missing-sources/single-file-core $(SF_CORE)
	$(SF_ROLLUP) --name singlefile --file $(SF)/lib/single-file.js $(SF_CORE)/single-file.js
	$(SF_ROLLUP) --name singlefile --file $(SF)/lib/single-file-frames.js $(SF_CORE)/single-file-frames.js
	$(SF_ROLLUP) --name singlefileBootstrap --file $(SF)/lib/single-file-bootstrap.js $(SF_CORE)/single-file-bootstrap.js
	$(SF_ROLLUP) --file $(SF)/lib/single-file-hooks-frames.js $(SF_CORE)/single-file-hooks-frames.js
	$(SF_ROLLUP) --file $(SF)/lib/single-file-infobar.js $(SF_CORE)/single-file-infobar.js
	$(SF_ROLLUP) --name singlefile --file $(SF)/lib/single-file-extension-editor-helper.js $(SF)/src/ui/content/content-ui-editor-helper-web.js
	rm -f $(SF_CORE)
	rmdir $(SF)/node_modules 2>/dev/null || true
	install -D -m644 /usr/share/nodejs/xregexp/xregexp-all.js chrome/content/zotero/xpcom/utilities/xregexp-all.js
	sh debian/build.sh
	bash debian/assemble.sh

# Upstream's suite needs its packed test runner and a browser window.
override_dh_auto_test:

override_dh_auto_install:
	install -d debian/zotero/usr/lib/zotero/app
	install -d debian/zotero/usr/bin
	install -d debian/zotero/usr/share/applications
	install -d debian/zotero/usr/share/man/man1

	install -m644 debian/build-xre/omni.ja debian/zotero/usr/lib/zotero/omni.ja
	install -m644 debian/build-app/omni.ja debian/zotero/usr/lib/zotero/app/omni.ja
	cp -a debian/build-resource debian/zotero/usr/lib/zotero/app/resource
	install -m644 debian/build-app/application.ini debian/zotero/usr/lib/zotero/app/application.ini
	install -m755 /usr/lib/firefox-esr/firefox-esr debian/zotero/usr/lib/zotero/zotero-bin

	install -m755 debian/bin/zotero debian/zotero/usr/bin/zotero
	install -m644 debian/zotero.desktop debian/zotero/usr/share/applications/zotero.desktop
	install -m644 debian/man/zotero.1 debian/zotero/usr/share/man/man1/zotero.1

	install -D -m644 app/linux/icons/icon32.png \
		debian/zotero/usr/share/icons/hicolor/32x32/apps/zotero.png
	install -D -m644 app/linux/icons/icon64.png \
		debian/zotero/usr/share/icons/hicolor/64x64/apps/zotero.png
	install -D -m644 app/linux/icons/icon128.png \
		debian/zotero/usr/share/icons/hicolor/128x128/apps/zotero.png
	install -D -m644 app/linux/icons/symbolic.svg \
		debian/zotero/usr/share/icons/hicolor/scalable/apps/zotero.svg

# zotero-bin is firefox-esr's launcher, copied so that /proc/self/exe points
# into /usr/lib/zotero; stripping or analysing it is not our business.
override_dh_strip:
	dh_strip -Xzotero-bin

# The launcher and the modified omni.ja are firefox-esr's, which is MPL-2.0 and
# has to keep the source of that exact version available (Policy 7.8).
FIREFOX_ESR_VERSION := $(shell dpkg-query -W -f='$${Version}' firefox-esr)

override_dh_gencontrol:
	dh_gencontrol -- -DBuilt-Using="firefox-esr (= $(FIREFOX_ESR_VERSION))"

override_dh_shlibdeps:
	dh_shlibdeps -- --ignore-missing-info

override_dh_clean:
	dh_clean
	rm -rf debian/build-src debian/build-tree debian/build-xre debian/build-app \
		debian/build-resource
	rm -f $(SF)/lib/single-file.js $(SF)/lib/single-file-frames.js \
		$(SF)/lib/single-file-bootstrap.js $(SF)/lib/single-file-hooks-frames.js \
		$(SF)/lib/single-file-infobar.js \
		$(SF)/lib/single-file-extension-editor-helper.js \
		chrome/content/zotero/xpcom/utilities/xregexp-all.js \
		chrome/content/zotero/xpcom/citeproc.js resource/allfaz.mjs
