#!/usr/bin/make -f
# vim: set noet ft=make
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

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

BIN:=$(shell ls tools/)
MANPAGES:=$(BIN:=.1)
MAN7PAGES:=$(shell ls doc/man/*.7)
REGEXPSOURCEFILES:=$(shell ls dh_nodejs/*)
export NPMIGNOREDEFAULT=$(shell pwd)/dh_nodejs/npmignore.default
GENERATEDFILES=lib/Debian/Debhelper/Buildsystem/nodejs.pm \
	lib/Debian/PkgJs/Banned.pm \
	lib/Debian/PkgJs/PackageLock.pm \
	autopkgtest/runner

%:
	dh $@

%.1:
	PERL5LIB=lib help2man --no-discard-stderr -n $* -N --help-option=-h ./tools/$* > $*.1

%.7: doc/*/README.md
	S=$@; S=$${S%.7}; S=doc/$${S#doc/man/pkg-js-}/README.md; \
	if which marked-man; then \
		marked-man $$S > $@; \
	fi

override_dh_auto_configure: $(GENERATEDFILES) lib/Debian/PkgJs/Version.pm
	dh_auto_configure

$(GENERATEDFILES): $(REGEXPSOURCEFILES)
	./dh_nodejs/mk_regexp $(GENERATEDFILES)

lib/Debian/PkgJs/Version.pm: debian/changelog
	perl -i -pe 's/[\d\.]+/'$(DEB_VERSION)'/' lib/Debian/PkgJs/Version.pm

override_dh_auto_install: lib/Debian/Debhelper/Buildsystem/nodejs.pm $(MANPAGES) $(MAN7PAGES)
	dh_auto_install

override_dh_installman:
	rm -f *.1p
	dh_installman
	# Remove duplicates
	for f in debian/pkg-js-tools/usr/share/man/man1/*.1p; do \
		rm -f $${f%p}; \
	done

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	dh_auto_test
	PERL5LIB=`pwd`/lib PATH=`pwd`/tools:$$PATH debian/tests/pkg-js-autopkgtest
endif
