#!/usr/bin/env bash
# Prepares a fresh Amp orb (Debian) to build and test magpie: the Go version
# go.mod names, the GTK 3 / WebKitGTK 4.1 headers the GUI links through cgo
# on Linux (Makefile, .github/workflows/test.yml), a warm module and build
# cache, and Playwright's Chromium and WebKit for `make test-ui`
# (internal/gui/tests/README.md). Idempotent: a stale snapshot skips what it
# already has.
set -euo pipefail
cd "$(dirname "$0")/.."

step() { printf '\n== %s (%s)\n' "$1" "$(date +%T)"; }
SECONDS=0

# --- system packages ---------------------------------------------------------
step "apt packages"
pkgs=(libgtk-3-dev libwebkit2gtk-4.1-dev pkg-config build-essential)
missing=()
for p in "${pkgs[@]}"; do
  dpkg-query -W -f='${Status}' "$p" 2>/dev/null | grep -q 'install ok installed' || missing+=("$p")
done
if ((${#missing[@]})); then
  sudo apt-get update -qq
  sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends "${missing[@]}"
else
  echo "already installed"
fi

# --- Go toolchain: the version go.mod names (CI: go-version-file) ------------
step "Go"
go_version="go$(sed -n 's/^go //p' go.mod)"
if [ "$(/usr/local/go/bin/go env GOVERSION 2>/dev/null || true)" != "$go_version" ]; then
  arch=$(dpkg --print-architecture)
  tmp=$(mktemp -d)
  curl -fsSL --compressed -o "$tmp/go.tgz" "https://go.dev/dl/$go_version.linux-$arch.tar.gz"
  sudo rm -rf /usr/local/go
  sudo tar -C /usr/local -xzf "$tmp/go.tgz"
  rm -rf "$tmp"
fi
sudo ln -sf /usr/local/go/bin/go /usr/local/bin/go
sudo ln -sf /usr/local/go/bin/gofmt /usr/local/bin/gofmt
# A newer go directive must not make the go command fetch another toolchain.
go env -w GOTOOLCHAIN=local
go version

# --- Go modules and build cache ---------------------------------------------
step "go mod download"
go mod download

# Compiling every package and dependency once fills GOCACHE, which the
# snapshot keeps, so a thread's first build or test compiles only its test
# files. (Also linking each package's test binary took over 8 minutes on 2
# cores, for little gain.)
step "warm Go build cache (gtk3)"
go build -tags gtk3 ./...

# --- Playwright for the GUI browser tests ------------------------------------
# The tests `require("playwright")` and find it through NODE_PATH; nothing in
# the repository installs it (internal/gui/tests/README.md).
step "Playwright"
pw_version=1.62.1
pw_dir="$HOME/.local/share/magpie-playwright"
if [ "$(node -p "require('$pw_dir/node_modules/playwright/package.json').version" 2>/dev/null || true)" != "$pw_version" ]; then
  mkdir -p "$pw_dir"
  npm install --prefix "$pw_dir" --no-audit --no-fund --no-save "playwright@$pw_version"
fi
# Installs only what is missing: browsers already in ~/.cache/ms-playwright
# and system libraries already present are skipped.
"$pw_dir/node_modules/.bin/playwright" install --with-deps chromium webkit

marker="# magpie: Playwright for internal/gui/tests"
if ! grep -Fqx "$marker" "$HOME/.bash_profile" 2>/dev/null; then
  cat >>"$HOME/.bash_profile" <<EOF

$marker
export NODE_PATH="$pw_dir/node_modules\${NODE_PATH:+:\$NODE_PATH}"
EOF
fi

step "done in ${SECONDS}s"
