#!/bin/sh
# ==============================================================================
# Goa GEL Frontend - Docker Entrypoint
# ==============================================================================
# Injects runtime configuration from environment variables
# ==============================================================================

set -e

# Configuration directory in nginx html root
CONFIG_DIR="/usr/share/nginx/html/assets"
CONFIG_FILE="${CONFIG_DIR}/config.json"

# Default values (same as build-time defaults)
API_BASE_URL="${API_BASE_URL:-http://localhost:3001/api/v1}"

echo "=== Goa GEL Frontend Runtime Configuration ==="
echo "API_BASE_URL: ${API_BASE_URL}"
echo "=============================================="

# Ensure config directory exists
mkdir -p "${CONFIG_DIR}"

# Generate runtime configuration JSON
cat > "${CONFIG_FILE}" << EOF
{
  "apiBaseUrl": "${API_BASE_URL}"
}
EOF

echo "Runtime config written to ${CONFIG_FILE}"

# Execute the main command (nginx)
exec "$@"
