#!/usr/bin/make -f

# output every command that modifies files on the build system.
export DH_VERBOSE = 1

export HOME=$(CURDIR)/debian/fakehomeyarn
export PATH := $(PATH):$(CURDIR)/debian/bin
export NODE_OPTIONS=--openssl-legacy-provider

include /usr/share/dpkg/architecture.mk

ifeq ($(DEB_HOST_ARCH),amd64)
	ARCH_VSCODE := x64
else ifeq ($(DEB_HOST_ARCH),arm64)
	ARCH_VSCODE := arm64
else ifeq ($(DEB_HOST_ARCH),armhf)
	ARCH_VSCODE := armhf
else
	$(error 'unsupported architecture')
endif


%:
	dh $@

override_dh_auto_build:
	mkdir debian/bin
	ln -s /usr/bin/yarnpkg debian/bin/yarn
	# install node-gyp with yarnpkg
	# if we use the Debian package node-gyp, the build is OK but the binary
	# file code-oss had issues: it uses too much CPU and the process doesn't
	# stop when you close the editor windows.
	yarnpkg global add node-gyp

	CHILD_CONCURRENCY=1 yarnpkg
	# Add max_old_space_size to fix "FATAL ERROR: Ineffective
	# mark-compacts near heap limit Allocation failed - JavaScript
	# heap out of memory" on armhf. Initial value was 8192
	node --max_old_space_size=10240 ./node_modules/gulp/bin/gulp.js compile-build
	node --max_old_space_size=10240 ./node_modules/gulp/bin/gulp.js compile-extensions-build
	yarnpkg gulp minify-vscode

	./debian/undo-telemetry.sh
	
	node --max_old_space_size=10240 ./node_modules/gulp/bin/gulp.js "vscode-linux-$(ARCH_VSCODE)-min-ci"
	node --max_old_space_size=10240 ./node_modules/gulp/bin/gulp.js "vscode-linux-$(ARCH_VSCODE)-build-deb"

override_dh_shlibdeps:
	# dpkg-shlibdeps: error fails because missing libraries.
	# several compiled parts are downloaded during the build
	dpkg-shlibdeps -e debian/code-oss/usr/lib/code-oss/bin/code-oss

override_dh_strip:
ifeq ($(DEB_HOST_ARCH),arm64)
	dh_strip -Xkeytar.node
else
	dh_strip
endif
