#!/usr/bin/make -f

# Modules that webpack and gulp have to resolve by name.  Debian keeps them in
# /usr/share/nodejs, but both tools resolve from the source tree, so they are
# linked into node_modules below (see debian/README.source).
NODE_MODULES = \
	@babel/core @babel/preset-env @babel/preset-react @babel/preset-typescript \
	@babel/plugin-transform-runtime @babel/runtime \
	@csstools/postcss-light-dark-function @fluent/bundle @fluent/dom @fluent/react \
	@svgr/webpack \
	autoprefixer babel-loader cached-iterable classnames clean-webpack-plugin copy-webpack-plugin \
	css-loader darkreader eld event-emitter gulp gulp-postcss html-webpack-plugin \
	immutable \
	jszip luaparse mathjax-full mini-css-extract-plugin ordered-read-streams \
	pako path-browserify postcss postcss-discard-comments postcss-dir-pseudo-class \
	postcss-loader postcss-nesting postcss-rtlcss postcss-selector-parser prop-types \
	queue raw-loader react react-dom \
	sass sass-loader sentencex-ts sorted-btree terser-webpack-plugin vinyl \
	webpack webpack-cli webpack-stream

%:
	dh $@

override_dh_auto_configure:

override_dh_auto_build:
	mkdir -p node_modules
	for d in @babel @csstools @fluent @svgr; do mkdir -p node_modules/$$d; done
	for m in $(NODE_MODULES); do \
	  if [ -e /usr/share/nodejs/$$m ]; then \
	    ln -sf /usr/share/nodejs/$$m node_modules/$$m; \
	  else \
	    echo "zotero-reader: build dependency module missing: $$m" >&2; exit 1; \
	  fi; \
	done
	# epub.js is a "file:" dependency upstream; it is vendored in the tarball.
	ln -sf ../epubjs/epub.js node_modules/epubjs
	# pdf.js' gulpfile resolves the webpack aliases for the legacy "fluent-*"
	# names against its own directory (path.join(__dirname, ...)), so it needs
	# a node_modules below pdfjs/pdf.js as well.
	ln -sfn ../../node_modules pdfjs/pdf.js/node_modules
	# The CMap and standard font data live in zotero-pdfjs-data; they are
	# linked into the source tree here only so that pdf.js' build can copy
	# them, and are replaced by symlinks into that package at install time.
	ln -sfn /usr/share/zotero/resource/pdfjs/bcmaps pdfjs/pdf.js/external/bcmaps
	ln -sfn /usr/share/zotero/resource/pdfjs/standard_fonts pdfjs/pdf.js/external/standard_fonts
	# The QuickJS sandbox is built by pdfjs-quickjs; pdf.js' gulpfile copies it
	# from external/quickjs/ into the web build, so it is linked in here and the
	# installed package takes it from that package instead of shipping a copy.
	mkdir -p pdfjs/pdf.js/external/quickjs
	ln -sfn /usr/share/javascript/pdfjs-quickjs/quickjs-eval.js pdfjs/pdf.js/external/quickjs/quickjs-eval.js
	ln -sfn /usr/share/javascript/pdfjs-quickjs/quickjs-eval.wasm pdfjs/pdf.js/external/quickjs/quickjs-eval.wasm
	# The image decoders and the colour management module are built by
	# pdfjs-jbig2, pdfjs-openjpeg and pdfjs-qcms; pdf.js' gulpfile copies them
	# from external/ into the web build, so they are copied in here first.
	cp /usr/share/javascript/pdfjs-jbig2/jbig2.js pdfjs/pdf.js/external/jbig2/jbig2.js
	cp /usr/share/javascript/pdfjs-jbig2/jbig2.wasm pdfjs/pdf.js/external/jbig2/jbig2.wasm
	cp /usr/share/javascript/pdfjs-jbig2/jbig2_nowasm_fallback.js pdfjs/pdf.js/external/jbig2/jbig2_nowasm_fallback.js
	cp /usr/share/javascript/pdfjs-openjpeg/openjpeg.js pdfjs/pdf.js/external/openjpeg/openjpeg.js
	cp /usr/share/javascript/pdfjs-openjpeg/openjpeg.wasm pdfjs/pdf.js/external/openjpeg/openjpeg.wasm
	cp /usr/share/javascript/pdfjs-openjpeg/openjpeg_nowasm_fallback.js pdfjs/pdf.js/external/openjpeg/openjpeg_nowasm_fallback.js
	cp /usr/share/javascript/pdfjs-qcms/qcms.js pdfjs/pdf.js/external/qcms/qcms.js
	cp /usr/share/javascript/pdfjs-qcms/qcms_bg.wasm pdfjs/pdf.js/external/qcms/qcms_bg.wasm
	cp /usr/share/javascript/pdfjs-qcms/qcms_utils.js pdfjs/pdf.js/external/qcms/qcms_utils.js
	# 1) pdf.js: build the generic viewer (upstream's pdfjs/build does the same,
	#    but installs the dependencies over the network first).
	cd pdfjs/pdf.js && gulp generic
	# 2) lay the result out as upstream's pdfjs/build does for PDFJS_CONFIG=zotero
	cd $(CURDIR) && mkdir -p build/zotero/pdf/build build/zotero/pdf/web/images
	cp pdfjs/pdf.js/build/generic/LICENSE build/zotero/pdf/
	cp pdfjs/pdf.js/build/generic/build/pdf.mjs build/zotero/pdf/build/
	cp pdfjs/pdf.js/build/generic/build/pdf.worker.mjs build/zotero/pdf/build/
	cp -r pdfjs/pdf.js/build/generic/web/cmaps build/zotero/pdf/web/
	cp -r pdfjs/pdf.js/build/generic/web/standard_fonts build/zotero/pdf/web/
	cp -r pdfjs/pdf.js/build/generic/web/iccs build/zotero/pdf/web/
	cp -r pdfjs/pdf.js/build/generic/web/wasm build/zotero/pdf/web/
	cp pdfjs/pdf.js/build/generic/web/viewer.html build/zotero/pdf/web/
	cp pdfjs/pdf.js/build/generic/web/viewer.mjs build/zotero/pdf/web/
	cp pdfjs/pdf.js/build/generic/web/images/loading-icon.gif build/zotero/pdf/web/images/
	cp pdfjs/viewer.css build/zotero/pdf/web/
	# 3) the reader bundle itself
	webpack --config-name zotero

