#!/bin/sh
# dice.place for Claude Code, set up in one go:
#
#   curl -fsSL https://dice.place/setup.sh | sh
#
# It adds the tables' server to Claude Code as an MCP server (for every folder: the user's scope) and puts the two
# skills in ~/.claude/skills: the host's (run a game at a real table) and the author's (make a game of your own).
# Run again, it fetches the skills anew and leaves a server already added as it is.
#
#   DICEPLACE_MASTER   where the skills are fetched from (https://dice.place)
#   DICEPLACE_TABLES   the tables' server (https://test.dice.place)
set -eu
MASTER=${DICEPLACE_MASTER:-https://dice.place}
TABLES=${DICEPLACE_TABLES:-https://test.dice.place}

command -v claude >/dev/null 2>&1 || { echo "Claude Code is not installed here (no \`claude\` command): see https://claude.com/claude-code" >&2; exit 1; }
command -v curl >/dev/null 2>&1 || { echo "curl is needed to fetch the skills" >&2; exit 1; }

for skill in diceplace-host diceplace-author; do
  dir="$HOME/.claude/skills/$skill"
  mkdir -p "$dir"
  # (fetched beside the file and moved over it, so a fetch that fails leaves the skill that was there)
  curl -fsSL "$MASTER/skills/$skill/SKILL.md" -o "$dir/SKILL.md.new"
  mv "$dir/SKILL.md.new" "$dir/SKILL.md"
  echo "skill $skill: $dir/SKILL.md"
done

if claude mcp get diceplace >/dev/null 2>&1; then
  echo "MCP server diceplace: already added, left as it is"
else
  claude mcp add --scope user --transport http diceplace "$TABLES/mcp" >/dev/null
  echo "MCP server diceplace: $TABLES/mcp"
fi

echo
echo "Done. Start a new Claude Code session and say: Host a dice.place table with the game watch."
