#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

# detect if build targets experimental suite (or is a draft)
DEB_SUITE_EXP = $(filter experimental% UNRELEASED,$(DEB_DISTRIBUTION))

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

ESLINT = eslint --format tap
MOCHA = mocha --reporter tap

ESLINT_BROKEN = \
 lib/utils/rjs.js \
 lib/utils/ast.js \
 lib/rules/amd-function-arity.js \
 lib/rules/sort-amd-paths.js

MOCHA_BROKEN = \
 tests/lib/rules/enforce-define.js

override_dh_auto_build: $(DOCS)

override_dh_auto_test:
	$(strip find lib tests/lib -type f -name '*.js' \
		$(patsubst %,-not -path '%',$(ESLINT_BROKEN)) \
		-exec $(ESLINT) {} +)
	$(ESLINT) $(ESLINT_BROKEN) | debian/tap-todo \
		'lib/utils/rjs.js::Arrow function used ambiguously with a conditional expression.*line: 82.*ruleId: no-confusing-arrow' \
		"lib/utils/ast.js::Assignment to function parameter 'node'.*line: 151.*ruleId: no-param-reassign" \
		'lib/rules/amd-function-arity.js::Arrow function used ambiguously with a conditional expression.*line: 82.*ruleId: no-confusing-arrow' \
		'lib/rules/sort-amd-paths.js::Arrow function used ambiguously with a conditional expression.*line: 74.*ruleId: no-confusing-arrow'
	$(strip find tests/lib/*/*.js \
		$(patsubst %,-not -path '%',$(MOCHA_BROKEN)) \
		-exec $(MOCHA) '{}' +)
	$(MOCHA) $(MOCHA_BROKEN) | debian/tap-todo \
		'^enforce-define invalid\z::Error: ESLint configuration in rule-tester is invalid:.*Unexpected top-level property "args"'

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

override_dh_clean:
	dh_clean -- $(DOCS)

%.html: %.md
	pandoc --from gfm-raw_html --to html --standalone --output $@ $<

%.txt: %.md
	pandoc --from gfm-raw_html --to plain --output $@ $<

%:
	dh $@
