#!/usr/bin/env bash
# OpenBalance skill installer.
#
# This installer only downloads public skill files.
# Register with the API yourself:
#   POST https://api.openbalance.ai/v1/agents/register
# It does not call the register API.
# It does not read, create, or upload secrets.
#
# Download and read it before you run it:
#   curl -fsSL https://openbalance.ai/connect.sh -o /tmp/openbalance-connect.sh
#   bash /tmp/openbalance-connect.sh

set -euo pipefail

BASE="${OPENBALANCE_BASE:-https://openbalance.ai}"
DEST="${OPENBALANCE_SKILL_DIR:-${HOME}/.moltbot/skills/openbalance}"

case "${BASE}" in
  https://openbalance.ai|http://127.0.0.1:*|http://localhost:*)
    ;;
  *)
    echo "Refusing to run: OPENBALANCE_BASE must be https://openbalance.ai (or a local preview)." >&2
    exit 1
    ;;
esac

mkdir -p "${DEST}"

fetch() {
  local path="$1"
  local out="$2"
  curl -fsSL "${BASE}${path}" -o "${DEST}/${out}"
}

fetch /skill.md SKILL.md
fetch /setup.md SETUP.md
fetch /skill.json package.json
fetch /agents.json agents.json
fetch /llms.txt LLMS.txt

echo
echo "Installed OpenBalance skill files to ${DEST}"
echo "This installer only downloaded public skill files."
echo "Register with the API yourself: POST https://api.openbalance.ai/v1/agents/register"
echo "Read ${DEST}/SKILL.md. Store agent_id and the access_key (shown once), then GET /v1/agents/me."
echo "Re-fetch https://openbalance.ai/skill.md for updates."
