#!/bin/sh
# Skipping Stones setup. Run from inside a git repository:
#   curl -fsSL https://app.skipping-stones.dev/setup.sh | sh
set -eu
BASE="${SKIPPING_STONES_URL:-https://app.skipping-stones.dev}"
HOST=$(printf '%s' "$BASE" | sed -e 's#^[a-z]*://##' -e 's#/.*$##')
BIN_DIR="${SKIPPING_STONES_BIN_DIR:-$HOME/.local/bin}"
step() { printf '\n==> %s\n' "$*"; }
json_field() { sed -n "s/.*\"$1\": *\"\([^\"]*\)\".*/\1/p" | head -n 1; }

top=$(git rev-parse --show-toplevel 2>/dev/null) || { echo "Skipping Stones: run this from inside a git repository."; exit 1; }
cd "$top"

step "Installing the skipping-stones CLI"
curl -fsSL "$BASE/install.sh" | SKIPPING_STONES_URL="$BASE" SKIPPING_STONES_BIN_DIR="$BIN_DIR" sh
PATH="$BIN_DIR:$PATH"; export PATH
ss=skipping-stones

step "Signing in"
if ! org_json=$($ss api /api/v1/org/current 2>/dev/null) || [ -z "$(printf '%s' "$org_json" | json_field id)" ]; then
  echo "A browser window opens: sign in with Google, create or join an organization, then approve the CLI."
  echo "(If no browser opens, open the link printed below.)"
  $ss login || $ss login --device
  org_json=$($ss api /api/v1/org/current)
fi
org_id=$(printf '%s' "$org_json" | json_field id)
org_name=$(printf '%s' "$org_json" | json_field name)
echo "Signed in to organization $org_name ($org_id)."

if ! git rev-parse --verify -q HEAD >/dev/null; then
  echo "Skipping Stones: this repository has no commits yet. Commit something, then run this again."
  exit 1
fi

step "Enabling capture for your coding agents and importing past sessions"
$ss enable --yes --import-history --checkpoint-push-remote origin </dev/null || $ss enable --yes </dev/null
for agent in claude-code codex cursor copilot-cli factoryai-droid pi; do
  if out=$($ss import "$agent" </dev/null 2>&1); then
    printf '  %s: %s\n' "$agent" "$(printf '%s\n' "$out" | grep -v '^ *$' | tail -n 1)"
  fi
done

origin=$(git remote get-url origin 2>/dev/null || true)
slug=$(printf '%s' "$origin" | sed -n -e 's#^git@github\.com:\(.*\)$#\1#p' -e 's#^ssh://git@github\.com/\(.*\)$#\1#p' -e 's#^https://\([^@/]*@\)\{0,1\}github\.com/\(.*\)$#\2#p' | sed -e 's#\.git$##' -e 's#/$##')
if [ -z "$slug" ]; then
  echo
  echo "Capture is on. This repo has no GitHub origin, so it is not mirrored yet; push it to GitHub and run this again."
  echo "Your organization: $BASE"
  exit 0
fi
owner=${slug%%/*}
repo=${slug#*/}

step "Connecting github.com/$slug"
code=$(curl -s -o /dev/null -w '%{http_code}' "https://api.github.com/repos/$slug" || echo 000)
if [ "$code" != 200 ] && ! printf '%s' "$org_json" | grep -q '"github_token_set": *true'; then
  if command -v gh >/dev/null 2>&1 && gh auth status >/dev/null 2>&1; then
    $ss api -X POST /api/v1/org/github-token -f token="$(gh auth token)" >/dev/null
    echo "github.com/$slug is private: shared your GitHub CLI token with $org_name so it can mirror it."
  else
    echo "github.com/$slug looks private. Add a GitHub token in $BASE/settings/org so the mirror can read it."
  fi
fi
$ss repo mirror add "/gh/$owner/$repo" --cluster "$HOST" --timeout 10m </dev/null || echo "Mirroring did not finish yet; it keeps going on the server."

step "Uploading checkpoints"
if git for-each-ref --count=1 'refs/skipping-stones/' | grep -q .; then
  git push --no-verify -q origin 'refs/skipping-stones/*:refs/skipping-stones/*' && echo "Pushed checkpoints to origin." || echo "Could not push checkpoints yet; they go up with your next git push."
fi
$ss api -X POST /api/v1/org/sync -f repo="gh/$owner/$repo" >/dev/null 2>&1 || true

echo
echo "Skipping Stones is set up for $slug."
echo "Open: $BASE/$owner/$repo"
