#!/usr/bin/env sh
set -e

SKILLS_SOURCE="${SQUASHER_SKILLS_SOURCE:-https://github.com/squasher-ai/squasher/tree/main/.agents/skills}"
MCP_URL="${SQUASHER_MCP_URL:-https://mcp.squasher.ai/mcp}"
YES=0
DO_SKILLS=0
DO_MCP=0

usage() {
  cat <<'HELP'
Usage: install.sh [--agents] [--skills-only] [--mcp-only] [--remote] [-y|--yes]

Installs Squasher Agent Skills and registers the hosted Squasher MCP server
with detected agent CLIs. The hosted remote MCP URL is:
  https://mcp.squasher.ai/mcp
HELP
}

for arg in "$@"; do
  case "$arg" in
    --agents) DO_SKILLS=1; DO_MCP=1 ;;
    --skills-only) DO_SKILLS=1; DO_MCP=0 ;;
    --mcp-only) DO_SKILLS=0; DO_MCP=1 ;;
    --remote) ;;
    -y|--yes) YES=1 ;;
    -h|--help) usage; exit 0 ;;
    *) printf 'Unknown option: %s\n' "$arg" >&2; usage; exit 2 ;;
  esac
done

if [ "$DO_SKILLS$DO_MCP" = "00" ]; then
  DO_SKILLS=1
  DO_MCP=1
fi

run() {
  printf '+'
  for part in "$@"; do printf ' %s' "$part"; done
  printf '\n'

  if [ "$YES" = "1" ]; then
    "$@"
    return
  fi

  printf 'Run this command? [y/N] '
  if ! read -r answer; then answer=no; fi
  case "$answer" in
    y|Y|yes|YES) "$@" ;;
    *) printf 'skipped\n'; return 125 ;;
  esac
}

run_required() {
  run "$@" || {
    status="$?"
    if [ "$status" = "125" ]; then
      return 0
    fi
    return "$status"
  }
}

maybe_add_mcp() {
  client="$1"
  shift
  if command -v "$client" >/dev/null 2>&1; then
    MCP_ATTEMPTED=1
    if run "$@"; then
      MCP_CONFIGURED=1
    else
      status="$?"
      if [ "$status" != "125" ]; then
        printf 'Could not configure Squasher MCP with %s. Add the hosted MCP URL manually if needed.\n' "$client"
      fi
    fi
  fi
}

if [ "$DO_SKILLS" = "1" ]; then
  if command -v npx >/dev/null 2>&1; then
    for skill in squasher-onboard squasher-cli squasher-mcp; do
      run_required npx -y skills add "$SKILLS_SOURCE/$skill"
    done
  else
    printf 'npx was not found. Install Node.js or add skills manually from %s\n' "$SKILLS_SOURCE"
  fi
fi

if [ "$DO_MCP" = "1" ]; then
  MCP_ATTEMPTED=0
  MCP_CONFIGURED=0
  maybe_add_mcp codex codex mcp add squasher --url "$MCP_URL"
  maybe_add_mcp claude claude mcp add --transport http --scope user squasher "$MCP_URL"
  maybe_add_mcp gemini gemini mcp add --transport http squasher "$MCP_URL"
  maybe_add_mcp amp amp mcp add squasher "$MCP_URL"

  if [ "$MCP_CONFIGURED" = "0" ]; then
    if [ "$MCP_ATTEMPTED" = "0" ]; then
      printf 'No supported MCP CLI was found. Add this hosted MCP URL manually:\n  %s\n' "$MCP_URL"
    else
      printf 'No MCP client was configured. Add this hosted MCP URL manually:\n  %s\n' "$MCP_URL"
    fi
  fi
fi

if command -v squasher >/dev/null 2>&1; then
  printf 'Squasher CLI detected. Authenticate with: squasher login\n'
else
  printf 'Optional: install the Squasher CLI, then run squasher login for terminal workflows.\n'
fi

cat <<'NEXT'

Next prompt for your coding agent:
Use $squasher-onboard to install Squasher in this repo and verify one real runtime event.
NEXT
