@echo off
setlocal EnableExtensions
title LiveryLab - Enable Long Windows Paths

set "REG_KEY=HKLM\SYSTEM\CurrentControlSet\Control\FileSystem"
set "REG_VALUE=LongPathsEnabled"
set "CURRENT_VALUE="

for /f "tokens=3" %%A in ('reg.exe query "%REG_KEY%" /v "%REG_VALUE%" 2^>nul ^| findstr /i "%REG_VALUE%"') do set "CURRENT_VALUE=%%A"

if /i "%CURRENT_VALUE%"=="0x1" goto already_enabled
if /i "%~1"=="--apply" goto apply

echo LiveryLab can use long Windows paths, but Windows must also allow them.
echo.
echo This optional tool changes the system-wide Windows setting:
echo   Enable Win32 long paths
echo.
echo Administrator permission is required. A Windows restart is recommended
echo after the change. LiveryLab never runs this tool automatically.
echo.
choice /c YN /n /m "Enable long Windows paths now? [Y/N] "
if errorlevel 2 goto cancelled

fltmc.exe >nul 2>&1
if not errorlevel 1 goto apply

echo.
echo Requesting administrator permission...
set "LIVERYLAB_LONG_PATH_HELPER=%~f0"
powershell.exe -NoProfile -Command "Start-Process -FilePath $env:LIVERYLAB_LONG_PATH_HELPER -ArgumentList '--apply' -Verb RunAs"
if errorlevel 1 goto elevation_failed
exit /b 0

:apply
fltmc.exe >nul 2>&1
if errorlevel 1 goto admin_required

reg.exe add "%REG_KEY%" /v "%REG_VALUE%" /t REG_DWORD /d 1 /f >nul
if errorlevel 1 goto write_failed

set "CURRENT_VALUE="
for /f "tokens=3" %%A in ('reg.exe query "%REG_KEY%" /v "%REG_VALUE%" 2^>nul ^| findstr /i "%REG_VALUE%"') do set "CURRENT_VALUE=%%A"
if /i not "%CURRENT_VALUE%"=="0x1" goto verify_failed

echo.
echo Long Windows paths are now enabled.
echo Restart Windows before relying on the new setting.
echo.
pause
exit /b 0

:already_enabled
echo Long Windows paths are already enabled on this PC.
echo No changes were made.
echo.
pause
exit /b 0

:cancelled
echo.
echo Cancelled. No changes were made.
pause
exit /b 0

:elevation_failed
echo.
echo Administrator permission was not granted. No changes were made.
pause
exit /b 1

:admin_required
echo.
echo This tool must be run as administrator. No changes were made.
pause
exit /b 1

:write_failed
echo.
echo Windows did not allow the registry setting to be changed.
echo The setting may be controlled by your administrator or Group Policy.
pause
exit /b 1

:verify_failed
echo.
echo The registry command completed, but the setting could not be verified.
echo No reliable change was detected.
pause
exit /b 1
