@echo off
setlocal EnableDelayedExpansion

set UV_SKIP_WHEEL_FILENAME_CHECK=1
set UV_LINK_MODE=copy

echo ComfyUI Installation Script
echo ==========================
echo.

REM Check if ComfyUI and venv already exist
if exist "ComfyUI\venv" (
    echo Existing ComfyUI installation detected. Proceeding with update...
    echo.
    cd ComfyUI
    call .\venv\Scripts\activate.bat
    goto UPDATE_EXISTING
)

:PYTHON_VERSION
echo Please select your Python version:
echo 1. Python 3.10
echo 2. Python 3.11
echo 3. Python 3.12
echo 4. Python 3.13
echo.
set /p PYTHON_CHOICE="Enter your choice (1, 2, 3 or 4): "

if "%PYTHON_CHOICE%"=="1" (
    set PYTHON_VERSION=3.10
    set PY_VERSION_ARG=-3.10
) else if "%PYTHON_CHOICE%"=="2" (
    set PYTHON_VERSION=3.11
    set PY_VERSION_ARG=-3.11
) else if "%PYTHON_CHOICE%"=="3" (
    set PYTHON_VERSION=3.12
    set PY_VERSION_ARG=-3.12
) else if "%PYTHON_CHOICE%"=="4" (
    set PYTHON_VERSION=3.13
    set PY_VERSION_ARG=-3.13
) else (
    echo Invalid choice. Please try again.
    goto PYTHON_VERSION
)

echo.
echo Installing ComfyUI with Python %PYTHON_VERSION%...
echo Both Flash Attention and Sage Attention will be installed.
echo Make sure that you have installed Python %PYTHON_VERSION%, Git, C++ tools, FFmpeg
echo Full tutorial to install these : https://youtu.be/-NjNy7afOQ0
echo.

git clone --depth 1 https://github.com/Comfy-Org/ComfyUI

cd ComfyUI

py --version >nul 2>&1
if "%ERRORLEVEL%" == "0" (
    echo Python launcher is available. Generating Python %PYTHON_VERSION% VENV
    py %PY_VERSION_ARG% -m venv venv
) else (
    echo Python launcher is not available, generating VENV with default Python. Make sure that it is %PYTHON_VERSION%
    python -m venv venv
)

call .\venv\Scripts\activate.bat

:UPDATE_EXISTING

git stash

git reset --hard

git pull --force

python -m pip install --upgrade pip

pip install uv

uv pip install torch==2.9.1 torchvision torchaudio==2.9.1 --index-url https://download.pytorch.org/whl/cu130

mkdir custom_nodes

cd custom_nodes

git clone --depth 1 https://github.com/ltdrdata/ComfyUI-Manager

git clone --depth 1 https://github.com/cubiq/ComfyUI_IPAdapter_plus

git clone --depth 1 https://github.com/city96/ComfyUI-GGUF

git clone --depth 1 https://github.com/ClownsharkBatwing/RES4LYF

if exist "ComfyUI-QuantOps" rmdir /s /q "ComfyUI-QuantOps"
if exist "ComfyUI-QuantOps" (
    echo Failed to remove existing ComfyUI-QuantOps folder.
    exit /b 1
)

git clone --depth 1 https://github.com/FurkanGozukara/ComfyUI-QuantOps || exit /b 1

git clone --depth 1 https://github.com/Fannovel16/ComfyUI-Frame-Interpolation

git clone --depth 1 https://github.com/FurkanGozukara/ComfyUI-TeaCache

git clone --depth 1 https://github.com/Fannovel16/comfyui_controlnet_aux

cd ComfyUI-TeaCache

git remote set-url origin https://github.com/FurkanGozukara/ComfyUI-TeaCache

git stash

git reset --hard

git pull --force

cd ..

cd ComfyUI-Manager

git stash

git reset --hard

git pull --force

uv pip install -r requirements.txt

cd ..

cd ComfyUI_IPAdapter_plus

