@echo off
setlocal

:: --------- Setup Log ---------
set "LOG_FILE=cosmic_setup.log"
echo [%DATE% %TIME%] Starting script... > "%LOG_FILE%"

:: --------- Define Paths ---------
set "CURRENT_DIR=%~dp0"
if "%CURRENT_DIR:~-1%"=="\" set "CURRENT_DIR=%CURRENT_DIR:~0,-1%"

:: Try to read the Filespec value
for /f "tokens=2,*" %%i in ('reg query "HKCU\Software\VB and VBA Program Settings\VIPMUD\DefaultFolder" /v Filespec 2^>nul') do set "TARGET_DIR=%%j\cosmic rage"

:: Fallback if not found
if not defined TARGET_DIR set "TARGET_DIR=%USERPROFILE%\Documents\VIP Mud\Cosmic Rage"

echo Target directory: %TARGET_DIR%

set "SET_FILE=%TARGET_DIR%\cosmic rage.set"
set "LOADER_FILE=%TARGET_DIR%\loader.set"
set "SOUNDS_DIR=%CURRENT_DIR%\sounds"
set "REPO_URL=https://gitea.nathantech.net//CosmicRage/CosmicRageSounds"
set "REPO_SUBFOLDER=wav"
set "GIT_PORTABLE=%CURRENT_DIR%\gitportable\bin\git.exe"

echo [%DATE% %TIME%] Script location: %CURRENT_DIR% >> "%LOG_FILE%"
echo [%DATE% %TIME%] Target directory: %TARGET_DIR% >> "%LOG_FILE%"
echo [%DATE% %TIME%] Sounds directory: %SOUNDS_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
)

:: --------- Handle Existing Cosmic Rage Folder ---------
if exist "%TARGET_DIR%" (
    echo The target folder already exists: "%TARGET_DIR%"
    echo Would you like to delete and recreate it? Warning: this may delete existing scripts or set gags. This cannot be undone! Please backup the folder. (Y/N)
    set "userchoice="
    set /p userchoice=Your choice:
    if /i "%userchoice%"=="Y" (
        echo [%DATE% %TIME%] Deleting existing folder... >> "%LOG_FILE%"
        rd /s /q "%TARGET_DIR%"
        if exist "%TARGET_DIR%" (
            echo [%DATE% %TIME%] ERROR: Could not delete folder. >> "%LOG_FILE%"
            echo ERROR: Could not delete folder. Files might be in use.
            pause
            exit /b
        )
        echo Creating fresh folder...
        mkdir "%TARGET_DIR%"
    ) else (
        echo [%DATE% %TIME%] Keeping existing folder. >> "%LOG_FILE%"
        echo Continuing with existing folder...
    )
) else (
    echo [%DATE% %TIME%] Folder does not exist. Creating new one. >> "%LOG_FILE%"
    mkdir "%TARGET_DIR%"
)

if not exist "%TARGET_DIR%" (
    echo [%DATE% %TIME%] ERROR: Failed to create folder. >> "%LOG_FILE%"
    echo ERROR: Could not create folder. Check permissions or path.
    pause
    exit /b
)
echo [%DATE% %TIME%] Folder is ready. >> "%LOG_FILE%"

:: --------- Download sounds using sparse checkout ---------
echo [%DATE% %TIME%] Downloading sound files using Git sparse clone... >> "%LOG_FILE%"
rd /s /q "%SOUNDS_DIR%" >nul 2>&1

"%GIT_CMD%" clone --depth=1 --filter=blob:none --sparse "%REPO_URL%" "%SOUNDS_DIR%"
if errorlevel 1 (
    echo [%DATE% %TIME%] ERROR: Git clone failed. >> "%LOG_FILE%"
    echo ERROR: Failed to clone repo. Check network or Git version.
    pause
    exit /b
)

pushd "%SOUNDS_DIR%"
"%GIT_CMD%" sparse-checkout set "%REPO_SUBFOLDER%"
if not exist "%SOUNDS_DIR%\%REPO_SUBFOLDER%" (
    echo [%DATE% %TIME%] ERROR: Failed to checkout sparse folder. >> "%LOG_FILE%"
    echo ERROR: Could not pull sparse folder. Check folder name or repo.
    popd
    pause
    exit /b
)
popd
echo [%DATE% %TIME%] Sounds successfully cloned. >> "%LOG_FILE%"

:: --------- Create or Recreate cosmic rage.set ---------
echo [%DATE% %TIME%] Creating or replacing cosmic rage.set >> "%LOG_FILE%"
if exist "%SET_FILE%" del /f /q "%SET_FILE%"

(
    echo #Load ^{%%CharInfo^(MudName^)/loader.set^}
    echo #If ^{%%Defined^(scpath^)=1 and %%Defined^(sppath^)=1^} ^{ #Load @scpath/Main.set^} ^{ #Say ^{WARNING! The scripts path and/or the sounds path are not defined. Unable to load the soundpack!^}^}
) > "%SET_FILE%"

echo [%DATE% %TIME%] Created: cosmic rage.set >> "%LOG_FILE%"
echo Created: %SET_FILE%)

:: --------- Create loader.set ---------
echo [%DATE% %TIME%] Creating loader.set >> "%LOG_FILE%"
(
    echo #Var scpath ^{%CURRENT_DIR%^/scripts}
    echo #Var sppath ^{%CURRENT_DIR%^/sounds/%REPO_SUBFOLDER%}
) > "%LOADER_FILE%"
echo Created: loader.set


:: --------- Copy SoundSync.exe (to VIP root folder) ---------
set "VIP_ROOT=%TARGET_DIR%\.."

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 "%CURRENT_DIR%\SoundSync.exe" (
        echo [%DATE% %TIME%] Copying SoundSync.exe to VIP root >> "%LOG_FILE%"
        copy /Y "%CURRENT_DIR%\SoundSync.exe" "%VIP_ROOT%\" >nul
        echo SoundSync.exe copied to VIP folder.
    ) else (
        echo [%DATE% %TIME%] WARNING: SoundSync.exe not found in script directory. >> "%LOG_FILE%"
        echo WARNING: SoundSync.exe not found.
    )
)


:: --------- Copy settings.set ---------
if exist "%CURRENT_DIR%\scripts\settings.set" (
    echo [%DATE% %TIME%] Copying settings.set >> "%LOG_FILE%"
    copy "%CURRENT_DIR%\scripts\settings.set" "%TARGET_DIR%\" >nul
    echo settings.set copied to target directory.
) else (
    echo [%DATE% %TIME%] WARNING: scripts\settings.set not found. >> "%LOG_FILE%"
    echo WARNING: scripts\settings.set not found.
)

:: --------- Copy gags folder ---------
if exist "%CURRENT_DIR%\scripts\gags" (
    echo [%DATE% %TIME%] Copying gags folder >> "%LOG_FILE%"
    xcopy /E /I /Y "%CURRENT_DIR%\scripts\gags" "%TARGET_DIR%\gags" >nul
    echo gags folder copied to target directory.
) else (
    echo [%DATE% %TIME%] WARNING: scripts\gags folder not found. >> "%LOG_FILE%"
    echo WARNING: scripts\gags folder not found.
)

:: --------- Finish ---------
echo [%DATE% %TIME%] Script completed successfully. >> "%LOG_FILE%"
echo Setup complete. Files are in "%TARGET_DIR%".
pause
exit /b
