StartBot.bat

@echo off
:: --- ADMIN AUTO-ELEVATE (Molt holt sich automatisch Admin-Rechte) ---
net session >nul 2>&1
if %errorlevel% neq 0 (
powershell -Command "Start-Process '%~f0' -Verb RunAs"
exit /b
)
:: ------------------------------------------------------------------

title Molt System-Wächter (Fix)
cd /d "%~dp0"

:: 1. Umgebungsvariablen zwingend setzen
set OLLAMA_HOST=127.0.0.1:11434
set OLLAMA_MODELS=%~dp0..\Ollama_Modelle
set OLLAMA_KEEP_ALIVE=-1

:: 2. Junction Point sicherstellen
set "LINK_CREATED=0"
if not exist "%USERPROFILE%\.ollama" (
if not exist "%~dp0.ollama" mkdir "%~dp0.ollama"
mklink /j "%USERPROFILE%\.ollama" "%~dp0.ollama"
set "LINK_CREATED=1"
echo [System] Temporärer Junction Point erstellt
)

:: 3. Ollama starten (Logs werden in ollama_log.txt umgeleitet)
echo [System] Starte Ollama (Hintergrund-Modus)...
start "Ollama Server" /B "%~dp0..\Ollama_Portable\ollama.exe" serve > "%~dp0ollama_log.txt" 2>&1

:: 4. Warten bis die API wirklich antwortet (nicht nur der Port offen ist)
echo Warte auf API-Antwort...
:check_loop
timeout /t 3 >nul
:: Wir fragen per 'curl' kurz bei der API nach, ob sie schon bereit ist
curl -s http://127.0.0.1:11434/api/tags >nul
if %errorlevel% neq 0 goto check_loop

echo [System] KI ist ONLINE!
echo [System] Starte Molt-Wächter-Bot mit Admin-Rechten...

:: 5. Bot starten
"%~dp0..\node.exe" Bot.js

:: 6. CLEANUP (Wird erst ausgeführt, wenn Bot beendet wird)
echo [System] Beende Sitzung und räume auf...
taskkill /F /IM ollama.exe >nul 2>&1

if "%LINK_CREATED%"=="1" (
echo [System] Entferne temporären Junction Point...
rmdir "%USERPROFILE%\.ollama"
)

echo [System] Stick kann jetzt sicher entfernt werden.
pause