#!/bin/sh
# Termbell — installs the termbell CLI and coding-agent plugin 2.2.2 into ~/.termbell.
#   curl -fsSL https://openmoney.ai/install.sh | sh
set -eu
BASE="${TERMBELL_URL:-${OPENMONEY_URL:-https://openmoney.ai}}"
DEST="${TERMBELL_HOME:-$HOME/.termbell}"
LEGACY="$HOME/.openmoney"
command -v node >/dev/null 2>&1 || { echo "Termbell needs Node.js 18 or newer: https://nodejs.org" >&2; exit 1; }
node -e 'process.exit(Number(process.versions.node.split(".")[0]) >= 18 ? 0 : 1)' || { echo "Termbell needs Node.js 18 or newer." >&2; exit 1; }
command -v curl >/dev/null 2>&1 || { echo "curl is required." >&2; exit 1; }
echo "Installing Termbell 2.2.2 into $DEST"
mkdir -p "$DEST/bin" "$DEST/plugin"
for f in '.agents/plugins/marketplace.json' '.claude-plugin/marketplace.json' 'manifest.json' 'termbell/.claude-plugin/plugin.json' 'termbell/.codex-plugin/plugin.json' 'termbell/.mcp.json' 'termbell/CHANGELOG.md' 'termbell/LICENSE' 'termbell/README.md' 'termbell/bin/harness.d.mts' 'termbell/bin/harness.mjs' 'termbell/bin/qr.d.mts' 'termbell/bin/qr.mjs' 'termbell/bin/termbell.d.mts' 'termbell/bin/termbell.mjs' 'termbell/commands/override.md' 'termbell/commands/state.md' 'termbell/commands/subscribe.md' 'termbell/commands/updates.md' 'termbell/harnesses.json' 'termbell/hooks/base64.d.ts' 'termbell/hooks/downsample.ts' 'termbell/hooks/ed25519.ts' 'termbell/hooks/hooks.json' 'termbell/hooks/live.tsx' 'termbell/skills/team-updates/SKILL.md' 'termbell/types/index.d.ts'; do
  mkdir -p "$DEST/plugin/$(dirname "$f")"
  curl -fsSL "$BASE/integrations/plugin/$f" -o "$DEST/plugin/$f"
done
chmod +x "$DEST/plugin/termbell/bin/termbell.mjs"
cat > "$DEST/bin/termbell" <<EOF
#!/bin/sh
exec node "$DEST/plugin/termbell/bin/termbell.mjs" "\$@"
EOF
chmod +x "$DEST/bin/termbell"
# A pre-2.0 login is carried over by the CLI on its first run; start a fresh config only when there's neither.
[ -f "$DEST/config.json" ] || [ -f "$LEGACY/config.json" ] || printf '{\n  "url": "%s"\n}\n' "$BASE" > "$DEST/config.json"
[ ! -f "$DEST/config.json" ] || chmod 600 "$DEST/config.json"
LINKED=""
if [ -z "${TERMBELL_NO_LINK:-${OPENMONEY_NO_LINK:-}}" ]; then
  for d in "$HOME/.local/bin" "/usr/local/bin" "/opt/homebrew/bin"; do
    if [ -d "$d" ] && [ -w "$d" ]; then ln -sf "$DEST/bin/termbell" "$d/termbell" && LINKED="$d/termbell" && break; fi
  done
fi
UPGRADE=""
if [ -f "$LEGACY/bin/openmoney" ]; then
  # The old command keeps working, as termbell.
  printf '#!/bin/sh\nexec "%s" "$@"\n' "$DEST/bin/termbell" > "$LEGACY/bin/openmoney"
  chmod +x "$LEGACY/bin/openmoney"
  UPGRADE=1
fi
echo
if [ -n "$LINKED" ]; then CMD=termbell; echo "Linked $LINKED"; else CMD="$DEST/bin/termbell"; echo "Add $DEST/bin to your PATH, or run $CMD"; fi
if [ -n "$UPGRADE" ]; then
  echo
  echo "Upgrading from OpenMoney for Teams: your login carries over. Rewiring your coding agents for Termbell…"
  "$DEST/bin/termbell" setup auto || true
  echo
  "$DEST/bin/termbell" status || true
  echo
  echo "Restart your coding agents (in Claude Code: /reload-plugins) to load Termbell."
else
  echo
  echo "Next:"
  echo "  $CMD login                      # connect with your work Google account (or: $CMD pair CODE from https://openmoney.ai/connect)"
  echo "  $CMD setup auto                 # wire every coding agent on this machine; or: $CMD setup claude-code codex cursor …"
fi
