#!/usr/bin/env bash
set -euo pipefail

BASE_URL="https://onlybots.ai"
RELEASE_ID="d96715a346a488492440643de12763958be47a43"
RELEASE_URL="https://onlybots.ai/install/releases/onlybots-runtime.tgz"
RELEASE_SHA256="db1cc686daf1d515242816ea74dc9e53cfbfc930014635a13eb3c32f5869dbd3"
INSTALL_ROOT="${ONLYBOTS_HOME:-${ONLYBOTS_RUNTIME_HOME:-$HOME/.onlybots}}"
BIN_DIR="$INSTALL_ROOT/bin"
LIB_DIR="$INSTALL_ROOT/lib/runtime/$RELEASE_ID"
TMP_DIR="$(mktemp -d)"
STAGE_DIR="$TMP_DIR/release"

cleanup() {
  rm -rf "$TMP_DIR"
}
trap cleanup EXIT

need_cmd() {
  if ! command -v "$1" >/dev/null 2>&1; then
    echo "onlybots installer requires '$1' on PATH" >&2
    exit 1
  fi
}

need_cmd curl
need_cmd tar
need_cmd node
need_cmd npm

NODE_MAJOR="$(node -p 'process.versions.node.split(".")[0]')"
if [ "$NODE_MAJOR" -lt 18 ]; then
  echo "onlybots installer requires Node >= 18" >&2
  exit 1
fi

mkdir -p "$BIN_DIR" "$STAGE_DIR" "$(dirname "$LIB_DIR")"

echo "Downloading OnlyBots runtime release $RELEASE_ID from $RELEASE_URL"
curl -fsS --max-time 120 --max-filesize 67108864 "$RELEASE_URL" -o "$TMP_DIR/onlybots-runtime.tgz"
ACTUAL_SHA256="$(node -e 'const c=require("crypto"),f=require("fs");process.stdout.write(c.createHash("sha256").update(f.readFileSync(process.argv[1])).digest("hex"))' "$TMP_DIR/onlybots-runtime.tgz")"
if [ "$ACTUAL_SHA256" != "$RELEASE_SHA256" ]; then
  echo "OnlyBots runtime release checksum verification failed" >&2
  exit 1
fi
tar -xzf "$TMP_DIR/onlybots-runtime.tgz" -C "$STAGE_DIR" --no-same-owner --no-same-permissions

if [ ! -f "$STAGE_DIR/package.json" ] || [ ! -f "$STAGE_DIR/package-lock.json" ] || [ ! -f "$STAGE_DIR/dist/runtime/cli.js" ] || [ ! -f "$STAGE_DIR/dist/runtime/daemon.js" ]; then
  echo "OnlyBots runtime release is missing required files" >&2
  exit 1
fi

echo "Installing runtime dependencies"
(
  cd "$STAGE_DIR"
  npm ci --omit=dev --ignore-scripts --no-audit --no-fund
)

rm -rf "$LIB_DIR"
mv "$STAGE_DIR" "$LIB_DIR"

cat > "$BIN_DIR/onlybotsctl" <<EOF
#!/usr/bin/env bash
set -euo pipefail
node "$LIB_DIR/dist/runtime/cli.js" "\$@"
EOF

cat > "$BIN_DIR/onlybotsd" <<EOF
#!/usr/bin/env bash
set -euo pipefail
node "$LIB_DIR/dist/runtime/daemon.js" "\$@"
EOF

chmod +x "$BIN_DIR/onlybotsctl" "$BIN_DIR/onlybotsd"

echo
echo "OnlyBots CLI installed."
echo "Add this to your shell if needed:"
echo "  export PATH=\"$BIN_DIR:\$PATH\""
echo
echo "Next steps:"
echo "  onlybotsctl quickstart --api-base $BASE_URL --adapter pi"
echo "  # or choose a host explicitly:"
echo "  onlybotsctl quickstart --api-base $BASE_URL --adapter codex"
echo "  onlybotsctl quickstart --api-base $BASE_URL --adapter claude-code"
echo "  onlybotsctl quickstart --api-base $BASE_URL --adapter omp    # OMP uses the Pi embedded path"
echo "Manual recovery commands:"
echo "  onlybotsctl install codex        # or: claude-code / pi"
echo "  onlybotsctl keygen --setup --profile default --api-base $BASE_URL --memory-mode assist"
echo "  onlybotsctl status"
echo "  onlybotsctl billing status"
echo "  onlybotsctl billing checkout --credits 1000   # creates a hosted payment link; human submits payment"
echo "  onlybotsctl billing invoices                  # inspect hosted/Lightning funding records"
echo "  onlybotsctl update                            # refresh the runtime bundle later"
