#!/bin/sh
# Builds the terminal guest's BusyBox: 1.37.0, linked statically against musl, from a pinned
# Buildroot LTS, inside a Debian container (podman, else docker). build_rootfs.sh then swaps it
# into Bellard's image. Building it ourselves is what makes the guest's GPL source exact: the
# Buildroot release, buildroot.defconfig and busybox.config pin everything, and `make legal-info`
# collects the corresponding source (out/legal-info/, which build_source_bundle.sh ships).
#
# Outputs, in terminal/tools/out/ (gitignored):
#   busybox         the stripped static riscv64 binary
#   busybox.links   one applet path per line, for build_rootfs.sh
#   legal-info/     sources, licenses and manifests from `make legal-info`
#
# The Buildroot tree, downloads and build output (several GB) live outside the repo, in
# $PSEF_BR_WORK (default ~/.cache/psef-buildroot). A first build takes 20-40 minutes, most of
# it the cross toolchain; a rebuild after a busybox.config change takes a minute or two.
set -eu
here=$(cd "$(dirname "$0")" && pwd)
repo=$(cd "$here/../.." && pwd)
work=${PSEF_BR_WORK:-$HOME/.cache/psef-buildroot}
br_ver=2026.02.3
br_sha256=5a59e7501b0b4ec52c41f4bfa79412320e0b37eae5f719605a258e8d0c6fc7fb
image=docker.io/library/debian:12
# BusyBox stamps its build time into the binary ("BusyBox v1.37.0 (<date>)"), read by its kconfig
# from SOURCE_DATE_EPOCH. Pinned to the build that ships in terminal/image/ (2026-11-14 16:08:18
# UTC), so a rebuild is byte-identical and build_source_bundle.sh accepts it without a
# build_rootfs.sh, which would change the image CRC and reset every slot's guest disk. Change it
# only together with a deliberate build_rootfs.sh.
source_date_epoch=1794672498

if command -v podman >/dev/null 2>&1; then
  engine=podman
  owner=""  # rootless podman: container root is the host user already
elif command -v docker >/dev/null 2>&1; then
  engine=docker
  owner="$(id -u):$(id -g)"  # rootful docker: hand the outputs back to the caller
else
  echo "build_busybox.sh: needs podman or docker" >&2
  exit 1
fi

mkdir -p "$work" "$here/out"
tarball="$work/buildroot-$br_ver.tar.xz"
if [ ! -f "$tarball" ]; then
  curl -sSfL "https://buildroot.org/downloads/buildroot-$br_ver.tar.xz" -o "$tarball.part"
  mv "$tarball.part" "$tarball"
fi
echo "$br_sha256  $tarball" | sha256sum -c -

# FORCE_UNSAFE_CONFIGURE: the build runs as the container's root, which some configure
# scripts (host-tar) refuse without it.
"$engine" run --rm -v "$work:/w:Z" -v "$repo:/src:Z" \
  -e DEBIAN_FRONTEND=noninteractive -e FORCE_UNSAFE_CONFIGURE=1 -e BR_VER="$br_ver" -e OWNER="$owner" \
  -e SOURCE_DATE_EPOCH="$source_date_epoch" "$image" sh -euc '
  apt-get update -qq
  apt-get install -y -qq build-essential file wget cpio unzip rsync bc python3 perl \
    xz-utils bzip2 patch ca-certificates >/dev/null
  cd /w
  [ -d "buildroot-$BR_VER" ] || tar -xf "buildroot-$BR_VER.tar.xz"
  br="make -C /w/buildroot-$BR_VER O=/w/output BR2_DL_DIR=/w/dl"
  $br BR2_DEFCONFIG=/src/terminal/tools/buildroot.defconfig defconfig
  # Pick up busybox.config edits: Buildroot only reads the file when BusyBox is configured.
  $br busybox-dirclean
  $br
  # Linux 4.15 predates the LICENSES/ directory Buildroot expects (4.16+); its license is COPYING.
  $br legal-info LINUX_HEADERS_LICENSE_FILES=COPYING
  out=/src/terminal/tools/out
  rm -rf "$out/legal-info"
  cp /w/output/target/bin/busybox "$out/busybox"
  cp /w/output/build/busybox-*/busybox.links "$out/busybox.links"
  cp -r /w/output/legal-info "$out/legal-info"
  [ -z "$OWNER" ] || chown -R "$OWNER" "$out" /w
'

! command -v file >/dev/null 2>&1 || file "$here/out/busybox"
sha256sum "$here/out/busybox"
echo "applets: $(wc -l < "$here/out/busybox.links")"
echo "wrote $here/out"
