#!/usr/bin/make -f

# Debian rules for opencode.
#
# opencode is a Bun-native TypeScript monorepo. There is no Node.js-runnable
# build: the CLI is produced by `bun build --compile` as a self-contained
# binary that embeds the Bun runtime and all JS dependencies. Bun is not
# packaged in Debian, so this build:
#
#   1. downloads the Bun toolchain (pinned to upstream's packageManager
#      version) from the network,
#   2. installs the npm dependencies with `bun install` (pinned by the
#      committed bun.lock), and
#   3. builds the CLI for the build host's architecture with
#      `bun run script/build.ts --single`, which also runs upstream's smoke
#      test (opencode --version).
#
# Everything (Bun, deps, models.dev catalog) is fetched from the network at
# build time: this package REQUIRES a network-enabled build worker and will
# not build on an offline buildd.
#
# The optional embedded web UI (`opencode web`) is skipped
# (--skip-embed-web-ui) to keep the build tractable; the TUI, headless server
# and the rest of the CLI are unaffected.

export DH_VERBOSE = 1
# sbuild sets HOME=/sbuild-nonexistent (unwritable); bun needs a writable HOME
# (install cache, tmp). Redirect HOME into the build tree.
export HOME := $(CURDIR)/debian/fakehome

# Upstream version and the Bun version upstream requires.
UPSTREAM_VERSION := 1.18.21
BUN_VERSION := 1.3.14

# Debian arch -> Bun build target name.
ifeq ($(DEB_HOST_ARCH),amd64)
BUN_TARGET_ARCH := x64
BUN_DL_ARCH := x64
else ifeq ($(DEB_HOST_ARCH),arm64)
BUN_TARGET_ARCH := arm64
BUN_DL_ARCH := aarch64
else
$(error opencode: unsupported DEB_HOST_ARCH $(DEB_HOST_ARCH))
endif

BUN_HOME := $(CURDIR)/debian/.bun-toolchain
BUN := $(BUN_HOME)/bun/bun

# Where the built binary lands.
DIST_NAME := opencode-linux-$(BUN_TARGET_ARCH)
DIST_DIR := packages/opencode/dist/$(DIST_NAME)

%:
	dh $@

# ----------------------------------------------------------------------------
# Configure: fetch the Bun toolchain (network).
# ----------------------------------------------------------------------------
override_dh_auto_configure:
	set -e; \
	echo "==> fetching bun $(BUN_VERSION) for linux-$(BUN_DL_ARCH)"; \
	mkdir -p "$(BUN_HOME)"; \
	curl -fSL -o "$(BUN_HOME)/bun.zip" \
		"https://github.com/oven-sh/bun/releases/download/bun-v$(BUN_VERSION)/bun-linux-$(BUN_DL_ARCH).zip"; \
	unzip -q -o "$(BUN_HOME)/bun.zip" -d "$(BUN_HOME)"; \
	mv "$(BUN_HOME)/bun-linux-$(BUN_DL_ARCH)" "$(BUN_HOME)/bun"; \
	rm -f "$(BUN_HOME)/bun.zip"; \
	"$(BUN)" --version

# ----------------------------------------------------------------------------
# Build: install deps and compile the CLI for the build host's arch.
# ----------------------------------------------------------------------------
override_dh_auto_build:
	set -e; \
	mkdir -p $(CURDIR)/debian/fakehome; \
	export PATH="$(BUN_HOME)/bun:$$PATH"; \
	echo "==> bun install (deps pinned by bun.lock; lifecycle scripts skipped:"; \
	echo "    native modules ship prebuilt binaries and need no node-gyp)"; \
	bun install --ignore-scripts; \
	echo "==> building opencode-linux-$(BUN_TARGET_ARCH) (single target, no web UI)"; \
	cd packages/opencode; \
	OPENCODE_VERSION=$(UPSTREAM_VERSION) \
		bun run script/build.ts --single --skip-install --skip-embed-web-ui

# ----------------------------------------------------------------------------
# Test: the build already ran upstream's `opencode --version` smoke test.
# The full upstream test suite needs LLM provider credentials; skip it.
# ----------------------------------------------------------------------------
override_dh_auto_test:
	@echo "Skipping upstream test suite (requires LLM provider credentials)."
	@echo "Upstream smoke test (opencode --version) ran during the build."

# ----------------------------------------------------------------------------
# Install: stage the compiled binary into /usr/lib/opencode and link it into
# /usr/bin. dh_install (debian/opencode.install) copies dist/ into the package.
# ----------------------------------------------------------------------------
override_dh_auto_install:
	set -e; \
	mkdir -p debian/opencode/usr/bin; \
	ln -s ../lib/opencode/dist/$(DIST_NAME)/bin/opencode debian/opencode/usr/bin/opencode

override_dh_install:
	dh_install

# ----------------------------------------------------------------------------
# Clean.
# ----------------------------------------------------------------------------
override_dh_auto_clean:
	rm -rf "$(BUN_HOME)" node_modules packages/opencode/dist debian/fakehome
	dh_auto_clean
