#!/usr/bin/make -f

# Debian's packages of the two dependencies that the full bundle includes
GRAPHLIB := $(shell node -p 'require.resolve("graphlib")')
LODASH := $(shell node -p 'require.resolve("lodash")')

%:
	dh $@

override_dh_auto_build:
	# The DOT parser, generated from its PEG.js grammar as upstream's Makefile
	# does (upstream used PEG.js 0.8, Debian has 0.10: same output shape)
	pegjs --allowed-start-rules start,graphStmt --format commonjs \
	  -o lib/dot-grammar.js src/dot-grammar.pegjs
	# Browser bundles, as upstream's Makefile: the full one includes graphlib
	# and lodash, the core one expects them as globals. lib/graphlib.js and
	# lib/lodash.js require("graphlib") and require("lodash"), which
	# Debian's browserify resolves to those two files themselves, so the
	# full bundle names the real modules with -r.
	mkdir -p dist
	browserify -r $(GRAPHLIB):graphlib -r $(LODASH):lodash browser.js \
	  > dist/graphlib-dot.js
	browserify browser.js --no-bundle-external > dist/graphlib-dot.core.js
	uglifyjs dist/graphlib-dot.js --comments '@license' -o dist/graphlib-dot.min.js
	uglifyjs dist/graphlib-dot.core.js --comments '@license' -o dist/graphlib-dot.core.min.js

# graphlib-dot.js and graphlib-dot.min.js include the code of these two
# packages; Static-Built-Using names their source packages. It is passed to
# dpkg-gencontrol rather than written in debian/control, whose parser in
# pkg-js-tools (Debian::Control) rejects that field.
override_dh_gencontrol:
	dh_gencontrol -- "-DStatic-Built-Using=$$(for p in node-graphlibrary node-lodash; do \
	  dpkg-query -W -f '$${source:Package} (= $${source:Version}), ' $$p; done | sed 's/, $$//')"
