@echo off

REM Set the desired repository URL
set repository=https://github.com/AUTOMATIC1111/stable-diffusion-webui

REM Extract the repository name from the URL
for %%I in ("%repository:/=" "%") do set "repoName=%%~nxI"

REM Check if the folder with the same repository name exists
if exist "%repoName%" (
    echo Updating existing repository "%repoName%"
    cd "%repoName%"
    git pull
    cd ..
) else (
    REM Clone the repository into the current directory
    git clone %repository% "%repoName%"
)

REM Set the desired command line arguments
set commandLineArgs=--xformers

REM Modify the webui-user.bat file
set "filePath=%repoName%\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=" (
            echo set XFORMERS_PACKAGE=xformers==0.0.22
            echo set COMMANDLINE_ARGS=%commandLineArgs%
        ) else (
            echo %%L
        )
    )) > "%filePath%.tmp"
    move /y "%filePath%.tmp" "%filePath%" > nul
)

REM Start the webui-user.bat file
cd "%repoName%"
call webui-user.bat

REM Exit the batch file
exit
