#!/usr/bin/make -f

# Debian rules for pi-dev.
#
# pi is a TypeScript npm workspaces monorepo. Upstream builds it with the
# tsgo compiler (@typescript/native-preview), which is not packaged in
# Debian; the debian/patches/* series rewrites the per-package build scripts
# to use the classic `tsc` compiler and repoints the tsconfig "extends" to
# debian/tsconfig.base.build.json (a build-time variant of tsconfig.base.json
# with the tsgo-only flags removed and target raised to ES2024).
#
# Dependencies are handled two ways:
#   * Runtime dependencies (everything shipped in /usr/lib/pi) are VENDORED
#     under debian/vendor/node_modules.tar.xz and installed verbatim; this
#     keeps the shipped runtime pinned and reproducible.
#   * The BUILD-TIME toolchain (the TypeScript compiler, shx and the @types/*
#     needed to compile) is NOT vendored. It is fetched from npm at build time
#     (npm install, loose semver) on top of the extracted runtime node_modules.
#     This requires a network-enabled build worker (e.g. sbuild with network
#     access); it will NOT build on a fully offline buildd.
#
# The toolchain is only a build tool: it never ships in the binary package, so
# fetching it does not affect the reproducibility of the shipped runtime.

export DH_VERBOSE = 1
export NODE_PATH := $(CURDIR)/node_modules
# sbuild sets HOME=/sbuild-nonexistent (unwritable); npm needs a writable HOME
# for its cache/logs/tmp. Redirect HOME into the build tree.
export HOME := $(CURDIR)/debian/fakehome

# Packages that make up the runtime, in build order.
PI_PACKAGES := tui telemetry ai agent protocol client server \
	session-backends/sqlite-node coding-agent

# Build-time toolchain fetched from npm (loose semver) during configure.
# These are the tools/@types needed to compile but not present in the
# vendored runtime node_modules.
PI_BUILD_TOOLCHAIN := typescript shx \
	@types/cross-spawn @types/diff @types/hosted-git-info @types/ms \
	@types/proper-lockfile @types/semver

%:
	dh $@

# ----------------------------------------------------------------------------
# Configure: lay down the vendored runtime node_modules (needed for the
# compiler to type-check cross-package imports), then fetch the build-time
# toolchain from npm over the network.
# ----------------------------------------------------------------------------
override_dh_auto_configure:
	mkdir -p $(CURDIR)/debian/fakehome
	# Vendored runtime deps (also used for type-checking at build time).
	tar -xJf debian/vendor/node_modules.tar.xz
	# Build-time toolchain from the network (requires a network-enabled build).
	# --engine-strict=false: the vendored runtime (e.g. undici) declares
	# node>=22.19 while we build against the distro's nodejs 20.19; npm's
	# engine check is only a warning by default, but force it off so it can
	# never abort the build. This does NOT relax what the runtime needs.
	npm install --no-save --ignore-scripts --no-audit --no-fund \
		--engine-strict=false \
		$(PI_BUILD_TOOLCHAIN)
	cp debian/tsconfig.base.build.json tsconfig.base.build.json
	dh_auto_configure

# ----------------------------------------------------------------------------
# Build: compile each workspace package with tsc.
# ----------------------------------------------------------------------------
override_dh_auto_build:
	set -e; \
	for p in $(PI_PACKAGES); do \
		echo "==> building packages/$$p"; \
		( cd "packages/$$p" && PATH="$(CURDIR)/node_modules/.bin:$$PATH" npm run build --ignore-scripts ); \
	done

# ----------------------------------------------------------------------------
# Test: skip the upstream test suite (requires LLM provider API keys).
# ----------------------------------------------------------------------------
override_dh_auto_test:
	@echo "Skipping upstream test suite (requires LLM provider credentials)."

# ----------------------------------------------------------------------------
# Install: stage the built runtime into /usr/lib/pi.
# ----------------------------------------------------------------------------
override_dh_auto_install:
	set -e; \
	LIBDIR=debian/pi-dev/usr/lib/pi; \
	mkdir -p "$$LIBDIR/packages"; \
	for p in tui telemetry ai agent protocol client server coding-agent; do \
		echo "==> staging packages/$$p"; \
		mkdir -p "$$LIBDIR/packages/$$p"; \
		cp -a "packages/$$p/package.json" "$$LIBDIR/packages/$$p/"; \
		cp -a "packages/$$p/dist" "$$LIBDIR/packages/$$p/"; \
	done; \
	echo "==> staging packages/sqlite-node"; \
	mkdir -p "$$LIBDIR/packages/sqlite-node"; \
	cp -a packages/session-backends/sqlite-node/package.json "$$LIBDIR/packages/sqlite-node/"; \
	cp -a packages/session-backends/sqlite-node/dist "$$LIBDIR/packages/sqlite-node/"; \
	echo "==> staging coding-agent runtime docs/examples/readme (read by the agent at runtime)"; \
	for f in README.md CHANGELOG.md LICENSE; do \
		[ -e "packages/coding-agent/$$f" ] && cp -a "packages/coding-agent/$$f" "$$LIBDIR/packages/coding-agent/" || true; \
	done; \
	for d in docs examples; do \
		[ -d "packages/coding-agent/$$d" ] && cp -a "packages/coding-agent/$$d" "$$LIBDIR/packages/coding-agent/" || true; \
	done; \
	echo "==> staging vendored runtime node_modules"; \
	tar -xJf debian/vendor/node_modules.tar.xz -C "$$LIBDIR"; \
	rm -rf "$$LIBDIR/node_modules/.bin"; \
	echo "==> trimming source/test content from staged packages (keep docs/examples)"; \
	find "$$LIBDIR/packages" -type d \( -name src -o -name test -o -name tests -o -name __tests__ \) -prune -exec rm -rf {} + 2>/dev/null || true; \
	find "$$LIBDIR/packages" -name '*.ts' ! -name '*.d.ts' -delete; \
	find "$$LIBDIR/packages" -name '*.tsbuildinfo' -delete; \
	echo "==> fixing @earendil-works workspace symlinks"; \
	for l in "$$LIBDIR"/node_modules/@earendil-works/*; do \
		[ -L "$$l" ] || continue; \
		pkg=$$(basename "$$(readlink "$$l")"); \
		ln -sfn "../../packages/$$pkg" "$$l"; \
	done

# ----------------------------------------------------------------------------
# Clean.
# ----------------------------------------------------------------------------
override_dh_auto_clean:
	rm -rf node_modules tsconfig.base.build.json debian/fakehome
	set -e; for p in $(PI_PACKAGES); do rm -rf "packages/$$p/dist"; done
	dh_auto_clean

# node_modules and dist are build artifacts; do not scan them for shlib deps.
override_dh_shlibdeps:
	@echo "Skipping dh_shlibdeps (JavaScript/WASM runtime, no ELF shared libs)."

override_dh_strip:
	@echo "Skipping dh_strip (no native ELF binaries to strip)."