override_dh_auto_test:

override_dh_auto_install:

override_dh_install:
	dh_install
	# Share the CMap and standard font data with zotero-pdfjs-data instead of
	# shipping a second copy of it.
	rm -rf debian/zotero-reader/usr/share/zotero/resource/reader/pdf/web/cmaps \
	       debian/zotero-reader/usr/share/zotero/resource/reader/pdf/web/standard_fonts
	# The upstream license copies only need to be in the source package; the
	# installed copyright file is debian/copyright.  The ICC profiles come out
	# of pdf.js' build with the executable bit set.
	rm -f debian/zotero-reader/usr/share/zotero/resource/reader/pdf/LICENSE \
	      debian/zotero-reader/usr/share/zotero/resource/reader/pdf/web/iccs/LICENSE
	chmod 644 debian/zotero-reader/usr/share/zotero/resource/reader/pdf/web/iccs/*.icc
	dh_link /usr/share/zotero/resource/pdfjs/bcmaps \
	        /usr/share/zotero/resource/reader/pdf/web/cmaps
	dh_link /usr/share/zotero/resource/pdfjs/standard_fonts \
	        /usr/share/zotero/resource/reader/pdf/web/standard_fonts
	# The MathJax web fonts are only ever fetched by URL from
	# resource/reader/mathjax-fonts/, so take them from node-mathjax-full
	# instead of shipping a second copy of them.
	dh_link /usr/share/nodejs/mathjax-full/ts/output/chtml/fonts/tex-woff-v2 \
	        /usr/share/zotero/resource/reader/mathjax-fonts

override_dh_clean:
	dh_clean
	rm -rf build node_modules pdfjs/pdf.js/node_modules pdfjs/pdf.js/build \
	       pdfjs/pdf.js/external/bcmaps pdfjs/pdf.js/external/standard_fonts \
	       pdfjs/pdf.js/external/quickjs
	# These are symlinks into the pdfjs-* packages, so remove them
	# individually: the LICENSE_* and README.md next to them belong to this
	# source package.
	rm -f pdfjs/pdf.js/external/jbig2/jbig2.js pdfjs/pdf.js/external/jbig2/jbig2.wasm pdfjs/pdf.js/external/jbig2/jbig2_nowasm_fallback.js
	rm -f pdfjs/pdf.js/external/openjpeg/openjpeg.js pdfjs/pdf.js/external/openjpeg/openjpeg.wasm pdfjs/pdf.js/external/openjpeg/openjpeg_nowasm_fallback.js
	rm -f pdfjs/pdf.js/external/qcms/qcms.js pdfjs/pdf.js/external/qcms/qcms_bg.wasm pdfjs/pdf.js/external/qcms/qcms_utils.js
