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

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

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

JESTDIR=$(CURDIR)/debian/jest
HELP2MAN=help2man --no-discard-stderr --no-info

%:
	dh $@

override_dh_auto_build:
	dh_auto_build --buildsystem=nodejs
	NODE_PATH=debian/build_modules node ./scripts/build.mjs
	# Use chalk@4 types (copied in debian/build_modules/@types/chalk)
	perl -i -ne 'print unless /"types"/' node_modules/chalk/package.json
	find node_modules/chalk/ -name '*.d.ts' -delete
	rm -f node_modules/@types/jest
	node ./scripts/buildTs.mjs || node ./scripts/buildTs.mjs
	# jest-snapshot-serializer-raw needs jest to be built
	cd ./jest-snapshot-serializer-raw && sh -ex ../debian/nodejs/jest-snapshot-serializer-raw/_build
	$(HELP2MAN) packages/jest/bin/jest.js --name 'Delightful JavaScript Testing' >jest.1
	$(HELP2MAN) packages/jest-repl/bin/jest-repl.js --name 'Jest cli usable in a sandboxed Jest environment' >jest-repl.1
	$(HELP2MAN) packages/jest-repl/bin/jest-runtime-cli.js --name 'Jest cli usable in a sandboxed Jest environment' >jest-runtime-cli.1

override_dh_install:
	dh_install
	# Remove separated package files from main one (jest)
	if [ -e $(JESTDIR) ]; then \
		cd $(JESTDIR) && rm -rf `cat ../../debian/node-*.install|sed 's/.* //g'`; \
		rmdir usr/share/nodejs/@bcoe; \
	fi

override_dh_installdocs:
	dh_installdocs
	# Auto-generate components docs
	dh_nodejs_autodocs auto_dispatch

override_dh_installexamples:
	dh_installexamples
	if [ -e $(JESTDIR) ]; then \
		find $(JESTDIR) -name .gitignore -delete; \
	fi

override_dh_auto_clean:
	dh_auto_clean --buildsystem=nodejs
	find . -name build|grep -v debian |xargs rm -rf

override_dh_fixperms:
	dh_fixperms
	if [ -e $(CURDIR)/debian/node-jest-debbundle ]; then \
		chmod +x $(CURDIR)/debian/node-jest-debbundle/usr/share/nodejs/import-local/fixtures/cli.js; \
		chmod +x $(CURDIR)/debian/node-jest-debbundle/usr/share/nodejs/is-ci/bin.js; \
	fi
	if [ -e $(JESTDIR) ]; then \
		chmod +x $(JESTDIR)/usr/share/nodejs/jest/bin/jest.js; \
		chmod +x $(JESTDIR)/usr/share/nodejs/jest-cli/bin/jest.js; \
		chmod +x $(JESTDIR)/usr/share/nodejs/jest-repl/bin/jest-repl.js; \
		chmod +x $(JESTDIR)/usr/share/nodejs/jest-repl/build/cli/index.js; \
		chmod +x $(JESTDIR)/usr/share/nodejs/jest-repl/bin/jest-runtime-cli.js; \
	fi
	find $(JESTDIR) -name '*.ts' -exec chmod -x '{}' \;
