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

# Avoid use of node module proxyquire in testsuite
TEST_EXCLUDES = tests/lib/cli.js tests/lib/config.js \
 tests/lib/cli-engine.js tests/lib/formatters/stylish.js

# Avoid use of node module cheerio in testsuite
TEST_EXCLUDES += tests/lib/formatters/html.js

# Avoid use of node module espree in testsuite
TEST_EXCLUDES += tests/lib/util/source-code.js

# Avoid use of node module proxyquire in testsuite
TEST_EXCLUDES += tests/lib/testers/rule-tester.js

JS_FILES = $(shell find lib -type f -name '*.js')
JSON_FILES = $(shell find conf -type f -name '*.json') .eslintrc
MARKDOWN_FILES = $(shell find docs -type f -name '*.md')
TEST_FILES = $(shell find tests/lib -type f -name '*.js' \
 $(patsubst %,-not -path '%',$(TEST_EXCLUDES)))

# 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)))
	bin/eslint.js Makefile.js
#	jsonlint -q -c $(JSON_FILES)
#	lintMarkdown $(MARKDOWN_FILES)
	bin/eslint.js --no-color $(JS_FILES)
	bin/eslint.js --no-color $(TEST_FILES)
	mocha --reporter dot --no-colors $(TEST_FILES) \
		$(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 $@
