#!/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
CHANGELOGS = CHANGELOG.html CHANGELOG.txt
MANPAGES = debian-man/eslint.1
endif

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

# same paths as "Makefile.js" TEST_FILES
TEST_FILES_MOCHA = $(strip \
 $(addsuffix /**/*.js,\
  $(shell find tests/lib -mindepth 1 -maxdepth 1 -type d) \
  tests/bin) \
 $(addsuffix /*.js,\
  tests/lib))
TEST_FILES_FIND = $(patsubst %/**/*.js,%,$(TEST_FILES_MOCHA))

# Work around missing node module "cheerio"
TEST_EXCLUDES += tests/lib/formatters/html.js

# Work around missing node module "table"
TEST_EXCLUDES += tests/lib/formatters/table.js

# Isolate known broken tests for slower isolated checking
TEST_BROKEN += \
 tests/bin/eslint.js \
 tests/lib/cli.js \
 tests/lib/cli-engine.js \
 tests/lib/config/config-validator.js \
 tests/lib/ignored-paths.js \
 tests/lib/rules/comma-dangle.js \
 tests/lib/rules/generator-star-spacing.js \
 tests/lib/rules/key-spacing.js \
 tests/lib/rules/keyword-spacing.js \
 tests/lib/rules/no-dupe-keys.js \
 tests/lib/rules/no-empty-label.js \
 tests/lib/rules/no-extra-parens.js \
 tests/lib/rules/no-new-symbol.js \
 tests/lib/rules/no-param-reassign.js \
 tests/lib/rules/no-self-assign.js \
 tests/lib/rules/no-undef.js \
 tests/lib/rules/no-unused-vars.js \
 tests/lib/rules/no-useless-computed-key.js \
 tests/lib/rules/no-useless-rename.js \
 tests/lib/rules/object-property-newline.js \
 tests/lib/rules/object-shorthand.js \
 tests/lib/rules/prefer-const.js \
 tests/lib/rules/quote-props.js \
 tests/lib/rules/rest-spread-spacing.js \
 tests/lib/rules/sort-keys.js \
 tests/lib/testers/rule-tester.js \
 tests/lib/traverse.js \
 tests/lib/util/glob-util.js \
 tests/lib/util/source-code-util.js

# generate manpage with help2man from --help option of Node.js script
_mkman = NODE_PATH=lib \
 help2man $(patsubst %,--name %,$3) --no-info --output $2 $1 \
 || { NODE_PATH=lib $1 --help; false; }

override_dh_auto_build: $(DOCS) $(CHANGELOGS) $(MANPAGES)

# strip extension from executable
debian-man/eslint:
	mkdir --parents $(dir $@)
	cp --force bin/eslint.js $@

# build manpage
debian-man/eslint.1: %.1: %
	$(call _mkman, $<, $@, \
		AST-based pattern checker and linting utility for JavaScript)

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
#	NODE_PATH=packages $(ESLINT) --report-unused-disable-directives Makefile.js
#	find conf -type f -name '*.json' \
#		-exec jsonlint {} +
#	find docs -type f -name '*.md' \
#		-exec lintMarkdown {} +
	NODE_PATH=packages $(strip find lib conf bin -type f -name '*.js' \
		-exec $(ESLINT) --report-unused-disable-directives {} + \
		$(if $(DEB_SUITE_EXP),|| true))
	NODE_PATH=packages $(strip find $(TEST_FILES_FIND) -type f -name '*.js' \
		-exec $(ESLINT) --report-unused-disable-directives {} + \
		$(if $(DEB_SUITE_EXP),|| true))
	NODE_PATH=packages $(strip find $(TEST_FILES_FIND) -type f -name '*.js' \
		$(patsubst %,-not -path '%',$(sort $(TEST_EXCLUDES) $(TEST_BROKEN))) \
		-exec $(MOCHA) {} +)
	NODE_PATH=packages $(strip $(MOCHA) $(sort $(TEST_BROKEN)) \
		$(if $(DEB_SUITE_EXP),|| true))
endif

# avoid installing stray config files
override_dh_install:
	dh_install -X.eslintrc.yml

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

override_dh_installchangelogs:
	dh_installchangelogs -- $(CHANGELOGS)

# unset bogus executable bits
override_dh_fixperms:
	dh_fixperms
	chmod --recursive a-x,a+X -- debian/*/usr/share/nodejs
	chmod --recursive a+x -- debian/*/usr/share/nodejs/eslint/bin/*

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

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

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

%:
	dh $@
