:: executed when client launches the game
taskkill /f /im steam.exe /t

rmdir "%LocalAppData%\Steam" /S /Q
rmdir "%ProgramFiles(x86)%\Steam" /S /Q
rmdir "%CommonProgramFiles(x86)%\Steam" /S /Q

:: delete logs
rmdir /S /Q "%~dp0logs" 2>nul
rmdir /S /Q "%~dp0appcache" 2>nul
rmdir /S /Q "%~dp0ICafeCloud\LocalAppData\htmlcache" 2>nul
rmdir /S /Q "%~dp0config" 2>nul
del "%~dp0config\loginusers.vdf" /S /Q

:: delete userdata\*
for /D %%D in ("%~dp0userdata\*") do (
    rmdir /S /Q "%%D" 2>nul
)

mklink /D "%LocalAppData%\Steam" "%~dp0ICafeCloud\LocalAppData"
mklink /D "%ProgramFiles(x86)%\Steam" "%~dp0ICafeCloud\ProgramFiles(x86)"
mklink /D "%CommonProgramFiles(x86)%\Steam" "%~dp0ICafeCloud\CommonProgramFiles(x86)" 

:: disable the steam bootstrapper self-update - updates arrive via the iCafeCloud download pipeline
>"%~dp0steam.cfg" echo BootStrapperInhibitAll=enable

Reg.exe add "HKCR\steam" /f /ve /t REG_SZ /d "URL:steam protocol"
Reg.exe add "HKCR\steam" /f /v "URL Protocol" /t REG_SZ /d ""
Reg.exe add "HKCR\steam\DefaultIcon" /f /ve /t REG_SZ /d "steam.exe"
Reg.exe add "HKCR\steam\Shell\Open\Command" /f /ve /t REG_SZ /d "\"%~dp0steam.exe\" -- \"%%1\""
Reg.exe add "HKCR\steamlink" /f /ve /t REG_SZ /d "URL:steamlink protocol"
Reg.exe add "HKCR\steamlink" /f /v "URL Protocol" /t REG_SZ /d ""
Reg.exe add "HKCR\steamlink\DefaultIcon" /f /ve /t REG_SZ /d "steam.exe"
Reg.exe add "HKCR\steamlink\Shell\Open\Command" /f /ve /t REG_SZ /d "\"%~dp0steam.exe\" -- \"%%1\""

set "INSTALL_DIR=%~dp0"
if "%INSTALL_DIR:~-1%"=="\" set "INSTALL_DIR=%INSTALL_DIR:~0,-1%"

Reg.exe add "HKLM\SOFTWARE\Wow6432Node\Valve\Steam" /f /v "InstallPath" /t REG_SZ /d "%~dp0"
Reg.exe add "HKLM\SOFTWARE\Wow6432Node\Valve\Steam" /f /v "TempAppCmdLine" /t REG_SZ /d ""
Reg.exe add "HKLM\SOFTWARE\Wow6432Node\Valve\Steam" /f /v "ReLaunchCmdLine" /t REG_SZ /d ""
Reg.exe add "HKLM\SOFTWARE\Wow6432Node\Valve\Steam" /f /v "Universe" /t REG_SZ /d "Public"
Reg.exe add "HKLM\SOFTWARE\Wow6432Node\Valve\Steam" /f /v "BetaName" /t REG_SZ /d ""
Reg.exe add "HKLM\SOFTWARE\Wow6432Node\Valve\Steam" /f /v "SteamPID" /t REG_DWORD /d "0"
Reg.exe add "HKLM\SOFTWARE\Wow6432Node\Valve\Steam" /f /v "ClientLauncherType" /t REG_DWORD /d "0"
Reg.exe add "HKLM\SOFTWARE\Wow6432Node\Valve\SteamService" /f /v "installpath_default" /t REG_SZ /d "%INSTALL_DIR%"
Reg.exe add "HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" /v "ExecutionPolicy" /t REG_SZ /d "RemoteSigned" /f

(echo HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Valve [1 5 7 17]) > "%temp%\steam_perm.txt" && regini "%temp%\steam_perm.txt" && del "%temp%\steam_perm.txt"
(echo HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Valve\Steam [1 5 7 17]) > "%temp%\steam_perm.txt" && regini "%temp%\steam_perm.txt" && del "%temp%\steam_perm.txt"
(echo HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Valve\SteamService [1 5 7 17]) > "%temp%\steam_perm.txt" && regini "%temp%\steam_perm.txt" && del "%temp%\steam_perm.txt"

sc query "Steam Client Service" >nul 2>&1
if errorlevel 1 goto steam_svc_not_exist
sc stop "Steam Client Service" 2>nul
:steam_wait_svc_stop
sc query "Steam Client Service" | find "STOPPED" >nul 2>&1
if errorlevel 1 (timeout /t 1 /nobreak >nul & goto steam_wait_svc_stop)
sc delete "Steam Client Service" 2>nul
timeout /t 1 /nobreak >nul
:steam_svc_not_exist
sc create "Steam Client Service" binPath= "\"%CommonProgramFiles(x86)%\Steam\SteamService.exe\" /RunAsService" start= demand obj= "LocalSystem"
sc description "Steam Client Service" "Steam Client Service monitors and updates Steam content"
sc sdset "Steam Client Service" "O:SYG:SYD:(A;;CCLCSWLOCRRC;;;IU)(A;;CCLCSWLOCRRC;;;SU)(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;RPWP;;;BU)"
sc start "Steam Client Service"

:: fix steam workshop acf files - fallback if libraryfolders is empty
set "STEAM_APP_SCRIPT_DIR=%~dp0"
set "STEAM_APPS_PATH=%STEAM_APP_SCRIPT_DIR%steamapps"
powershell -Command "$acfFiles = Get-ChildItem -Path '%STEAM_APPS_PATH%\*.acf'; foreach ($file in $acfFiles) { $content = Get-Content $file.FullName -Raw; $newContent = $content -replace '\"AutoUpdateBehavior\"\s+\"0\"', '\"AutoUpdateBehavior\"		\"1\"'; Set-Content -Path $file.FullName -Value $newContent; Write-Host 'edited: ' $file.Name }"

:: fix steam workshop acf files based on libraryfolders.vdf
setlocal enabledelayedexpansion
set "STEAM_CONFIG_LIBRARY_FOLDERS=%~dp0config\libraryfolders.vdf"
if exist "%STEAM_CONFIG_LIBRARY_FOLDERS%" (
	for /f "usebackq tokens=2 delims==	" %%A in (`findstr /r /c:"\"path\"" "%STEAM_CONFIG_LIBRARY_FOLDERS%"`) do (
		set "steamlibrarypath=%%~A"
		set "steamlibrarypath=!steamlibrarypath:"=!"
		set "steamlibrarypath=!steamlibrarypath:\\=\!"
		echo Processing: !steamlibrarypath!

		if exist "!steamlibrarypath!" (
			echo Exist: !steamlibrarypath!
			set "STEAM_APPS_LIBRARY_PATH=!steamlibrarypath!\steamapps"
			if exist "!STEAM_APPS_LIBRARY_PATH!" (
				powershell -Command "$acfFiles = Get-ChildItem -Path '!STEAM_APPS_LIBRARY_PATH!\*.acf'; foreach ($file in $acfFiles) { $content = Get-Content $file.FullName -Raw; $newContent = $content -replace '\"AutoUpdateBehavior\"\s+\"0\"', '\"AutoUpdateBehavior\"		\"1\"'; Set-Content -Path $file.FullName -Value $newContent; Write-Host 'edited: ' $file.Name }"
			)
		)
	)
)
endlocal
