@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion
title ComfyUI Tam Kurulum - RTX 4090 - Ticari/NSFW/Full Stack

echo.
echo ================================================================
echo      ComfyUI TAM KURULUM (Ticari + NSFW + Video + LoRA)
echo      Hedef: D:\__sistem__\ProgramFiles\ComfyUi
echo      GPU:   NVIDIA RTX 4090 - CUDA 12.1
echo ================================================================
echo.
echo Bu script sunlari kuracaktir:
echo   - ComfyUI + ComfyUI Manager
echo   - PyTorch 2.3 (CUDA 12.1) + xformers
echo   - 15+ temel custom node (ReActor, IPAdapter, ControlNet, vs.)
echo   - Video nodelari (VideoHelperSuite, AnimateDiff, Hunyuan, LTX)
echo   - Face/detail nodelari (Impact Pack, Face Detailer, Ultimate Upscale)
echo.
pause

net session >nul 2>&1
if %errorLevel% neq 0 (
    echo [UYARI] Yonetici olarak calistirin. Sag tik - "Yonetici olarak calistir"
    pause
    exit /b 1
)

set "INSTALL_DIR=D:\__sistem__\ProgramFiles\ComfyUi"
set "COMFY_DIR=%INSTALL_DIR%\ComfyUI"

echo [1/6] Klasor hazirligi...
if not exist "D:\__sistem__\ProgramFiles" mkdir "D:\__sistem__\ProgramFiles"
if not exist "%INSTALL_DIR%" mkdir "%INSTALL_DIR%"
cd /d "%INSTALL_DIR%"

echo.
echo [2/6] Git ve Python kontrolu...
where git >nul 2>&1 || (echo [HATA] Git kurun: https://git-scm.com/download/win && pause && exit /b 1)
where python >nul 2>&1 || (echo [HATA] Python 3.10/3.11 kurun: https://www.python.org/downloads/ && pause && exit /b 1)
python --version
echo [OK]

echo.
echo [3/6] ComfyUI klonlaniyor...
if exist "%COMFY_DIR%\.git" (
    cd "%COMFY_DIR%" && git pull
) else (
    git clone https://github.com/comfyanonymous/ComfyUI.git
)

echo.
echo [4/6] Python sanal ortami + PyTorch CUDA 12.1...
cd /d "%COMFY_DIR%"
if not exist "venv" python -m venv venv
call venv\Scripts\activate.bat
python -m pip install --upgrade pip wheel setuptools
echo      PyTorch 2.6 + CUDA 12.6 indiriliyor (yaklasik 2.5 GB)...
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
pip install -r requirements.txt
echo      xformers (hizlandirma) yukleniyor...
pip install xformers --index-url https://download.pytorch.org/whl/cu126

echo.
echo [5/6] Custom nodelar kuruluyor...
cd "%COMFY_DIR%\custom_nodes"

:: Manager (mutlaka ilk)
if not exist "ComfyUI-Manager" git clone https://github.com/ltdrdata/ComfyUI-Manager.git

:: Temel yardimcilar
if not exist "rgthree-comfy" git clone https://github.com/rgthree/rgthree-comfy.git
if not exist "was-node-suite-comfyui" git clone https://github.com/WASasquatch/was-node-suite-comfyui.git
if not exist "ComfyUI-Custom-Scripts" git clone https://github.com/pythongosssss/ComfyUI-Custom-Scripts.git
if not exist "ComfyUI-Crystools" git clone https://github.com/crystian/ComfyUI-Crystools.git
if not exist "efficiency-nodes-comfyui" git clone https://github.com/jags111/efficiency-nodes-comfyui.git

:: Detay/Yuz/Upscale
if not exist "ComfyUI-Impact-Pack" git clone https://github.com/ltdrdata/ComfyUI-Impact-Pack.git
if not exist "ComfyUI-Impact-Subpack" git clone https://github.com/ltdrdata/ComfyUI-Impact-Subpack.git
if not exist "ComfyUI_UltimateSDUpscale" git clone https://github.com/ssitu/ComfyUI_UltimateSDUpscale.git
if not exist "ComfyUI_essentials" git clone https://github.com/cubiq/ComfyUI_essentials.git

:: ControlNet + IPAdapter
if not exist "ComfyUI_IPAdapter_plus" git clone https://github.com/cubiq/ComfyUI_IPAdapter_plus.git
if not exist "comfyui_controlnet_aux" git clone https://github.com/Fannovel16/comfyui_controlnet_aux.git
if not exist "ComfyUI-Advanced-ControlNet" git clone https://github.com/Kosinkadink/ComfyUI-Advanced-ControlNet.git

:: Face swap (ReActor - NSFW icin guvenlik filtresiz fork)
if not exist "comfyui-reactor-node" git clone https://github.com/Gourieff/comfyui-reactor-node.git

:: Video
if not exist "ComfyUI-VideoHelperSuite" git clone https://github.com/Kosinkadink/ComfyUI-VideoHelperSuite.git
if not exist "ComfyUI-AnimateDiff-Evolved" git clone https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved.git
if not exist "ComfyUI-HunyuanVideoWrapper" git clone https://github.com/kijai/ComfyUI-HunyuanVideoWrapper.git
if not exist "ComfyUI-LTXVideo" git clone https://github.com/Lightricks/ComfyUI-LTXVideo.git
if not exist "ComfyUI-WanVideoWrapper" git clone https://github.com/kijai/ComfyUI-WanVideoWrapper.git
if not exist "ComfyUI-Frame-Interpolation" git clone https://github.com/Fannovel16/ComfyUI-Frame-Interpolation.git

:: GGUF (kuantize model destegi - VRAM tasarrufu)
if not exist "ComfyUI-GGUF" git clone https://github.com/city96/ComfyUI-GGUF.git

:: Custom node bagimliliklari
echo.
echo      Custom node bagimliliklari yukleniyor...
for /d %%D in ("%COMFY_DIR%\custom_nodes\*") do (
    if exist "%%D\requirements.txt" (
        echo      -^> %%~nxD
        pip install -r "%%D\requirements.txt" >nul 2>&1
    )
    if exist "%%D\install.py" (
        python "%%D\install.py" >nul 2>&1
    )
)

:: ReActor icin insightface (ozel kurulum)
pip install insightface==0.7.3 onnx onnxruntime-gpu

echo.
echo [6/6] Model klasor yapisi olusturuluyor...
cd "%COMFY_DIR%\models"
for %%F in (checkpoints loras vae controlnet upscale_models embeddings ipadapter clip_vision unet diffusion_models vae_approx insightface facerestore_models sams ultralytics ultralytics\bbox ultralytics\segm clip text_encoders) do (
    if not exist "%%F" mkdir "%%F"
)

echo.
echo ================================================================
echo [TAMAMLANDI] Kurulum basarili!
echo ================================================================
echo.
echo Sonraki adim: "2_MODEL_INDIR.bat" scriptini calistirin.
echo Baslatmak icin: "3_COMFYUI_BASLAT.bat"
echo.
pause
endlocal
