#!/bin/sh
# The werkmeister repo's werkmeister-install setup container, mounted from the
# workspace's own worktree (`${REPO}/apps/cli/…` in services.yaml, SW-555), so a
# branch's change to it runs in that branch's containers. With `install`, it installs and builds the repo
# first; then it installs the host-facing CLIs the werkmeister repo's own services
# need into the per-feature `tools` volume mounted at /opt/werkmeister-tools.
# Idempotent: each download is skipped when its binary already exists, so a
# re-up of the same feature costs nothing.
#
# `install` is an argument, not the default, because this file comes from the
# worktree while the command comes from ~/.werkmeister/services.yaml: a registry
# synced before the `install` argument still calls this with no argument, and it
# must not install twice.
#
# SW-31: branches from before and after the pnpm switch both run this script, so
# the lockfile in /app picks the package manager.
set -eu

TOOLS=${WERKMEISTER_TOOLS_DIR:-/opt/werkmeister-tools}
LOCKS=${WERKMEISTER_INSTALL_LOCKS:-/locks}
PATH="$TOOLS/bin:$PATH"

# corepack's pnpm shim, in the tools volume so the app containers (which have it
# on PATH) can run pnpm too; it fetches the packageManager version on first use.
if [ ! -x "$TOOLS/bin/pnpm" ]; then
  mkdir -p "$TOOLS/bin"
  corepack enable --install-directory "$TOOLS/bin" pnpm
fi

# Hold one of WERKMEISTER_INSTALL_SLOTS host-wide lock files (the bind-mounted
# ~/.werkmeister/locks) so workspaces starting together don't all install at once.
# The slot is held on fd 9 until the install finishes.
take_install_slot() {
  slots=${WERKMEISTER_INSTALL_SLOTS:-2}
  waiting=""
  while :; do
    i=1
    while [ "$i" -le "$slots" ]; do
      exec 9>"$LOCKS/install-$i"
      flock -n 9 && return
      i=$((i + 1))
    done
    if [ -z "$waiting" ]; then
      echo "werkmeister-install: $slots installs already running on this host, waiting"
      waiting=1
    fi
    sleep 1
  done
}

# SW-33: skip npm ci while the lockfile and Node major match the stamp in the
# node_modules volume (npm ci wipes node_modules). The stamp is removed before
# and written only after a successful install, so a failed one retries next up.
# SW-362: pnpm never skips: its own up-to-date check takes seconds and restores
# a package missing under the volume, which the stamps cannot see. The build
# always runs: turbo restores it from the shared TURBO_CACHE_DIR, and dist/ may
# be stale after a pull. SW-541: the pr gate's starts read the worktree stamp
# and pass --no-deps while it matches, so the gate skips this script; keep the
# stamp format in step with setupsCurrent.
# SW-85: each package's own node_modules is in the worktree, not the volume, so
# a second stamp in the worktree (.turbo/ is git-ignored on every branch) must
# match too: a worktree re-created on a kept volume has none, and reinstalls.
if [ "${1:-}" = install ]; then
  mkdir -p "$LOCKS"
  if [ -f pnpm-lock.yaml ]; then
    pm=pnpm
    install="pnpm install --frozen-lockfile"
  else
    pm=npm
    install="npm ci"
  fi
  # Stamp names predate SW-93; renaming them would force one full reinstall per workspace.
  stamp=node_modules/.werkmeister-install-hash
  want="$(cat package-lock.json pnpm-lock.yaml 2>/dev/null | sha256sum | cut -d' ' -f1) $(node -v | cut -d. -f1)"
  wstamp=.turbo/.werkmeister-install-hash
  if [ "$pm" = npm ] && [ "$(cat "$stamp" 2>/dev/null)" = "$want" ] && [ "$(cat "$wstamp" 2>/dev/null)" = "$want" ]; then
    echo "werkmeister-install: lockfile and Node major unchanged, skipping $install"
  else
    take_install_slot
    rm -f "$stamp" "$wstamp"
    $install
    mkdir -p .turbo
    echo "$want" >"$stamp"
    echo "$want" >"$wstamp"
    exec 9>&-
  fi
  # SW-35: one build at a time host-wide. turbo names its temp cache files by
  # pid, and identical install containers get identical pids, so two installs
  # of one commit could write the same temp file. Queued, the first builds and
  # the rest restore from the cache.
  flock "$LOCKS/turbo-build" $pm run build
fi

DOCKER_VERSION=27.5.1
COMPOSE_VERSION=v2.32.4
KUBECTL_VERSION=v1.31.4

ARCH="$(uname -m)" # x86_64 | aarch64 — docker/compose name assets this way
case "$ARCH" in
  x86_64) KARCH=amd64 ;;
  aarch64) KARCH=arm64 ;;
  *) echo "werkmeister-install: unsupported arch $ARCH" >&2; exit 1 ;;
esac

mkdir -p "$TOOLS/bin" "$TOOLS/docker/cli-plugins"

if [ ! -x "$TOOLS/bin/docker" ]; then
  curl -fsSL "https://download.docker.com/linux/static/stable/$ARCH/docker-$DOCKER_VERSION.tgz" \
    | tar -xz -C "$TOOLS/bin" --strip-components=1 docker/docker
fi

if [ ! -x "$TOOLS/docker/cli-plugins/docker-compose" ]; then
  curl -fsSL -o "$TOOLS/docker/cli-plugins/docker-compose" \
    "https://github.com/docker/compose/releases/download/$COMPOSE_VERSION/docker-compose-linux-$ARCH"
  chmod +x "$TOOLS/docker/cli-plugins/docker-compose"
fi

if [ ! -x "$TOOLS/bin/kubectl" ]; then
  curl -fsSL -o "$TOOLS/bin/kubectl" "https://dl.k8s.io/release/$KUBECTL_VERSION/bin/linux/$KARCH/kubectl"
  chmod +x "$TOOLS/bin/kubectl"
fi

# SW-580: the plugin's gen-types, `claude plugin validate` and `claude plugin test` need `claude`.
# The version is CI's pin (.github/workflows/ci.yml); none of them needs a login.
CLAUDE_CODE_VERSION=2.1.316
if [ ! -x "$TOOLS/bin/claude" ]; then
  npm install --prefix "$TOOLS/claude-code" "@anthropic-ai/claude-code@$CLAUDE_CODE_VERSION"
  ln -sf "$TOOLS/claude-code/node_modules/.bin/claude" "$TOOLS/bin/claude"
fi

# SW-604, SW-683: node:22 has no lsof, so pause-resume's resumeAndNudge tests skipped in the gate. It goes here, not in
# a service's `test:` row: that row also runs on the host as a pre-approved command, and must not run apt-get there.
if [ ! -x "$TOOLS/bin/lsof" ]; then
  deb_dir="$(mktemp -d)"
  (cd "$deb_dir" && apt-get update -qq && apt-get download -qq lsof)
  dpkg -x "$deb_dir"/lsof_*.deb "$TOOLS/lsof"
  rm -rf "$deb_dir"
  ln -sf "$TOOLS/lsof/usr/bin/lsof" "$TOOLS/bin/lsof"
fi

"$TOOLS/bin/docker" --version
"$TOOLS/bin/claude" --version
"$TOOLS/bin/lsof" -v 2>&1 | head -n 3
DOCKER_CONFIG="$TOOLS/docker" "$TOOLS/bin/docker" compose version
"$TOOLS/bin/kubectl" version --client
