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

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

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

GULP=/usr/bin/gulp --gulpfile Gulpfile.mjs --cwd .
BABELJSDESC="The compiler for writing next generation JavaScript"
BABELJSHELPERSDESC="Build an external Babel helpers file"
BABELJSNODEDESC="CLI that works exactly the same as the Node.js CLI, with the added benefit of compiling with Babel presets and plugins before running it"
BABEL=/usr/bin/babeljs
# pirates >= 4.0.7 ships its sources directly in lib/, nothing to build there
OTHER_BABEL_COMPONENTS_TO_BUILD=babel-check-duplicated-nodes babel-plugin-dynamic-import-node babel-preset-modules
OTHER_BABEL_MODULES_TO_BUILD:=$(shell for m in $(OTHER_BABEL_COMPONENTS_TO_BUILD); do pkgjs-pjson $$m;done)

# Babel 8 build environment, see "prepublish-build" in Makefile.source.ts
BUILD_ENV=NODE_ENV=production BABEL_ENV=production STRIP_BABEL_VERSION_FLAG=true IS_PUBLISH=true

# The build is bootstrapped with whatever @babel/core is installed: when that
# is still Babel 7, its parser cannot read the Babel 8 sources and a few
# type-level rewrites are needed (see debian/bootstrap-babel7.sh).
BABEL_MAJOR:=$(shell pkgjs-pjson /usr/share/nodejs/@babel/core version 2>/dev/null | cut -d. -f1)
ifeq ($(BABEL_MAJOR),7)
BOOTSTRAP_SRC=sh debian/bootstrap-babel7.sh
BOOTSTRAP_RESTORE=sh debian/bootstrap-babel7-restore.sh
else
BOOTSTRAP_SRC=:
BOOTSTRAP_RESTORE=:
endif

%:
	dh $@

override_dh_auto_configure:
	# Don't link internal modules
	pkgjs-install-minimal
	pkgjs-utils link_external_modules
	pkgjs-utils link_build_modules
	ln -sfn ../scripts/repo-utils 'node_modules/$$repo-utils'
	debian/rules private-build-modules

