#!/bin/sh

set -e

# Set default values for environment variables
VITE_MCPX_SERVER_URL="${VITE_MCPX_SERVER_URL:-}"
VITE_MCPX_SERVER_PORT="${VITE_MCPX_SERVER_PORT:-9000}"
VITE_WS_URL="${VITE_WS_URL:-}"
VITE_AUTH0_DOMAIN="${VITE_AUTH0_DOMAIN:-}"
VITE_AUTH0_CLIENT_ID="${VITE_AUTH0_CLIENT_ID:-}"
VITE_AUTH0_AUDIENCE="${VITE_AUTH0_AUDIENCE:-mcpx-webapp}"
VITE_ENABLE_LOGIN="${VITE_ENABLE_LOGIN:-false}"
VITE_OAUTH_CALLBACK_BASE_URL="${VITE_OAUTH_CALLBACK_BASE_URL:-}"
VITE_ENABLE_ENTERPRISE="${VITE_ENABLE_ENTERPRISE:-false}"
VITE_AUTH_BFF_URL="${VITE_AUTH_BFF_URL:-}"
VITE_ENABLE_PERMISSIONS="${VITE_ENABLE_PERMISSIONS:-false}"
VITE_ENABLE_DYNAMIC_CAPABILITIES="${VITE_ENABLE_DYNAMIC_CAPABILITIES:-false}"
VITE_ADD_SERVER_CHECKBOX="${VITE_ADD_SERVER_CHECKBOX:-}"
VITE_ENABLE_CAPABILITIES_UI="${VITE_ENABLE_CAPABILITIES_UI:-false}"

echo "Generating config.json from environment variables..."

# Create config.json
CONFIG_FILE="${HOME}/packages/ui/config.json"
mkdir -p "$(dirname "$CONFIG_FILE")"

cat > "$CONFIG_FILE" << EOF
{
  "VITE_MCPX_SERVER_URL": "${VITE_MCPX_SERVER_URL}",
  "VITE_MCPX_SERVER_PORT": "${VITE_MCPX_SERVER_PORT}",
  "VITE_WS_URL": "${VITE_WS_URL}",
  "VITE_AUTH0_DOMAIN": "${VITE_AUTH0_DOMAIN}",
  "VITE_AUTH0_CLIENT_ID": "${VITE_AUTH0_CLIENT_ID}",
  "VITE_AUTH0_AUDIENCE": "${VITE_AUTH0_AUDIENCE}",
  "VITE_ENABLE_LOGIN": "${VITE_ENABLE_LOGIN}",
  "VITE_OAUTH_CALLBACK_BASE_URL": "${VITE_OAUTH_CALLBACK_BASE_URL}",
  "VITE_ENABLE_ENTERPRISE": "${VITE_ENABLE_ENTERPRISE}",
  "VITE_AUTH_BFF_URL": "${VITE_AUTH_BFF_URL}",
  "VITE_ENABLE_PERMISSIONS": "${VITE_ENABLE_PERMISSIONS}",
  "VITE_ENABLE_DYNAMIC_CAPABILITIES": "${VITE_ENABLE_DYNAMIC_CAPABILITIES}",
  "VITE_ADD_SERVER_CHECKBOX": "${VITE_ADD_SERVER_CHECKBOX}",
  "VITE_ENABLE_CAPABILITIES_UI": "${VITE_ENABLE_CAPABILITIES_UI}"
}
EOF

echo "Config.json created successfully"
