@echo off
chcp 65001 >nul
title LTX-Desktop-WanGP - Guncelleme
color 0E

REM ============================================================
REM  LTX-Desktop-WanGP Guncelleme Scripti
REM  Git pull + pnpm install + Wan2GP guncelleme
REM ============================================================

set "KOK=D:\__sistem__\Masaustu\FikoYT.Yeni.Cag\video-uretim"
set "LTX=%KOK%\ltx-desktop-wangp"
set "REPO=%LTX%\LTX-Desktop-WanGP"

echo =========================================================
echo    LTX-Desktop-WanGP Guncelleniyor
echo =========================================================
echo.

if not exist "%REPO%\.git" (
    color 0C
    echo [HATA] LTX-Desktop-WanGP reposu bulunamadi!
    echo   Beklenen: %REPO%
    echo.
    echo Once "LTX-Desktop-WanGP-Kur.bat" dosyasini calistirin.
    pause
    exit /b 1
)

cd /d "%REPO%"

REM --- Ana repo guncelle ---
echo [1/3] LTX-Desktop-WanGP guncelleniyor (git pull)...
git pull
if errorlevel 1 (
    echo [UYARI] git pull basarisiz oldu. Devam ediliyor...
)
echo.

REM --- Node.js bagimliliklari guncelle ---
echo [2/3] Node.js bagimliliklari guncelleniyor (pnpm install)...
call pnpm install
if errorlevel 1 (
    echo [UYARI] pnpm install basarisiz oldu.
)
echo.

REM --- Wan2GP alt reposu guncelle ---
echo [3/3] Wan2GP backend guncelleniyor...
if exist "%REPO%\Wan2GP\.git" (
    cd /d "%REPO%\Wan2GP"
    git pull
    if errorlevel 1 (
        echo [UYARI] Wan2GP git pull basarisiz oldu.
    )
    cd /d "%REPO%"
) else if exist "%REPO%\Wan2GP\scripts\update.bat" (
    call "%REPO%\Wan2GP\scripts\update.bat"
) else (
    echo [BILGI] Wan2GP alt reposu bulunamadi, atlaniyor.
)
echo.

echo =========================================================
echo    Guncelleme tamamlandi.
echo =========================================================
echo.

REM ============================================================
REM  CONFIG KORUMA - git pull sonrasi guvenli ayarlari koru
REM  (attention_mode=sage2 veya compile=transformer geri gelirse)
REM ============================================================
set "WGP_CONFIG=%REPO%\Wan2GP\wgp_config.json"
set "LTX_SETTINGS=%REPO%\settings.json"
set "VENV_PY=%REPO%\backend\.venv\Scripts\python.exe"
if not exist "%VENV_PY%" set "VENV_PY=python"

if exist "%WGP_CONFIG%" (
    echo [*] Config dosyalari guvenli varsayilanlara aliniyor...
    "%VENV_PY%" -c "import json; p=r'%WGP_CONFIG%'; c=json.load(open(p,'r',encoding='utf-8')); c.setdefault('attention_mode','sage'); c.setdefault('compile',''); c.setdefault('video_profile',4); c.setdefault('image_profile',4); c.setdefault('audio_profile',4);" 2>nul
    REM Eger sage2 geri gelmisse ve VS BuildTools yoksa sage'e dusur
    "%VENV_PY%" -c "import json,os; p=r'%WGP_CONFIG%'; c=json.load(open(p,'r',encoding='utf-8')); vs=any(os.path.exists(v) for v in [r'C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat', r'C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat', r'C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Auxiliary\Build\vcvars64.bat']); sa2=os.path.exists(os.path.join(r'%REPO%','backend','.venv','Lib','site-packages','sageattention')) and any('2.' in f for f in os.listdir(os.path.join(r'%REPO%','backend','.venv','Lib','site-packages')) if f.startswith('sageattention-')); orig=c.get('attention_mode','sage'); c['attention_mode']='sage2' if (vs and sa2) else ('sage' if orig=='sage2' else orig); c['compile']='' if c.get('compile')=='transformer' else c.get('compile',''); json.dump(c,open(p,'w',encoding='utf-8'),indent=4); print('  [OK] wgp_config.json: attention=',c['attention_mode'],'/ compile=',repr(c['compile']))"
)
if exist "%LTX_SETTINGS%" (
    "%VENV_PY%" -c "import json; p=r'%LTX_SETTINGS%'; c=json.load(open(p,'r',encoding='utf-8')); c['use_torch_compile']=False; json.dump(c,open(p,'w',encoding='utf-8'),indent=2); print('  [OK] settings.json: use_torch_compile=False')"
)
echo.

echo Baslatmak icin: LTX-Desktop-WanGP-Baslat.bat
echo.
pause
