#!/bin/sh
# Model discovery for COBOL - outputs discovered models to files

# Discover models from MODEL_ENDPOINT_N
echo "=== Model Discovery ===" > /tmp/models.txt
model_count=0
i=1
while [ $i -le 9999 ]; do
    eval endpoint=\$MODEL_ENDPOINT_$i
    [ -z "$endpoint" ] && break
    i=$((i + 1))

    echo "Discovering from: $endpoint" >> /tmp/models.txt
    response=$(curl -s "${endpoint}/models")

    # Extract first model ID
    model_id=$(echo "$response" | grep -o '"id":"[^"]*"' | head -1 | sed 's/"id":"//g' | sed 's/"//g')

    if [ -n "$model_id" ]; then
        echo "$model_id" >> /tmp/model_$model_count.txt
        echo "$endpoint" >> /tmp/endpoint_$model_count.txt
        echo "  - Discovered: $model_id" >> /tmp/models.txt
        model_count=$((model_count + 1))
    fi
done

# Discover TTS
i=1
while [ $i -le 9999 ]; do
    eval endpoint=\$TTS_ENDPOINT_$i
    [ -z "$endpoint" ] && break
    echo "$endpoint" > /tmp/tts_endpoint.txt
    echo "Discovering TTS from: $endpoint" >> /tmp/models.txt
    i=$((i + 1))
done

echo "Total models: $model_count" >> /tmp/models.txt
cat /tmp/models.txt
