#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

%:
	dh $@

override_dh_auto_configure:
	mkdir -p node_modules/@types
	set -e; for i in chai jquery jsdom mocha node; do \
		ln -sf /usr/lib/nodejs/@types/$$i node_modules/@types/$$i; done

COPY_AUX_FILES = \
	cp -a $(1)/addons $(2) && \
	cd $(1) && find -name *.css -exec cp --parents '{}' ../$(2) \; \

override_dh_auto_build:
	tsc --project .
	$(call COPY_AUX_FILES,src,lib)
# Otherwise browserify-lite complains
	touch lib/addons/index.js
	mkdir -p dist
	cd dist && browserify-lite ../lib/xterm.js --outfile xterm.js --standalone Terminal
	$(call COPY_AUX_FILES,lib,dist)
# Undo browserify-lite workarounds
	sed -i -e 's/REQUIRE_NO_BROWSERIFY/require/g' dist/xterm.js
	rm -f dist/addons/index.js

override_dh_auto_test:
# These tests depend on jsdom which can't (yet) be installed in Debian
# They pass when run locally though
	rm -f lib/Linkifier.test.js lib/utils/CharMeasure.test.js
	mocha lib/

# Upstream does not have tests for the browserified version, but you can test
# that manually by running this:
run-demo:
	test -e build || ln -sf dist build
	python -m SimpleHTTPServer & x=$$!; \
	chromium --temp-profile 'http://localhost:8000/demo/index.html'; kill $$!

override_dh_auto_clean:
	dh_auto_clean
	rm -rf node_modules lib dist build

override_dh_link:
# Deduplicate, link via dh_link
	rm -rf debian/node-xterm/usr/lib/nodejs/xterm/lib/addons
	dh_link
