#!/usr/bin/make -f

# Build pi from source as an upstream-supported Bun standalone executable.
#
# The source build uses Node/npm and upstream's pinned tsgo compiler for the
# workspace build and TypeScript code generation, so nodejs >= 22.19 remains
# a build dependency (currently provided by node26). Bun 1.3.14 is not
# packaged in Debian and is downloaded as a pinned build tool. npm dependencies
# are installed from package-lock.json.
#
# The binary package contains only the compiled Pi executable and the runtime
# assets Pi deliberately resolves beside it. No system Node.js, Bun, or full
# node_modules tree is required at runtime.

export DH_VERBOSE = 1
export HOME := $(CURDIR)/debian/fakehome

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

BUN_VERSION := 1.3.14
ifeq ($(DEB_HOST_ARCH),amd64)
BUN_ARCHIVE := bun-linux-x64-baseline.zip
BUN_DIR := bun-linux-x64-baseline
BUN_SHA256 := a063908ae08b7852ca10939bbdc6ceed3ddabce8fb9402dce83d65d73b36e6c7
CLIPBOARD_PACKAGE := clipboard-linux-x64-gnu
CLIPBOARD_FILE := clipboard.linux-x64-gnu.node
else ifeq ($(DEB_HOST_ARCH),arm64)
BUN_ARCHIVE := bun-linux-aarch64.zip
BUN_DIR := bun-linux-aarch64
BUN_SHA256 := a27ffb63a8310375836e0d6f668ae17fa8d8d18b88c37c821c65331973a19a3b
CLIPBOARD_PACKAGE := clipboard-linux-arm64-gnu
CLIPBOARD_FILE := clipboard.linux-arm64-gnu.node
else
$(error pi-dev: unsupported DEB_HOST_ARCH $(DEB_HOST_ARCH))
endif

BUN_HOME := $(CURDIR)/debian/.bun-toolchain
BUN_URL := https://github.com/oven-sh/bun/releases/download/bun-v$(BUN_VERSION)/$(BUN_ARCHIVE)
BUN := $(BUN_HOME)/$(BUN_DIR)/bun
PI_DIST := $(CURDIR)/packages/coding-agent/dist

%:
	dh $@

override_dh_auto_configure:
	mkdir -p $(CURDIR)/debian/fakehome $(BUN_HOME)
	curl --retry 5 --retry-all-errors -fSL -o "$(BUN_HOME)/$(BUN_ARCHIVE)" "$(BUN_URL)"
	printf '%s  %s\n' '$(BUN_SHA256)' '$(BUN_ARCHIVE)' | \
		(cd "$(BUN_HOME)" && sha256sum -c -)
	unzip -q -o "$(BUN_HOME)/$(BUN_ARCHIVE)" -d "$(BUN_HOME)"
	rm -f "$(BUN_HOME)/$(BUN_ARCHIVE)"
	"$(BUN)" --version
	# Locked source-build dependencies; lifecycle scripts are not required.
	npm ci --ignore-scripts --no-audit --no-fund

override_dh_auto_build:
	# Compile every workspace from source using the model snapshot included in
	# the upstream source release. This avoids mutable network-generated data.
	PATH="$(dir $(BUN)):$(CURDIR)/node_modules/.bin:$$PATH" npm run build:offline
	# Upstream standalone entrypoint plus the explicit image worker entrypoint.
	cd packages/coding-agent && PATH="$(dir $(BUN)):$(CURDIR)/node_modules/.bin:$$PATH" \
		bun build --compile --no-compile-autoload-bunfig \
		./dist/bun/cli.js ./src/utils/image-resize-worker.ts --outfile dist/pi
	# Copy only the runtime assets expected beside a Bun-compiled executable.
	PATH="$(dir $(BUN)):$(CURDIR)/node_modules/.bin:$$PATH" \
		npm --prefix packages/coding-agent run copy-binary-assets
	# Stage the platform clipboard addon exactly as upstream release archives do.
	mkdir -p "$(PI_DIST)/node_modules/@mariozechner/clipboard"
	cp -a node_modules/@mariozechner/clipboard/. \
		"$(PI_DIST)/node_modules/@mariozechner/clipboard/"
	cp -a "node_modules/@mariozechner/$(CLIPBOARD_PACKAGE)/$(CLIPBOARD_FILE)" \
		"$(PI_DIST)/node_modules/@mariozechner/clipboard/$(CLIPBOARD_FILE)"

override_dh_auto_test:
	if echo " $(DEB_BUILD_OPTIONS) " | grep -q ' nocheck '; then \
		echo "Skipping smoke tests (DEB_BUILD_OPTIONS contains nocheck)."; \
		exit 0; \
	fi
	# The build already compiled and staged the executable. Smoke-test the
	# standalone mode and session picker startup surface without provider keys.
	env -u BUN_BE_BUN "$(PI_DIST)/pi" --version
	env -u BUN_BE_BUN "$(PI_DIST)/pi" --help >/dev/null

override_dh_auto_install:
	install -d debian/pi-dev/usr/lib/pi debian/pi-dev/usr/bin
	install -m 0755 "$(PI_DIST)/pi" debian/pi-dev/usr/lib/pi/pi
	# Runtime assets deliberately resolved relative to process.execPath.
	cp -a "$(PI_DIST)/package.json" "$(PI_DIST)/README.md" \
		"$(PI_DIST)/CHANGELOG.md" "$(PI_DIST)/photon_rs_bg.wasm" \
		"$(PI_DIST)/theme" "$(PI_DIST)/assets" "$(PI_DIST)/export-html" \
		"$(PI_DIST)/docs" "$(PI_DIST)/examples" "$(PI_DIST)/node_modules" \
		debian/pi-dev/usr/lib/pi/
	install -m 0755 debian/pi debian/pi-dev/usr/bin/pi
	install -D -m 0644 debian/pi-dev.desktop \
		debian/pi-dev/usr/share/applications/pi-dev.desktop
	install -D -m 0644 debian/pi-dev.svg \
		debian/pi-dev/usr/share/icons/hicolor/scalable/apps/pi-dev.svg

# Bun standalone executables append their module graph to the ELF. strip/dwz
# remove or rewrite that payload and leave a plain Bun runtime.
override_dh_strip:
	@echo "Skipping dh_strip: it corrupts Bun standalone executables."
	env -u BUN_BE_BUN debian/pi-dev/usr/lib/pi/pi --version

override_dh_dwz:
	@echo "Skipping dh_dwz: it is unsafe for Bun standalone executables."

override_dh_auto_clean:
	-dh_auto_clean
	rm -rf node_modules debian/fakehome debian/.bun-toolchain debian/pi-dev
	rm -rf packages/*/dist packages/session-backends/*/dist
	find . -name '*.tsbuildinfo' -delete
	rm -rf .artifacts
