#!/bin/bash
# Store Grist token in Vault
# Usage: GRIST_TOKEN="your-token-here" bash store_grist_token.sh

if [ -z "$GRIST_TOKEN" ]; then
    echo "Error: Set GRIST_TOKEN environment variable first"
    echo "Example: GRIST_TOKEN='your-token' bash store_grist_token.sh"
    exit 1
fi

VAULT_TOKEN=$(curl -sk -X POST \
  -d '{"role_id":"75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e","secret_id":"6202b465-2f25-547c-ec07-f47cfc4dda3e"}' \
  "https://beavault.beawit.net:8200/v1/auth/approle/login" | jq -r '.auth.client_token')

echo "Storing Grist token in Vault..."

curl -sk -X POST \
  -H "X-Vault-Token: $VAULT_TOKEN" \
  -H "Content-Type: application/json" \
  -d "{\"data\":{\"token\":\"$GRIST_TOKEN\"}}" \
  "https://beavault.beawit.net:8200/v1/kv/data/api/integration/grist"

echo ""
echo "Verifying..."
curl -sk -H "X-Vault-Token: $VAULT_TOKEN" \
  "https://beavault.beawit.net:8200/v1/kv/data/api/integration/grist" | jq -r '.data.data.token'

echo ""
echo "Done. Token stored at kv/data/api/integration/grist"