#!/bin/bash
# Local setup - prepare .env files
#
# This script runs LOCALLY to create .env files from examples.
# For server setup, use: ssh server 'cd ~/core_room/server && ./setup.sh'
#
# Usage:
#   ./setup.sh

set -e

# Change to parent directory (services are in ../service_name)
cd "$(dirname "$0")/.."

SERVICE_DIRS=()

# Find all service directories (have docker-compose.yml, exclude ctrl/nginx/server)
for dir in */; do
    dirname="${dir%/}"
    if [ -f "$dir/docker-compose.yml" ] && [ "$dirname" != "ctrl" ] && [ "$dirname" != "nginx" ] && [ "$dirname" != "server" ]; then
        SERVICE_DIRS+=("$dirname")
    fi
done

echo "=== Local Environment Setup ==="
echo ""

# Create .env files from examples
echo "Creating .env files from examples..."
for service in "${SERVICE_DIRS[@]}"; do
    if [ ! -f "$service/.env" ] && [ -f "$service/.env.example" ]; then
        cp "$service/.env.example" "$service/.env"
        echo "  Created $service/.env"
    elif [ -f "$service/.env" ]; then
        echo "  $service/.env already exists"
    fi
done

echo ""
echo "=== Local Setup Complete ==="
echo ""
echo "Local development:"
echo "  - Edit .env files for local values"
echo "  - Run: ./start.sh"
echo ""
echo "Server deployment:"
echo "  1. Deploy: ./deploy.sh"
echo "  2. On server: ssh server 'cd ~/core_room/server && ./setup.sh'"
