#!/bin/bash
# Bezalel Evennia Setup — Install Evennia, create world, start services
set -e

echo "=== Setting up Evennia on Bezalel ==="

cd /root/wizards/bezalel

# Install Python virtualenv and Evennia
if [ ! -d "evennia/venv" ]; then
    mkdir -p evennia
    cd evennia
    python3 -m venv venv
    source venv/bin/activate
    pip install --upgrade pip
    pip install evennia>=2.0.0
else
    source evennia/venv/bin/activate
fi

# Initialize bezalel_world
cd /root/wizards/bezalel
if [ ! -d "bezalel_world/hermes-agent" ]; then
    cd bezalel_world
    evennia init bezalel_world --no-webclient 2>/dev/null || evennia init bezalel_world
fi

cd bezalel_world

# Configure ports: 4100 (telnet), 4101 (web)
# These don't conflict with Bezalel's Hermes on 8646
echo "" >> server/conf/settings.py
echo "SERVERNAME = 'bezalel_world'" >> server/conf/settings.py
echo "TELNET_PORTS = [4100]" >> server/conf/settings.py
echo "WEBSERVER_PORTS = [4101]" >> server/conf/settings.py
echo "WEBSOCKET_PORTS = [4102]" >> server/conf/settings.py

# Initialize and migrate
evennia migrate

# Start Evennia
evennia start

echo "=== Evennia installed and started ==="
evennia status

echo "Bezalel Evennia is live on:"
echo "  Telnet: 104.131.15.18:4100"
echo "  Web: http://104.131.15.18:4101"
echo "  WebSocket: ws://104.131.15.18:4102"
