@echo off
setlocal

:: --------- Define Paths (without trailing backslashes) ---------
set "BASE_DIR=%~dp0"
set "BASE_DIR=%BASE_DIR:~0,-1%"

set "LOG_FILE=%BASE_DIR%\update.log"
set "GIT_PORTABLE=%BASE_DIR%\gitportable\bin\git.exe"
set "MAIN_REPO_DIR=%BASE_DIR%"
set "SCRIPTS_TEMP_DIR=%BASE_DIR%\temp_scripts"
set "SOUNDS_DIR=%BASE_DIR%\cosmic rage\worlds\cosmic rage\sounds"
set "REPO_SUBFOLDER=ogg"
set "SCRIPTS_REPO_URL=http://nathantech.net:3000/CosmicRage/Mush-Soundpack.git"
set "SOUNDS_REPO_URL=https://nathantech.net:3000/CosmicRage/CosmicRageSounds.git"

:: --------- Setup Log ---------
echo [%DATE% %TIME%] Starting update process... > "%LOG_FILE%"
echo [%DATE% %TIME%] GitPortable: %GIT_PORTABLE% >> "%LOG_FILE%"
echo [%DATE% %TIME%] Main repository directory: %MAIN_REPO_DIR% >> "%LOG_FILE%"
echo [%DATE% %TIME%] Sounds directory: %SOUNDS_DIR% >> "%LOG_FILE%"
echo [%DATE% %TIME%] Temporary scripts directory: %SCRIPTS_TEMP_DIR% >> "%LOG_FILE%"

:: --------- Determine which Git to use ---------
set "GIT_CMD="
where git >nul 2>nul
if %errorlevel%==0 (
    set "GIT_CMD=git"
    echo [%DATE% %TIME%] Git found in PATH. >> "%LOG_FILE%"
) else if exist "%GIT_PORTABLE%" (
    set "GIT_CMD=%GIT_PORTABLE%"
    echo [%DATE% %TIME%] Git not found in PATH, using GitPortable. >> "%LOG_FILE%"
) else (
    echo [%DATE% %TIME%] ERROR: Git not found in PATH or at %GIT_PORTABLE% >> "%LOG_FILE%"
    echo ERROR: Git not found in PATH or at %GIT_PORTABLE%
    pause
    exit /b
)

:: --------- Mark user-specific files as 'assume unchanged' ---------
set "MCL_FILE=cosmic rage\worlds\cosmic rage\cosmic rage.mcl"
if exist "%MAIN_REPO_DIR%\%MCL_FILE%" (
    %GIT_CMD% -C "%MAIN_REPO_DIR%" update-index --assume-unchanged "%MCL_FILE%"
    echo [%DATE% %TIME%] Marked %MCL_FILE% as assume-unchanged. >> "%LOG_FILE%"
    if errorlevel 1 (
        echo [%DATE% %TIME%] WARNING: Failed to mark %MCL_FILE% as assume-unchanged. Will proceed without excluding it. >> "%LOG_FILE%"
        echo WARNING: Could not mark %MCL_FILE% as assume-unchanged.
        %GIT_CMD% -C "%MAIN_REPO_DIR%" update-index --no-assume-unchanged "%MCL_FILE%" >nul 2>&1
    ) else (
        echo [%DATE% %TIME%] Marked %MCL_FILE% as assume-unchanged. >> "%LOG_FILE%"
    )
) else (
    echo [%DATE% %TIME%] WARNING: %MCL_FILE% not found, skipping assume-unchanged. >> "%LOG_FILE%"
)

:: Pull updates
%GIT_CMD% -C "%MAIN_REPO_DIR%" stash              >> "%LOG_FILE%" 2>&1
%GIT_CMD% -C "%MAIN_REPO_DIR%" pull               >> "%LOG_FILE%" 2>&1

:: If pull failed, re-clone
if errorlevel 1 (
    echo [%DATE% %TIME%] ERROR: Git pull failed. Cloning repo instead... >> "%LOG_FILE%"
    goto clone_repo
)

:: Success branch
echo [%DATE% %TIME%] Git pull successful or no updates found. >> "%LOG_FILE%"
echo [%DATE% %TIME%] Reapplying stashed changes...           >> "%LOG_FILE%"
%GIT_CMD% -C "%MAIN_REPO_DIR%" stash pop                   >> "%LOG_FILE%" 2>&1
goto pull_sounds


:clone_repo
:: --------- Clone the Main Repo into Temporary Directory ---------
if exist "%SCRIPTS_TEMP_DIR%" (
    echo [%DATE% %TIME%] Deleting existing temporary scripts directory... >> "%LOG_FILE%"
    rd /s /q "%SCRIPTS_TEMP_DIR%" >nul 2>&1
)
echo [%DATE% %TIME%] Cloning main repo into temporary directory... >> "%LOG_FILE%"
"%GIT_CMD%" clone "%SCRIPTS_REPO_URL%" "%SCRIPTS_TEMP_DIR%" >> "%LOG_FILE%"
if errorlevel 1 (
    echo [%DATE% %TIME%] ERROR: Git clone failed for main scripts repo. >> "%LOG_FILE%"
    echo ERROR: Failed to clone the main scripts repo. Check network or GitPortable version.
    pause
    exit /b
)

:: --------- Replace Files in the Main Directory (Safe Copy) ---------
echo [%DATE% %TIME%] Preparing to copy files... >> "%LOG_FILE%"
set "SRC_DIR=%~dp0temp_scripts"
set "DEST_DIR=%~dp0."
echo SRC_DIR: [%SRC_DIR%] >> "%LOG_FILE%"
echo DEST_DIR: [%DEST_DIR%] >> "%LOG_FILE%"

if not exist "%SRC_DIR%" (
    echo [%DATE% %TIME%] ERROR: Source directory does not exist: %SRC_DIR% >> "%LOG_FILE%"
    exit /b
)
if not exist "%DEST_DIR%" (
    echo [%DATE% %TIME%] ERROR: Destination directory does not exist: %DEST_DIR% >> "%LOG_FILE%"
    exit /b
)

robocopy "%SRC_DIR%" %~dp0 /E /COPY:DAT /XO >> "%LOG_FILE%" 2>&1
if errorlevel 8 (
    echo [%DATE% %TIME%] ERROR: Robocopy failed. Exit code: %errorlevel% >> "%LOG_FILE%"
    pause
    exit /b
)

:: --------- Clean Up Temporary Directory ---------
rd /s /q "%SCRIPTS_TEMP_DIR%" >nul 2>&1

:pull_sounds
:: --------- Pull Updates for Sounds Repo ---------
if exist "%SOUNDS_DIR%" (
    echo [%DATE% %TIME%] Pulling updates for the sounds repository... >> "%LOG_FILE%"
    "%GIT_CMD%" -C "%SOUNDS_DIR%" pull >> "%LOG_FILE%"
    if errorlevel 1 (
        echo [%DATE% %TIME%] ERROR: Failed to pull updates for the sounds repository. >> "%LOG_FILE%"
        echo ERROR: Failed to pull updates for the sounds repository. Check the log for details.
    )
) else (
    echo [%DATE% %TIME%] ERROR: Sounds directory not found at %SOUNDS_DIR% >> "%LOG_FILE%"
    echo ERROR: Sounds directory not found at %SOUNDS_DIR%.
)

:: --------- Final Logging ---------
echo [%DATE% %TIME%] Update process completed. >> "%LOG_FILE%"
pause
exit /b
