#!/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

lib/index.js: src/index.js
	babeljs src -d lib

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

# TODO: enable when node-arrayify-stream and node-streamify-stream is in Debian
override_dh_auto_test:
	tape Xqueue/test/*.js
#	mocha

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:
