#!/bash
# Setup script for Supabase via Docker on LXC/VM
# Run this on a NEW LXC/VM (Ubuntu/Debian)

# 1. Update and install dependencies
sudo apt update && sudo apt upgrade -y
sudo apt install -y git curl docker.io docker-compose-v2

# 2. Clone Supabase Docker
git clone --depth 1 https://github.com/supabase/supabase
cd supabase/docker

# 3. Setup environment variables
cp .env.example .env

# Generate secure random strings for secrets
JWT_SECRET=$(openssl rand -base64 32)
ANON_KEY=$(openssl rand -base64 32)
SERVICE_ROLE_KEY=$(openssl rand -base64 32)
POSTGRES_PASSWORD=$(openssl rand -base64 16)

# Update .env (Basic replacement)
sed -i "s/JWT_SECRET=.*/JWT_SECRET=$JWT_SECRET/" .env
sed -i "s/ANON_KEY=.*/ANON_KEY=$ANON_KEY/" .env
sed -i "s/SERVICE_ROLE_KEY=.*/SERVICE_ROLE_KEY=$SERVICE_ROLE_KEY/" .env
sed -i "s/POSTGRES_PASSWORD=.*/POSTGRES_PASSWORD=$POSTGRES_PASSWORD/" .env

# 4. Start Supabase
sudo docker compose up -d

echo "--------------------------------------------------"
echo "Supabase is starting up!"
echo "Postgres Password: $POSTGRES_PASSWORD"
echo "API URL: http://$(hostname -I | awk '{print $1}'):8000"
echo "Studio (Dashboard): http://$(hostname -I | awk '{print $1}'):8001"
echo "--------------------------------------------------"
