@echo off

:: Set necessary folders and paths
set "BASE_DIR=%~dp0"
set "CACHE_DIR=%BASE_DIR%wincache"
set "APPDATA=%CACHE_DIR%\appdata"
set "LOCALAPPDATA=%CACHE_DIR%\localappdata"
set "USERPROFILE=%CACHE_DIR%\userprofile"
set "TEMP=%CACHE_DIR%\temp"
set "TMP=%CACHE_DIR%\temp"
set "HOMEDRIVE=%BASE_DIR%"
set "HOMEPATH=%CACHE_DIR%\homepath"
set "HOMESHARE=%CACHE_DIR%\homeshare"
set "DOCUMENTS=%CACHE_DIR%\documents"
set "DOWNLOADS=%CACHE_DIR%\downloads"
set "DESKTOP=%CACHE_DIR%\desktop"
set "FAVORITES=%CACHE_DIR%\favorites"
set "MUSIC=%CACHE_DIR%\music"
set "PICTURES=%CACHE_DIR%\pictures"
set "VIDEOS=%CACHE_DIR%\videos"

:: Create directories if they do not exist
mkdir "%CACHE_DIR%" 2>nul
mkdir "%APPDATA%" 2>nul
mkdir "%LOCALAPPDATA%" 2>nul
mkdir "%USERPROFILE%" 2>nul
mkdir "%TEMP%" 2>nul
mkdir "%HOMEPATH%" 2>nul
mkdir "%HOMESHARE%" 2>nul
mkdir "%DOCUMENTS%" 2>nul
mkdir "%DOWNLOADS%" 2>nul
mkdir "%DESKTOP%" 2>nul
mkdir "%FAVORITES%" 2>nul
mkdir "%MUSIC%" 2>nul
mkdir "%PICTURES%" 2>nul
mkdir "%VIDEOS%" 2>nul

:: Add necessary directories to PATH
rem set "PATH=C:\Windows\system32;C:\Windows;%APP_DIR%;%APP_DIR%\bin;%BASE_DIR%\miniconda3\Scripts;%BASE_DIR%\miniconda3"

:: Set other environment variables if needed
rem set "HF_HOME=%CACHE_DIR%\hf-cache"
rem mkdir "%HF_HOME%" 2>nul
rem set "TORCH_HOME=%CACHE_DIR%\torch-cache"
rem mkdir "%TORCH_HOME%" 2>nul
rem set TTS_HOME=%CACHE_DIR%\tts
rem mkdir "%TTS_HOME%" 2>nul

:: Save old environment variables
setlocal EnableDelayedExpansion
set "OLD_APPDATA=%APPDATA%"
set "OLD_LOCALAPPDATA=%LOCALAPPDATA%"
set "OLD_USERPROFILE=%USERPROFILE%"
set "OLD_TEMP=%TEMP%"
set "OLD_TMP=%TMP%"
set "OLD_HOMEDRIVE=%HOMEDRIVE%"
set "OLD_HOMEPATH=%HOMEPATH%"
set "OLD_HOMESHARE=%HOMESHARE%"
set "OLD_DOCUMENTS=%DOCUMENTS%"
set "OLD_DOWNLOADS=%DOWNLOADS%"
set "OLD_DESKTOP=%DESKTOP%"
set "OLD_FAVORITES=%FAVORITES%"
set "OLD_MUSIC=%MUSIC%"
set "OLD_PICTURES=%PICTURES%"
set "OLD_VIDEOS=%VIDEOS%"

:: Set new environment variables
set "APPDATA=%CACHE_DIR%\appdata"
set "LOCALAPPDATA=%CACHE_DIR%\localappdata"
set "USERPROFILE=%CACHE_DIR%\userprofile"
set "TEMP=%CACHE_DIR%\temp"
set "TMP=%CACHE_DIR%\temp"
set "HOMEDRIVE=%BASE_DIR%"
set "HOMEPATH=%CACHE_DIR%\homepath"
set "HOMESHARE=%CACHE_DIR%\homeshare"
set "DOCUMENTS=%CACHE_DIR%\documents"
set "DOWNLOADS=%CACHE_DIR%\downloads"
set "DESKTOP=%CACHE_DIR%\desktop"
set "FAVORITES=%CACHE_DIR%\favorites"
set "MUSIC=%CACHE_DIR%\music"
set "PICTURES=%CACHE_DIR%\pictures"
set "VIDEOS=%CACHE_DIR%\videos"

:: Run the application
AnythingLLM.exe

:: Restore original environment variables
set "APPDATA=%OLD_APPDATA%"
set "LOCALAPPDATA=%OLD_LOCALAPPDATA%"
set "USERPROFILE=%OLD_USERPROFILE%"
set "TEMP=%OLD_TEMP%"
set "TMP=%OLD_TMP%"
set "HOMEDRIVE=%OLD_HOMEDRIVE%"
set "HOMEPATH=%OLD_HOMEPATH%"
set "HOMESHARE=%OLD_HOMESHARE%"
set "DOCUMENTS=%OLD_DOCUMENTS%"
set "DOWNLOADS=%OLD_DOWNLOADS%"
set "DESKTOP=%OLD_DESKTOP%"
set "FAVORITES=%OLD_FAVORITES%"
set "MUSIC=%OLD_MUSIC%"
set "PICTURES=%OLD_PICTURES%"
set "VIDEOS=%OLD_VIDEOS%"

endlocal

:: Optional: pause to keep the command prompt open after execution
rem pause
