#!/bin/bash
# LM Studio service wrapper.
#
# Lets lms server start handle daemon launch (passkey setup, etc.),
# optionally auto-loads a model (LMS_MODEL + LMS_CONTEXT_LENGTH),
# then monitors the llmster process and exits when it dies so systemd
# can restart the service.
set -e

LMS="${HOME}/.lmstudio/bin/lms"
LMS_PORT="${LMS_PORT:-1234}"
LMS_BIND="${LMS_BIND:-0.0.0.0}"
LMS_MODEL="${LMS_MODEL:-}"
LMS_CONTEXT_LENGTH="${LMS_CONTEXT_LENGTH:-65536}"
LMS_GPU_OFFLOAD="${LMS_GPU_OFFLOAD:-max}"

echo "Starting LM Studio server (port ${LMS_PORT}, bind ${LMS_BIND})..."
"$LMS" server start --port "$LMS_PORT" --bind "$LMS_BIND"

if [[ -n "$LMS_MODEL" ]]; then
    echo "Unloading any previously loaded models..."
    "$LMS" unload --all 2>/dev/null || true
    echo "Auto-loading model ${LMS_MODEL} (context ${LMS_CONTEXT_LENGTH}, gpu ${LMS_GPU_OFFLOAD})..."
    "$LMS" load "$LMS_MODEL" --context-length "$LMS_CONTEXT_LENGTH" --gpu "$LMS_GPU_OFFLOAD"
fi

# llmster bind sur 127.0.0.1 après chargement du backend Vulkan.
# socat proxy sur l'IP LAN (non-conflictuel avec le loopback) → llmster.
if [[ "$LMS_BIND" != "127.0.0.1" ]]; then
    LAN_IP=$(ip -4 addr show scope global | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | head -1)
    if [[ -n "$LAN_IP" ]]; then
        echo "Starting socat proxy ${LAN_IP}:${LMS_PORT} → 127.0.0.1:${LMS_PORT}..."
        socat TCP-LISTEN:"${LMS_PORT}",fork,bind="${LAN_IP}",reuseaddr TCP:127.0.0.1:"${LMS_PORT}" &
        SOCAT_PID=$!
        echo "socat PID: ${SOCAT_PID}"
    fi
fi

echo "Server started. Monitoring llmster daemon..."
while pgrep -u "$(id -un)" -x llmster > /dev/null 2>&1; do
    sleep 15
done

echo "llmster daemon stopped unexpectedly, exiting..."
exit 1