override_dh_auto_build:
	@echo
	@echo "Step 0: other builds"
	pkgjs-utils clean_internal_modules
	mv node_modules node_modules.save
	debian/rules other-babel-modules
	mv node_modules.save node_modules

	@echo
	@echo "Step 1: Link /usr/share/nodejs/@babel/* build dependencies into node_modules"
	LIST=`pkgjs-utils modules_list` && \
		for m in $$LIST `pkgjs-utils main`; do \
			if [ -e /usr/share/nodejs/$$m ]; then \
				rm -f node_modules/$$m; \
				ln -vs /usr/share/nodejs/$$m node_modules/$$m || true; \
			fi; \
		done
	ln -s ../../packages/babel-helper-string-parser node_modules/@babel/helper-string-parser || true

	@echo
	@echo "Step 2: build charcodes"
	cd charcodes; \
	for D in ./packages/*; do \
		mkdir -p "$$D/lib"; \
		$(BABEL) "$$D/src/index.js" -o "$$D/lib/index.js" --env-name "cjs"; \
		$(BABEL) "$$D/src/index.js" -o "$$D/lib/index.mjs" --env-name "esm"; \
	done

	@echo
	@echo "Step 3: compile the build tooling"
	# Debian's nodejs is built without TypeScript type stripping
	sh debian/build-tooling.sh
	esbuild babel.config.ts --bundle --platform=node --format=cjs \
		--outfile=babel.config.cjs --external:@babel/core \
		--define:import.meta.url=__babelConfigURL \
		--banner:js='const __babelConfigURL = require("node:url").pathToFileURL(__filename).href;' \
		--log-level=warning

	@echo
	@echo "Step 4: build babel"
	node scripts/generators/tsconfig.mjs
	node scripts/generators/archived-libs-typings.mjs
	node scripts/parallel-tsc/tsc.mjs ./packages/babel-parser
	$(GULP) bundle-babel-parser-dts
	# the rewrites below must come after tsc: they hide type-only re-exports
	$(BOOTSTRAP_SRC)
	$(BUILD_ENV) $(GULP) build-rollup build-babel
	# the @babel/traverse generators read @babel/types: they must see the one
	# that has just been built, not the Babel 7 from the archive
	ln -sfn ../../packages/babel-types node_modules/@babel/types
	$(BUILD_ENV) $(GULP) generate-type-helpers generate-runtime-helpers \
		generate-helper-globals-data
	rm -f node_modules/@babel/types
	ln -s /usr/share/nodejs/@babel/types node_modules/@babel/types
	# the generators re-emit sources that the bootstrap has to fix up again
	$(BOOTSTRAP_SRC)
	$(BUILD_ENV) $(GULP) build-rollup build-babel
	$(BUILD_ENV) $(GULP) generate-standalone
	sh debian/fix-import-attributes.sh
	$(BOOTSTRAP_RESTORE)

	@echo
	@echo "Step 5: switch to the freshly built babel"
	# the declarations were emitted from the rewritten sources: drop them and
	# the incremental state, so that step 6 rebuilds them from the real ones
	rm -rf dts
	find . -name tsconfig.tsbuildinfo -not -path './node_modules/*' -delete
	LIST=`pkgjs-utils modules_list;pkgjs-utils main` && (cd node_modules && rm -rf $$LIST)
	pkgjs-utils link_internal_modules
	ln -sfn ../../packages/babel-core node_modules/@babel/core
	set -e; for m in `cat debian/nodejs/extcopies`; do \
		d=`dirname $$m`; \
		[ "$$d" = "." ] || mkdir -p node_modules/$$d; \
		cp -rL /usr/share/nodejs/$$m node_modules/$$m; \
	done
	debian/rules private-build-modules
	cd packages/babel-plugin-transform-runtime && node scripts/build-dist.mjs
	node packages/babel-types/scripts/generators/flow.mjs \
		> packages/babel-types/lib/index.js.flow

	@echo
	@echo "Step 6: build typescript declarations"
	node scripts/parallel-tsc/tsc.mjs .
	tsc -p ./packages/babel-helpers/src/helpers/tsconfig.json
	$(GULP) bundle-dts
	node packages/babel-types/scripts/generators/typescript-legacy.mjs \
		> packages/babel-types/lib/index-legacy.d.ts
	tsc -p tsconfig.dts-bundles.json

	@echo
	@echo "Step 7: build @babel/standalone"
	# "build-babel-standalone" is a gulp.series that gulp 4 does not run, and
	# "generate-standalone" already ran above
	$(BUILD_ENV) $(GULP) rollup-babel-standalone
	cd packages/babel-standalone && \
	terser -o babel.min.js babel.js

	help2man --no-info \
		--version-string $(DEB_VERSION_UPSTREAM) \
		-n $(BABELJSDESC) \
		packages/babel-cli/bin/babel.js > babeljs.1
	help2man --no-info \
		--version-string $(DEB_VERSION_UPSTREAM) \
		-n $(BABELJSNODEDESC) \
		packages/babel-node/bin/babel-node.js > babeljs-node.1
	help2man --no-info \
		--version-string $(DEB_VERSION_UPSTREAM) \
		-n $(BABELJSHELPERSDESC) \
		packages/babel-build-external-helpers/bin/babel-build-external-helpers.js \
		> babeljs-external-helpers.1

# rollup-plugin-dts cannot digest the getter namespaces rollup emits, so use a
# private, patched copy of it. @rollup/plugin-babel needs a private copy too:
# as a symlink it would always load the @babel/core of the archive, while the
# last steps have to run on the babel that has just been built.
private-build-modules:
	set -e; for m in rollup-plugin-dts @rollup/plugin-babel; do \
		rm -rf node_modules/$$m; \
		mkdir -p `dirname node_modules/$$m`; \
		cp -rL /usr/share/nodejs/$$m node_modules/$$m; \
		chmod -R u+w node_modules/$$m; \
	done
	patch -p1 -d node_modules/rollup-plugin-dts \
		< debian/rollup-plugin-dts-getters.diff

other-babel-modules:
	set -e; for m  in $(OTHER_BABEL_COMPONENTS_TO_BUILD); do \
			(echo "Building $$m"; \
				cd $$m; \
				rm -rf lib; \
				$(BABEL) --out-dir lib src --ignore '**/*.test.js'; \
			); \
			ls $$m/lib/*.js; \
		done

override_dh_auto_clean:
	dh_auto_clean --buildsystem=nodejs
	[ ! -f debian/tooling.list ] || xargs -r rm -f < debian/tooling.list
	rm -f debian/tooling.list debian/json-imports.list
	rm -f Gulpfile.mjs babel-worker.mjs babel.config.cjs

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
override_dh_auto_test:
endif

override_dh_install:
	dh_install
	find debian -name .npmignore -delete
	find debian -name .gitignore -delete
	find debian -name tsconfig.tsbuildinfo -delete
	cd debian/node-babel7 && rm -rf `cat ../../debian/node-babel7-*.install|sed 's/.* //g'`
	perl -i -pe 's#(?:\.\./)+usr/#/usr/#' `find debian/*/usr/share/nodejs -name '*.map'`
	# Move source-map files into node-babel7-debug
	dh_nodejs_build_debug_package node-babel7-debug

override_dh_installdocs:
	dh_installdocs
	# Auto-generate components docs
	dh_nodejs_autodocs auto_dispatch
	mv debian/node-babel7-debug/usr/share/doc/node-babel7-debug debian/node-babel7-debug/usr/share/
	rm -rf debian/node-babel7-debug/usr/share/doc/*
	mv debian/node-babel7-debug/usr/share/node-babel7-debug debian/node-babel7-debug/usr/share/doc/

override_dh_fixperms:
	dh_fixperms
	chmod +x debian/node-babel7/usr/share/nodejs/@babel/cli/bin/babel.js \
		debian/node-babel7/usr/share/nodejs/@babel/build-external-helpers/bin/babel-build-external-helpers.js \
		debian/node-babel7/usr/share/nodejs/@babel/node/bin/babel-node.js \
		debian/node-babel7/usr/share/nodejs/@babel/parser/bin/babel-parser.js
