#!/bin/bash
# QwenClaw Rig Service Startup Script
# Starts the Rig AI agent service

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
RIG_DIR="$SCRIPT_DIR/../rig-service"

echo "🦀 Starting QwenClaw Rig Service..."

cd "$RIG_DIR"

# Check if built
if [ ! -f "target/release/qwenclaw-rig" ]; then
    echo "⚠️  Rig service not built. Building now..."
    cargo build --release
fi

# Load environment variables
if [ -f ".env" ]; then
    export $(cat .env | grep -v '^#' | xargs)
fi

# Set defaults if not set
export RIG_HOST="${RIG_HOST:-127.0.0.1}"
export RIG_PORT="${RIG_PORT:-8080}"
export OPENAI_API_KEY="${OPENAI_API_KEY:-}"

echo "🚀 Starting Rig service on http://$RIG_HOST:$RIG_PORT"

# Start service
exec cargo run --release
