#!/usr/bin/make -f

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

# generate documentation unless nodoc requested
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
DOCS = README.html README.txt
CHANGELOGS = CHANGELOG.html CHANGELOG.txt
endif

ESLINT = eslint
MOCHA = mocha --no-timeout
# normalize output with TAP where possible unless terse requested
ifeq (,$(filter terse,$(DEB_BUILD_OPTIONS)))
ESLINT += --format tap
MOCHA += --reporter tap
else
ESLINT += --format unix
MOCHA += --reporter dot --no-colors
endif

# Work around missing node module "typescript-eslint-parser"
TEST_EXCLUDES += tests/typescript.js

%:
	dh $@

%.html: %.md
	cmark-gfm $< > $@

%.txt: %.md
	cmark-gfm --to plaintext $< > $@

override_dh_clean:
	dh_clean -- $(DOCS) $(CHANGELOGS)

override_dh_auto_build:
	rollup -c

execute_after_dh_auto_build: $(DOCS) $(CHANGELOGS)

# TODO: check full testsuite
override_dh_auto_test:
	$(ESLINT) --no-eslintrc \
		--parser-options 'sourceType: "module", ecmaVersion: "2020"' \
		--ignore-pattern tests/get-declared-variables.js \
		'lib/**/*.js' 'tests/*.js' 'tests/*.cjs'
	$(MOCHA) 'tests/*.cjs'
#	$(MOCHA) $(patsubst %,--exclude %,$(TEST_EXCLUDES)) 'tests/*.js'

override_dh_installdocs:
	dh_installdocs --all -- $(DOCS)

override_dh_installchangelogs:
	dh_installchangelogs -- $(CHANGELOGS)

execute_after_dh_fixperms:
	find debian/*/usr/share/nodejs/@types -type f -exec chmod -x '{}' +

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