@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%\sounds"
set "REPO_SUBFOLDER=wav"
set "SCRIPTS_REPO_URL=http://nathantech.net:3000/CosmicRage/VIPMudCosmicRageScripts.git"
set "SOUNDS_REPO_URL=https://nathantech.net:3000/CosmicRage/CosmicRageSounds.git"

:: --------- Locate VIP Root Directory ---------
set "VIP_ROOT="

for /f "tokens=2,*" %%i in ('
    reg query "HKCU\Software\VB and VBA Program Settings\VIPMUD\DefaultFolder" /v Filespec 2^>nul
') do set "VIP_ROOT=%%j"

:: Fallback if registry not found
if not defined VIP_ROOT (
    set "VIP_ROOT=%USERPROFILE%\Documents\VIP Mud"
)

echo [%DATE% %TIME%] VIP root directory resolved to: %VIP_ROOT% >> "%LOG_FILE%"

if not exist "%VIP_ROOT%" (
    echo [%DATE% %TIME%] ERROR: VIP root directory does not exist: %VIP_ROOT% >> "%LOG_FILE%"
    echo ERROR: VIP folder not found. Update aborted.
    pause
    exit /b
)

:: --------- 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
)

:: --------- Attempt to Pull from Main Repo or Clone if Failed ---------
echo [%DATE% %TIME%] Attempting to pull updates from the main repo... >> "%LOG_FILE%"
%GIT_CMD% -C "%MAIN_REPO_DIR%" pull               >> "%LOG_FILE%" 2>&1
if errorlevel 1 (
    echo [%DATE% %TIME%] ERROR: Git pull failed. Cloning repo instead... >> "%LOG_FILE%"
    goto clone_repo
) else (
    echo [%DATE% %TIME%] Git pull successful or no updates found. >> "%LOG_FILE%"
    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%" "%DEST_DIR%" /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%.
)

:: --------- Copy SoundSync.exe to VIP Root (if missing) ---------
if exist "%VIP_ROOT%\SoundSync.exe" (
    echo [%DATE% %TIME%] SoundSync.exe already exists in VIP root. Skipping copy. >> "%LOG_FILE%"
    echo SoundSync.exe already present in VIP folder. Skipping.
) else (
    if exist "%BASE_DIR%\SoundSync.exe" (
        echo [%DATE% %TIME%] Copying SoundSync.exe to VIP root >> "%LOG_FILE%"
        copy /Y "%BASE_DIR%\SoundSync.exe" "%VIP_ROOT%\" >nul
        echo SoundSync.exe copied to VIP folder.
    ) else (
        echo [%DATE% %TIME%] WARNING: SoundSync.exe not found alongside update script. >> "%LOG_FILE%"
        echo WARNING: SoundSync.exe not found.
    )
)

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