@echo off
setlocal

:: Set this to 1 to force download/install even if git is in PATH
set "FORCE_DOWNLOAD=1"

:: Git download URL
set "GIT_URL=https://github.com/git-for-windows/git/releases/download/v2.50.0.windows.2/PortableGit-2.50.0.2-64-bit.7z.exe"

:: Extract archive name from URL
for %%A in ("%GIT_URL%") do (
    for %%B in (%%~nxA) do set "GIT_ARCHIVE=%%~B"
)

:: Paths
set "SCRIPT_DIR=%~dp0"
set "EXTRACT_DIR=%SCRIPT_DIR%gitportable"
set "LOG_FILE=%SCRIPT_DIR%git_setup.log"

echo Downloading Git, please wait... It may take a while.

:: Redirect output to log
call :main >> "%LOG_FILE%" 2>&1
goto :end

:main
echo ====================================================
echo Script started at %DATE% %TIME%
echo ====================================================
echo.

:: Check if git is in PATH unless forced
where git >nul 2>nul
if %errorlevel%==0 (
    if "%FORCE_DOWNLOAD%"=="0" (
        echo Git found in PATH, skipping download and extraction.
        goto :done
    ) else (
        echo Git found in PATH but FORCE_DOWNLOAD=1, continuing anyway.
    )
)

:: Check if extraction folder exists
if exist "%EXTRACT_DIR%\" (
    echo 'gitportable' folder already exists.
    if "%FORCE_DOWNLOAD%"=="0" (
        echo Skipping download and extraction.
        goto :done
    ) else (
        echo FORCE_DOWNLOAD=1, continuing with download and extraction.
    )
)

:: Download Git using available method
echo Downloading Git...
call :download "%GIT_URL%" "%SCRIPT_DIR%%GIT_ARCHIVE%"
if %errorlevel% neq 0 (
    echo Error: Download failed.
    goto :done
)
echo Download completed.

:: Clean extraction directory if forced
if "%FORCE_DOWNLOAD%"=="1" if exist "%EXTRACT_DIR%\" (
    echo Removing existing '%EXTRACT_DIR%' folder before extraction...
    rmdir /s /q "%EXTRACT_DIR%"
)

:: Create extraction directory
echo Preparing extraction folder...
mkdir "%EXTRACT_DIR%"
if %errorlevel% neq 0 (
    echo Error: Failed to create extraction directory.
    goto :done
)

:: Extract Git silently
echo Extracting Git...
"%SCRIPT_DIR%%GIT_ARCHIVE%" -o"%EXTRACT_DIR%" -y >nul 2>&1
if %errorlevel% neq 0 (
    echo Error: Extraction failed.
    goto :done
)
echo Extraction completed.

:: Cleanup
echo Cleaning up archive file...
del "%SCRIPT_DIR%%GIT_ARCHIVE%"
if %errorlevel% neq 0 (
    echo Warning: Failed to delete archive file.
)

echo.
echo Git Portable is ready in: %EXTRACT_DIR%
echo ====================================================
echo Script ended at %DATE% %TIME%
echo ====================================================

:done
exit /b

:download
:: Args: %1 - URL, %2 - Output file

:: Try PowerShell
where powershell >nul 2>nul
if %errorlevel%==0 (
    powershell -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri '%~1' -OutFile '%~2'"
    if %errorlevel%==0 exit /b 0
)

:: Try curl
where curl >nul 2>nul
if %errorlevel%==0 (
    curl -L -o "%~2" "%~1"
    if %errorlevel%==0 exit /b 0
)

:: Try bitsadmin
where bitsadmin >nul 2>nul
if %errorlevel%==0 (
    bitsadmin /transfer downloadjob /download /priority normal "%~1" "%~2"
    if %errorlevel%==0 exit /b 0
)

echo Error: No supported downloader (certutil, PowerShell, curl, bitsadmin) found.
exit /b 1

:end
echo.
echo Log saved to: %LOG_FILE%
echo Press Enter to close this window...
pause >nul
