# Shared deployment constants + helpers for the product container images.
# Single-sourced in @norskvideo/ctl-dev-kit and sourced by the product build
# scripts (the bundle driver build-bundle-image.sh, and studio's bespoke
# build-image.sh). This is the one place the pinned base + bun live, so no two
# product images can drift.
#
# Location-independent: this file makes NO assumption about where in a repo it
# sits (it used to live at products/deployment/common.sh and derive the repo
# root from its own path). The same file works vendored/symlinked in the
# monorepo and installed into a split-out product repo's node_modules — callers
# pass the product directory in, they don't rely on this file's location.
#
# Base choice — gcr.io/distroless/cc-debian12: glibc plus libgcc/libstdc++ (bun
# and probe's native rust addon link these) and nothing else — no shell, no
# package manager. studio's compiled binary would fit the smaller
# distroless/base-debian12, but we take probe's minimum (cc) for every product so
# a host running several products pulls one shared base layer. Pinned by digest
# because distroless publishes only moving tags (no immutable version tags). No
# shell is fine: images run via exec-form CMD and health is checked over HTTP.
PRODUCT_BASE_IMAGE="gcr.io/distroless/cc-debian12@sha256:7ee09f36862efbdbf70422db263e411c2618409ca46faa555bd5b636155307df"

# The pinned bun version. Override with BUN_VERSION; otherwise walk up from the
# given directory (default: PWD) to the nearest package.json declaring
# "packageManager": "bun@X" — the workspace root in the monorepo, the repo root
# in a split-out product. Prints X.
product_bun_version() {
  if [ -n "${BUN_VERSION:-}" ]; then
    printf '%s' "${BUN_VERSION}"
    return 0
  fi
  local dir version
  dir="${1:-$PWD}"
  while [ "${dir}" != "/" ]; do
    if [ -f "${dir}/package.json" ]; then
      version="$(sed -n 's/.*"packageManager": *"bun@\([^"]*\)".*/\1/p' "${dir}/package.json")"
      if [ -n "${version}" ]; then
        printf '%s' "${version}"
        return 0
      fi
    fi
    dir="$(dirname "${dir}")"
  done
  echo "could not determine bun version (no packageManager in any ancestor package.json)" >&2
  return 1
}

# Prints the upstream oven/bun slim image ref for the pinned version. The bundle
# products import their runtime bun from this image Docker-natively (COPY --from),
# so they never touch the host cache; only studio's host-side --compile needs the
# extracted binary below.
product_bun_slim_image() {
  local version
  version="$(product_bun_version)" || return 1
  printf 'oven/bun:%s-slim' "${version}"
}

# Prints the path to an upstream (glibc, FHS-loader) bun of the pinned version,
# extracted from oven/bun:<version>-slim and cached. The nix-devshell bun is
# patched to a /nix/store loader: it won't run in the base image, and baked into
# a `--compile` binary it makes that binary fail to exec in a stock container.
# So the bun that ships (bundle products) or does the compile (studio) must be
# this one. Progress goes to stderr so `$(product_upstream_bun)` captures only
# the path.
product_upstream_bun() {
  local version cache_dir bun_path cid
  version="$(product_bun_version)" || return 1
  cache_dir="${XDG_CACHE_HOME:-${HOME}/.cache}/norsk-ctl"
  bun_path="${cache_dir}/bun-${version}"
  if [ ! -x "${bun_path}" ]; then
    echo "==> fetching upstream bun ${version} from oven/bun:${version}-slim" >&2
    mkdir -p "${cache_dir}"
    cid="$(docker create "oven/bun:${version}-slim")"
    docker cp "${cid}:/usr/local/bin/bun" "${bun_path}"
    docker rm "${cid}" >/dev/null
  fi
  printf '%s' "${bun_path}"
}
