#!/bin/sh
# Installs the SB-Box skill into the current project:
#   .opencode/skills/sb-box/SKILL.md   — instructions for driving sandboxes with `gsk sb-box` (no code)
#
#   curl -fsSL https://www.genspark.ai/sb-box/opencode/install.sh | sh
#
# Existing files are left alone. The skill needs `gsk` on PATH
# (npm i -g @genspark/cli && gsk login); sandboxes come from the public
# base template unless the model is told to use one of yours.
set -eu

BASE="${SB_BOX_OPENCODE_BASE:-https://www.genspark.ai/sb-box/opencode}"

fetch() {
  # $1 = relative path under BASE and under .opencode/
  dest=".opencode/$1"
  if [ -e "$dest" ]; then
    echo "skip   $dest (exists)"
    return
  fi
  mkdir -p "$(dirname "$dest")"
  # Download beside the destination and move into place only on success, so
  # a failed or interrupted fetch never leaves an empty file that the next
  # run would "skip".
  if ! curl -fsSL "$BASE/$1" -o "$dest.part"; then
    rm -f "$dest.part"
    echo "failed $BASE/$1" >&2
    exit 1
  fi
  mv "$dest.part" "$dest"
  echo "wrote  $dest"
}

fetch skills/sb-box/SKILL.md

command -v gsk >/dev/null 2>&1 || echo "note: gsk not found — npm i -g @genspark/cli && gsk login"
echo "done. Start opencode (or any agent that reads .opencode/skills) and ask it to run code on a sandbox."
