#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/default.mk

# Parrot echo has Node.js 20, while upstream requires Node.js 24.22.3.
# Use the same upstream-pinned Node release and keep pnpm's downloads and
# caches inside the source tree.
NODE_VERSION := 24.22.3
PNPM_VERSION := 11.28.5
NODE_ARCH := $(if $(filter amd64,$(DEB_HOST_ARCH)),x64,arm64)
NODE_SHA256 := $(if $(filter amd64,$(DEB_HOST_ARCH)),061ec3e3062115705b56b51fa9cc330ab50d34b6969387d4e4b1a50c6809acc2,261af2af250dfd7e9f25cf151d1b2713a0adf8f10a9ad116c4947f68627affe8)
PNPM_SHA512 := 484563d30b4c2ad27cb48aa94ff2b302dbcebc8ff6bf28816c2589886e146ce7f76f354f3bc1a438e6f5e9768c302aa0a6f7add8232903cd4cf81426cbc14d9e
NODE_ARCHIVE := node-v$(NODE_VERSION)-linux-$(NODE_ARCH).tar.gz
NODE_DIR := $(CURDIR)/debian/node
PNPM_DIR := $(CURDIR)/debian/pnpm/package
RUNTIME_DIR := $(CURDIR)/debian/runtime
PNPM = $(NODE_DIR)/bin/node $(PNPM_DIR)/bin/pnpm.mjs

export HOME := $(CURDIR)/debian/home
export XDG_CACHE_HOME := $(CURDIR)/debian/xdg-cache
export PNPM_CONFIG_STORE_DIR := $(CURDIR)/debian/pnpm-store
export npm_config_cache := $(CURDIR)/debian/npm-cache
export npm_config_devdir := $(CURDIR)/debian/node-gyp-cache
export PATH := $(NODE_DIR)/bin:$(CURDIR)/debian/pnpm-home:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export CI := true
export DSH_TELEMETRY_DISABLED := 1
export DSH_CLIENT_COMMIT_HASH := 4b399c39a6d1479cd84ca591b4d7c88e24d38a96
export DSH_CLIENT_VERSION := 0.2.3-alpha.2

%:
	dh $@

override_dh_auto_clean:
	dh_clean
	if [ -x $(NODE_DIR)/bin/node ] && [ -f node_modules/.modules.yaml ]; then \
		$(PNPM) run clean; \
	fi
	rm -rf debian/node debian/pnpm debian/pnpm-home debian/runtime \
		debian/pnpm-store debian/npm-cache debian/node-gyp-cache \
		debian/xdg-cache debian/home debian/node.tar.gz debian/pnpm.tgz \
		native/system/.release native/system/packages/linux-*/bin
	find . -type d -name node_modules -prune -exec rm -rf '{}' +

override_dh_auto_configure:
	mkdir -p $(CURDIR)/debian/home $(CURDIR)/debian/pnpm-home debian/pnpm
	curl --fail --location --retry 3 --output debian/node.tar.gz \
		https://nodejs.org/dist/v$(NODE_VERSION)/$(NODE_ARCHIVE)
	printf '%s  %s\n' '$(NODE_SHA256)' debian/node.tar.gz | sha256sum --check
	tar xzf debian/node.tar.gz -C debian
	mv debian/node-v$(NODE_VERSION)-linux-$(NODE_ARCH) $(NODE_DIR)
	curl --fail --location --retry 3 --output debian/pnpm.tgz \
		https://registry.npmjs.org/pnpm/-/pnpm-$(PNPM_VERSION).tgz
	printf '%s  %s\n' '$(PNPM_SHA512)' debian/pnpm.tgz | sha512sum --check
	tar xzf debian/pnpm.tgz -C debian/pnpm
	chmod +x $(PNPM_DIR)/bin/pnpm.mjs
	ln -s ../pnpm/package/bin/pnpm.mjs debian/pnpm-home/pnpm
	$(PNPM) --version
	$(PNPM) install --frozen-lockfile

override_dh_auto_build:
	$(PNPM) run verify-runtime-closure
	$(PNPM) run build:artifacts --profile official
	$(PNPM) --dir native/system run build:native

override_dh_auto_test:

override_dh_auto_install:
	rm -rf $(RUNTIME_DIR)
	$(PNPM) --filter dsh-python-runtime-closure deploy --prod \
		--config.allow-unused-patches=true \
		--config.node-linker=hoisted \
		--config.pnpmfile=$(CURDIR)/scripts/python-runtime-deploy.pnpmfile.mjs \
		--config.link-workspace-packages=true \
		--config.hoist-workspace-packages=false \
		$(RUNTIME_DIR)
	$(NODE_DIR)/bin/node --import tsx/esm --input-type=module --eval \
		"import { materializeStagedLinks, deduplicateStagedWorkspacePackages } from './scripts/build-exe-for-python-sdk-staging.ts'; import { verifyStagedDependencies } from './scripts/executable-packaging.ts'; await materializeStagedLinks('$(RUNTIME_DIR)'); await deduplicateStagedWorkspacePackages('$(RUNTIME_DIR)', '$(RUNTIME_DIR)'); await verifyStagedDependencies('$(RUNTIME_DIR)');"
	install -d $(CURDIR)/debian/dsh/usr/lib/dsh
	cp -a $(RUNTIME_DIR) $(CURDIR)/debian/dsh/usr/lib/dsh/app
	install -Dm755 $(NODE_DIR)/bin/node \
		$(CURDIR)/debian/dsh/usr/lib/dsh/node/bin/node
	install -Dm644 $(NODE_DIR)/LICENSE \
		$(CURDIR)/debian/dsh/usr/lib/dsh/node/LICENSE
	cp -a $(PNPM_DIR) $(CURDIR)/debian/dsh/usr/lib/dsh/pnpm
	install -d $(CURDIR)/debian/dsh/usr/lib/dsh/bin \
		$(CURDIR)/debian/dsh/usr/bin
	ln -s ../node/bin/node $(CURDIR)/debian/dsh/usr/lib/dsh/bin/node
	printf '%s\n' '#!/bin/sh' \
		'exec /usr/lib/dsh/node/bin/node /usr/lib/dsh/pnpm/bin/pnpm.mjs "$$@"' \
		> $(CURDIR)/debian/dsh/usr/lib/dsh/bin/pnpm
	printf '%s\n' '#!/bin/sh' \
		'PATH="/usr/lib/dsh/bin$${PATH:+:$$PATH}"' 'export PATH' \
		'exec /usr/lib/dsh/node/bin/node /usr/lib/dsh/app/node_modules/@deepseek-ai/dsh/lib/bin.js "$$@"' \
		> $(CURDIR)/debian/dsh/usr/lib/dsh/bin/dsh
	chmod 755 $(CURDIR)/debian/dsh/usr/lib/dsh/bin/pnpm \
		$(CURDIR)/debian/dsh/usr/lib/dsh/bin/dsh
	ln -s ../lib/dsh/bin/dsh $(CURDIR)/debian/dsh/usr/bin/dsh