git stash

git reset --hard

git pull --force

cd ..

cd ComfyUI-GGUF

git stash

git reset --hard

git pull --force

uv pip install -r requirements.txt

cd ..

cd RES4LYF

git stash

git reset --hard

git pull --force

uv pip install -r requirements.txt

cd ..

cd ..

echo Installing ComfyUI requirements...

uv pip install -r requirements.txt

pip uninstall xformers -y

if "%PYTHON_VERSION%"=="3.10" (
	uv pip install https://huggingface.co/MonsterMMORPG/Wan_GGUF/resolve/main/flash_attn-2.8.3+torch2.9.1.cuda13.1-cp310-cp310-win_amd64.whl
	uv pip install https://huggingface.co/MonsterMMORPG/Wan_GGUF/resolve/main/insightface-0.7.3-cp310-cp310-win_amd64.whl
    uv pip install https://huggingface.co/MonsterMMORPG/Wan_GGUF/resolve/main/deepspeed-0.16.5-cp310-cp310-win_amd64.whl	
) else if "%PYTHON_VERSION%"=="3.11" (
    uv pip install https://huggingface.co/MonsterMMORPG/Wan_GGUF/resolve/main/flash_attn-2.8.3+torch2.9.1.cuda13.1-cp311-cp311-win_amd64.whl
	uv pip install https://huggingface.co/MonsterMMORPG/Wan_GGUF/resolve/main/insightface-0.7.3-cp311-cp311-win_amd64.whl
    uv pip install https://huggingface.co/MonsterMMORPG/Wan_GGUF/resolve/main/deepspeed-0.16.5-cp311-cp311-win_amd64.whl
) else if "%PYTHON_VERSION%"=="3.12" (
    uv pip install https://huggingface.co/MonsterMMORPG/Wan_GGUF/resolve/main/flash_attn-2.8.3+torch2.9.1.cuda13.1-cp312-cp312-win_amd64.whl
	uv pip install https://huggingface.co/MonsterMMORPG/Wan_GGUF/resolve/main/insightface-0.7.3-cp312-cp312-win_amd64.whl
    uv pip install https://huggingface.co/MonsterMMORPG/Wan_GGUF/resolve/main/deepspeed-0.16.5-cp312-cp312-win_amd64.whl
) else if "%PYTHON_VERSION%"=="3.13" (
    uv pip install https://huggingface.co/MonsterMMORPG/Wan_GGUF/resolve/main/flash_attn-2.8.3+torch2.9.1.cuda13.1-cp313-cp313-win_amd64.whl
	uv pip install https://huggingface.co/MonsterMMORPG/Wan_GGUF/resolve/main/insightface-0.7.3-cp313-cp313-win_amd64.whl
)

uv pip install https://huggingface.co/MonsterMMORPG/Wan_GGUF/resolve/main/xformers-0.0.34+41531cee.d20260109-cp39-abi3-win_amd64.whl
uv pip install https://huggingface.co/MonsterMMORPG/Wan_GGUF/resolve/main/sageattention-2.2.0+torch2.9.1.cuda13.1-cp39-abi3-win_amd64.whl

cd ..

echo Installing Shared requirements...

uv pip install -r requirements_Comfy.txt

echo.
echo =====================================================
echo The following libraries were installed in the venv:
echo =====================================================
echo.
echo =====================================================
echo Installation completed successfully! If you had previously installed, all packages updated to latest
echo Python %PYTHON_VERSION% virtual environment created and configured.
echo We have installed and or updated Flash Attention 2.8.3, Sage Attention 2.2.0, InsightFace, DeepSpeed (Only Python 3.13 lacks this), xFormers 0.0.33, Accelerate, Diffusers, Triton 3.4, ComfyUI-Manager, ComfyUI_IPAdapter_plus, ComfyUI-GGUF, ComfyUI-Impact-Pack
echo =====================================================
echo.

REM Pause to keep the command prompt open
pause
