@echo off

set VENV_FOLDER=.\stable-diffusion-webui\venv

if exist "%VENV_FOLDER%\Scripts\activate.bat" (
    call "%VENV_FOLDER%\Scripts\activate.bat"
) else (
    echo The virtual environment does not exist in the current folder.
    echo Please create the virtual environment before running this script.
)

REM pip install -U --pre xformers

REM Set the target directory where the repository will be cloned
set "targetDir=.\stable-diffusion-webui\extensions\sd_dreambooth_extension"

REM Clone the Git repository into the target directory
git clone https://github.com/d8ahazard/sd_dreambooth_extension "%targetDir%"

REM Check if the cloning process was successful
if %errorlevel% equ 0 (
    echo Repository cloned successfully!
) else (
    echo Error occurred during cloning. Please check the Git URL and try again.
)

cd stable-diffusion-webui\extensions\sd_dreambooth_extension

git pull

git checkout dc413a14379b165355502d9f65856c40a4bb5b6f

cd ..
cd ..

dir

set commandLineArgs=--xformers --skip-install

set commandLineArgs=--xformers

REM Modify the webui-user.bat file
set "filePath=webui-user.bat"

REM Check if the file exists
if exist "%filePath%" (
    echo Modifying "%filePath%"
    (for /f "usebackq delims=" %%L in ("%filePath%") do (
        set "line=%%L"
        if "%%L"=="set COMMANDLINE_ARGS=--xformers" (
            echo set COMMANDLINE_ARGS=%commandLineArgs%
        ) else (
            echo %%L
        )
    )) > "%filePath%.tmp"
    move /y "%filePath%.tmp" "%filePath%" > nul
)

cd stable-diffusion-webui\extensions\sd_dreambooth_extension

pip install xformers==0.0.22

pip install -r requirements.txt

pip uninstall httpx --yes

pip install httpx==0.24.1

pip install gradio_client==0.8.1

REM pip install https://github.com/jllllll/bitsandbytes-windows-webui/releases/download/wheels/bitsandbytes-0.41.1-py3-none-win_amd64.whl --force-reinstall

echo All completed. You can now start your Automatic1111 Web UI from webui-user.bat inside stable-diffusion-webui folder

pause