#!/bin/bash
# BC-250 sd-restapi Autostart + Model Loader
# Usage: bash /home/fabian/start_sdcpp.sh

export AMD_VULKAN_ICD=RADV
export GGML_VK_FORCE_MAX_ALLOCATION_SIZE=536870912
export RADV_PERFTEST=nggc

BIN=/home/fabian/sd-restapi/build/bin/sdcpp-restapi
CONFIG=/home/fabian/sd-restapi/config.json
LOG=/tmp/sdcpp.log

# Kill existing
pkill -f sdcpp-restapi 2>/dev/null
sleep 1

# Start server
$BIN --config $CONFIG > $LOG 2>&1 &
PID=$!
echo "Started PID=$PID"

# Wait for server to be ready
for i in $(seq 1 30); do
  if curl -s http://localhost:8080/health >/dev/null 2>&1; then
    echo "Server ready after ${i}s"
    break
  fi
  sleep 1
done

# Load models
echo "Loading models..."
curl -s -X POST http://localhost:8080/models/load \
  -H "Content-Type: application/json" \
  -d '{
    "model_name": "z_image_turbo-Q5_K_S.gguf",
    "model_type": "diffusion",
    "llm": "Qwen_3_4b-Q8_0.gguf",
    "vae": "ae.safetensors"
  }'
echo ""
echo "=== READY ==="
echo "Web UI:  http://192.168.178.150:8080/ui/"
echo "API:     http://192.168.178.150:8080/"
echo "Log:     $LOG"
