#!/bin/sh
set -e

# The build tools are ES modules and Node's ESM resolver ignores NODE_PATH, so
# they have to be reachable through a local node_modules tree.  chokidar comes
# in through tasks/task.js, ws through tasks/reload.js, and the TypeScript
# declarations of the browser APIs are shipped in debian/build_modules (see
# debian/copyright).
mkdir -p node_modules/@rollup node_modules/@types
for module in rollup typescript tslib chokidar globby less malevic ws; do
	ln -sfn /usr/share/nodejs/$module node_modules/$module
done
for module in plugin-replace plugin-typescript plugin-node-resolve pluginutils; do
	ln -sfn /usr/share/nodejs/@rollup/$module node_modules/@rollup/$module
done
for module in chrome filesystem filewriter har-format; do
	ln -sfn ../../debian/build_modules/@types/$module node_modules/@types/$module
done

node debian/build-api.mjs
node debian/build-webext.mjs

# The user interface fonts are provided by the fonts-open-sans package and
# linked into the extension when the package is built (see
# debian/webext-dark-reader.links), so the copies vendored in the upstream tree
# must not be shipped.  The checks below catch a silently incomplete build.
rm -f build/release/firefox/ui/assets/fonts/OpenSans-*.ttf
test -s build/release/firefox/manifest.json
test -s build/release/firefox/background/index.js
test -s build/release/firefox/inject/index.js
test -s build/release/firefox/ui/popup/index.js
test -d build/release/firefox/_locales
test ! -e build/release/firefox/ui/assets/fonts/OpenSans-Regular.ttf
