#!/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

ESLINT = bin/eslint.js --rulesdir lib/internal-rules/ --no-color

# Work around missing node module "ignore"
ESLINT_EXCLUDES += \
 tests/bench/* \
 tests/fixtures/* \
 tests/performance/*

# Work around missing node module "proxyquire"
TEST_EXCLUDES = \
 tests/lib/cli-engine.js \
 tests/lib/cli.js \
 tests/lib/config/config-file.js \
 tests/lib/config/config-initializer.js \
 tests/lib/config/config-ops.js \
 tests/lib/config.js \
 tests/lib/config/plugins.js \
 tests/lib/formatters/codeframe.js \
 tests/lib/formatters/stylish.js \
 tests/lib/util/source-code-util.js

# 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

# Work around missing node module "leche"
TEST_EXCLUDES += \
 tests/lib/cli-engine.js \
 tests/lib/cli.js \
 tests/lib/config/config-file.js \
 tests/lib/config/config-ops.js \
 tests/lib/rule-context.js \
 tests/lib/util/module-resolver.js \
 tests/lib/util/source-code.js

# Work around missing node module "mock-fs"
TEST_EXCLUDES += \
 tests/lib/config.js \
 tests/lib/util/npm-util.js

# Isolate known broken tests for slower isolated checking
TEST_BROKEN += \
 tests/bin/eslint.js \
 tests/lib/config/config-validator.js \
 tests/lib/config-validator.js \
 tests/lib/eslint.js \
 tests/lib/ignored-paths.js \
 tests/lib/rules/array-bracket-spacing.js \
 tests/lib/rules/comma-dangle.js \
 tests/lib/rules/computed-property-spacing.js \
 tests/lib/rules/eol-last.js \
 tests/lib/rules/indent.js \
 tests/lib/rules/keyword-spacing.js \
 tests/lib/rules/newline-after-var.js \
 tests/lib/rules/no-empty-label.js \
 tests/lib/rules/no-new-symbol.js \
 tests/lib/rules/no-redeclare.js \
 tests/lib/rules/no-restricted-imports.js \
 tests/lib/rules/no-restricted-modules.js \
 tests/lib/rules/no-restricted-properties.js \
 tests/lib/rules/no-shadow.js \
 tests/lib/rules/no-unexpected-multiline.js \
 tests/lib/rules/no-unused-vars.js \
 tests/lib/rules/prefer-const.js \
 tests/lib/rules/sort-imports.js \
 tests/lib/rules/sort-keys.js \
 tests/lib/rules/symbol-description.js \
 tests/lib/testers/rule-tester.js \
 tests/lib/traverse.js \
 tests/lib/util/glob-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)))
	$(ESLINT) Makefile.js
#	find conf -type f -name '*.json' \
#		-exec jsonlint {} +
#	find docs -type f -name '*.md' \
#		-exec lintMarkdown {} +
	$(strip find lib conf bin -type f -name '*.js' \
		$(patsubst %,-not -path '%',\
			$(sort $(ESLINT_EXCLUDES))) \
		-exec $(ESLINT) {} +)
	$(strip find tests -type f -name '*.js' \
		$(patsubst %,-not -path '%',\
			$(sort $(ESLINT_EXCLUDES))) \
		-exec $(ESLINT) {} +)
	$(strip find tests -type f -name '*.js' \
		$(patsubst %,-not -path '%',\
			$(sort $(ESLINT_EXCLUDES) \
				$(TEST_EXCLUDES) \
				$(TEST_BROKEN))) \
		-exec mocha --reporter dot --no-colors {} +)
	$(strip NODE_ENV=test prove --verbose \
		--exec "mocha --reporter=tap" \
		$(sort $(TEST_BROKEN)) \
		$(if $(DEB_SUITE_EXP),|| true))
endif

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