#!/usr/bin/make -f

# resolve DEB_VERSION
include /usr/share/dpkg/pkg-info.mk

STEM = rdflib
UGLIFY_SOURCES = lib/index.js $(filter-out lib/index.js,$(wildcard lib/*.js))

override_dh_auto_build: debian/js/$(STEM).min.js.gz

# Babel 8 (node-babel7 in unstable) no longer rewrites ES modules to
# CommonJS by default with the upstream .babelrc, so force it explicitly
lib/index.js: src/index.js
	babeljs src -d lib --plugins @babel/plugin-transform-modules-commonjs

debian/js/$(STEM).js: lib/index.js
	mkdir -p debian/js
	cat $(UGLIFY_SOURCES) > debian/js/$(STEM).js

# TODO: check spec when node-arrayify-stream and node-streamify-stream are in Debian
# Upstream 1.26.0 lint configuration requires eslint 8 together with
# eslint-plugin-import and eslint-plugin-jest, not available in Debian yet
# TODO: re-enable `eslint src perf test spec` when they are
# TODO: enable jest tests when node-rdf-isomorphic and @rdfjs/data-model are in Debian
override_dh_auto_test:
#	standard Xqueue
	tape Xqueue/test/*.js
#	mocha
#	rdf-test-suite spec/parser.js http://w3c.github.io/rdf-tests/turtle/manifest.ttl -i '{ \"format\": \"turtle\" }' -c .rdf-test-suite-cache/
#	rdf-test-suite spec/parser.js http://w3c.github.io/rdf-tests/ntriples/manifest.ttl -i '{ \"format\": \"n-triples\" }' -c .rdf-test-suite-cache/
#	rdf-test-suite spec/parser.js http://w3c.github.io/rdf-tests/nquads/manifest.ttl -i '{ \"format\": \"n-quads\" }' -c .rdf-test-suite-cache/
#	rdf-test-suite spec/parser.js http://w3c.github.io/rdf-tests/trig/manifest.ttl -i '{ \"format\": \"trig\" }' -c .rdf-test-suite-cache/

override_dh_gencontrol:
	dh_gencontrol -- \
		-V"queue:VERSION=$(shell jq --raw-output .version < Xqueue/package.json)~$(DEB_VERSION)" \
		-V"types:VERSION=$(shell jq --raw-output .version < Xtypes/package.json)~$(DEB_VERSION)"

# optimize JavaScript for browser use
# * include source-map using upstream intermediary
debian/js/%.min.js: debian/js/%.js
	uglifyjs --compress --mangle --comments \
		--source-map "base='$(abspath $(dir $@))',url='$(notdir $@).map'" \
		--output debian/js/$(STEM).min.js \
		-- $(UGLIFY_SOURCES)

# pre-compress for browser use
%.gz: %
	pigz --force --keep -11 -- $<
	brotli --force --keep --best --suffix=.brotli -- $<

%:
	dh $@

.SECONDARY:
