#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

export DH_GOLANG_INSTALL_EXTRA := internal/bundler/snapshots
export NPM_DIR:=$(shell perl -e "\$$a={amd64=>'64',i386=>'32',arm64=>'arm64',armel=>'arm',mips64el=>'mips64le',ppc64el=>'ppc64le'};print defined \$$a->{$(DEB_TARGET_ARCH)} ? 'esbuild-linux-'.\$$a->{$(DEB_TARGET_ARCH)} : ''")

%:
	dh $@ --builddirectory=_build --buildsystem=golang

override_dh_auto_build:
	dh_auto_build --builddirectory=_build --buildsystem=golang
	ln -s _build/bin/esbuild .
	node scripts/esbuild.js ./esbuild --neutral
	set -e; if test "$(NPM_DIR)" != ""; then \
		HOME=/tmp $(MAKE) --no-print-directory GOOS=linux GOARCH=amd64 NPMDIR=npm/$(NPM_DIR) platform-unixlike; \
	fi
	HOME=/tmp node scripts/esbuild.js ./esbuild --wasm

override_dh_auto_test:
	dh_auto_test --builddirectory=_build --buildsystem=golang
	node -e 'require("./npm/esbuild")'

override_dh_install:
	dh_install
	set -e; if test "$(NPM_DIR)" != ""; then \
		cp -a npm/$(NPM_DIR) debian/esbuild/usr/lib/$(DEB_TARGET_MULTIARCH)/nodejs/; \
	fi
	find debian/esbuild/usr/lib/$(DEB_TARGET_MULTIARCH)/nodejs/ -name '*.md' -delete || true

override_dh_gencontrol:
	set -e; if test "$(NPM_DIR)"; then \
		dh_gencontrol -- -Vnodejs:Provides="node-$(NPM_DIR)"; \
	else \
		dh_gencontrol; \
	fi
