#!/usr/bin/make -f

# 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 --no-timeout

# TODO: drop when node-eslint-config-mysticatea is in Debian
ESLINT += --no-eslintrc --env es6,node

TEST_BROKEN += \
 tests/lib/rules/no-deprecated-api.js \
 tests/lib/rules/no-missing-import.js \
 tests/lib/rules/no-missing-require.js \
 tests/lib/rules/no-unsupported-features.js

override_dh_auto_build: $(DOCS)
	node scripts/update.js
# FIXME: drop when tests are unbroken
	sed -i -e '/no-missing-\(import\|require\)/d' \
		lib/recommended.json \
		lib/rules.js

override_dh_auto_test:
	$(ESLINT) lib tests/lib
	$(strip find tests/lib -type f -name '*.js' \
		$(patsubst %,-not -path '%',$(sort $(TEST_BROKEN))) \
		-exec $(MOCHA) {} +)
	$(strip $(MOCHA) tests/lib/rules/no-deprecated-api.js \
		| perl -p \
		-e '/^not ok \d+ no-deprecated-api invalid root;/ and $$todo++;' \
		-E '/^not ok / and $$bad++; END{say "# TODO $$todo/1"; exit ($$bad - $$todo)}')
	$(strip $(MOCHA) tests/lib/rules/no-missing-import.js \
		| perl -p \
		-e '/^not ok \d+ no-missing-import valid import eslint / and $$todo++;' \
		-e '/^not ok \d+ no-missing-import valid import mocha / and $$todo++;' \
		-E '/^not ok / and $$bad++; END{say "# TODO $$todo/5"; exit ($$bad - $$todo)}')
	$(strip $(MOCHA) tests/lib/rules/no-missing-require.js \
		| perl -p \
		-e '/^not ok \d+ no-missing-require valid require\(.eslint\b.*\)/ and $$todo++;' \
		-e '/^not ok \d+ no-missing-require valid require\(.mocha\b.*\)/ and $$todo++;' \
		-E '/^not ok / and $$bad++; END{say "# TODO $$todo/6"; exit ($$bad - $$todo)}')
	$(strip $(MOCHA) tests/lib/rules/no-unsupported-features.js \
		| perl -p \
		-e 'm|^not ok \d+ no-unsupported-features invalid \Q/*Classes in non-strict mode: 4.0.0*/ class A {} new (class{})()\E| and $$todo++;' \
		-e 'm|^not ok \d+ no-unsupported-features invalid \Q/*Classes in non-strict mode: 5.0.0*/ class A {} new (class{})()\E| and $$todo++;' \
		-E '/^not ok / and $$bad++; END{say "# TODO $$todo/2"; exit ($$bad - $$todo)}')

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 $@
