#!/usr/bin/make -f
# -*- makefile -*-

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

export HOME=/tmp
ARBORIST_VERSION=$(shell pkgjs-pjson node_modules/@npmcli/arborist version)
NPM_PACKLIST_VERSION=$(shell pkgjs-pjson node_modules/npm-packlist version)
PACOTE_VERSION=$(shell pkgjs-pjson node_modules/pacote version)
QRT_VERSION=$(shell pkgjs-pjson node_modules/qrcode-terminal version)

%:
	dh $@ --with bash-completion

override_dh_auto_build:
ifeq (,$(filter nodoc,$(DEB_BUILD_PROFILES)))
	prefix=/usr make mandocs
	help2man --no-discard-stderr -n "the npm tree doctor" -N \
			--version-string $(ARBORIST_VERSION) \
			node_modules/@npmcli/arborist/bin/index.js \
		>man/man1/npm-arborist.1
	node_modules/qrcode-terminal/bin/qrcode-terminal.js --help
	help2man -N -n 'The JavaScript Package Handler' --version-string $(PACOTE_VERSION) \
		node_modules/pacote/lib/bin.js >man/man1/pacote.1
	help2man -N -n 'List files to embed in npm package' --version-string $(NPM_PACKLIST_VERSION) \
		node_modules/npm-packlist/bin/index.js >man/man1/npm-packlist.1
endif

override_dh_fixperms:
	dh_fixperms
	chmod a-x debian/npm/usr/share/nodejs/npm/lib/utils/completion.sh
	chmod a+x debian/npm/usr/share/nodejs/npm/bin/npm-cli.js
	chmod a+x debian/npm/usr/share/nodejs/npm/bin/npx-cli.js
	chmod a+x debian/npm/usr/share/nodejs/@npmcli/arborist/bin/index.js
	chmod a+x debian/npm/usr/share/nodejs/npm-packlist/bin/index.js
	chmod a+x debian/npm/usr/share/nodejs/pacote/lib/bin.js
	chmod a+x debian/npm/usr/share/nodejs/qrcode-terminal/bin/qrcode-terminal.js

override_dh_auto_test:
	# Test are lauched only during autopkgtest

override_dh_auto_install:
	dh_auto_install --buildsystem=nodejs
	rm -vf debian/npm/usr/share/nodejs/npm/bin/node-gyp-bin/node-gyp.cmd \
		debian/npm/usr/share/nodejs/npm/bin/*.cmd \
		debian/npm/usr/share/nodejs/npm/node_modules/@npmcli/run-script/lib/node-gyp-bin/node-gyp.cmd

override_dh_installdocs:
	dh_installdocs
	dh_nodejs_autodocs

override_dh_link:
	rm -rf debian/npm/usr/share/nodejs/npm/man
	dh_link

override_dh_gencontrol:
	dh_gencontrol -- -Vpacote:Version="$(PACOTE_VERSION)" \
			-Vqrt:Version="$(QRT_VERSION)" \
			-Vnpmpl:Version="$(NPM_PACKLIST_VERSION)" \
			-Varborist:Version="$(ARBORIST_VERSION)"
