#!/bin/bash
set -euo pipefail

echo "[gotrue] Waiting for PostgREST on localhost:3000..."

# 等待 postgrest 启动就绪
until curl -s -o /dev/null -w "%{http_code}" http://localhost:3000/ | grep -qE "^(200|401)$"; do
  echo "[gotrue] Waiting..."
  sleep 1.5
done

echo "[gotrue] PostgREST is ready. Starting GoTrue..."

# 必要环境变量（监听默认 127.0.0.1:9999 即可）
export GOTRUE_SITE_URL="http://localhost:3000"
export GOTRUE_JWT_SECRET="super-secret-jwt-token-with-at-least-32-characters-long"
export GOTRUE_DB_DRIVER="postgres"
export DATABASE_URL="postgres://supabase_auth_admin:postgres@localhost:5432/postgres"
export API_EXTERNAL_URL="http://localhost:8000/auth/v1"
export GOTRUE_API_HOST=127.0.0.1
export PORT=9999
export GOTRUE_EXTERNAL_EMAIL_ENABLED=true
export GOTRUE_MAILER_AUTOCONFIRM=true
export GOTRUE_MAILER_EXTERNAL_HOSTS=127.0.0.1
export GOTRUE_JWT_DEFAULT_GROUP_NAME=anon

# 启动 GoTrue（路径视你 build 的二进制实际位置）
exec /usr/local/bin/auth
