@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 Remove any existing folder with the same repository name
echo "if you want a fresh install delete existing stable-diffusion-webui folder"
REM if exist "%repoName%" (
REM    echo Removing existing folder "%repoName%"
REM    rd /s /q "%repoName%"
REM )

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 COMMANDLINE_ARGS=%commandLineArgs%
        ) else (
            echo %%L
        )
    )) > "%filePath%.tmp"
    move /y "%filePath%.tmp" "%filePath%" > nul
)

REM Start the webui-user.bat file
cd "%repoName%"

git checkout 394ffa7b0a7fff3ec484bcd084e673a8b301ccc8

call webui-user.bat

REM Exit the batch file
exit
