ThunderbirdBundle/Windows/getProfile.bat

122 lines
3.1 KiB
Batchfile

@echo off
set defaultProfile=NONE
set lastProfilePath=NONE
set profileCount=0
set profilePath=NONE
set defaultStartWithLastProfile=0
:: wait, proc, done
set inProc=wait
set inProfile=NONE
goto :main
:getexe
set "PathToExe=TB_NOT_FOUND"
set "k=HKCU\Software\Mozilla\Mozilla Thunderbird"
set CurrentVersion=NONE
for /f "tokens=3*" %%a in ('reg query "%k%" /V CurrentVersion 2^>NUL ^|findstr /ri "REG_SZ"') do set "CurrentVersion=%%a %%b"
if "%CurrentVersion%"=="NONE" goto :getexe_regfail
set "k=HKCU\Software\Mozilla\Mozilla Thunderbird\%CurrentVersion%\Main"
set PathToExe=NONE
for /f "tokens=3*" %%a in ('reg query "%k%" /V PathToExe 2^>NUL ^|findstr /ri "REG_SZ"') do set "PathToExe=%%a %%b"
if "%PathToExe%"=="NONE" goto :getexe_regfail
if not exist "%PathToExe%" goto :getexe_regfail
goto :getexe_found
:getexe_regfail
set "PathToExe=%ProgramFiles(x86)%\Mozilla Thunderbird\thunderbird.exe"
if exist "%PathToExe%" goto :getexe_found
set "PathToExe=%ProgramFiles%\Mozilla Thunderbird\thunderbird.exe"
if exist "%PathToExe%" goto :getexe_found
set "PathToExe=%ProgramW6432%\Mozilla Thunderbird\thunderbird.exe"
if exist "%PathToExe%" goto :getexe_found
echo TB_NOT_FOUND
exit 1
goto :eof
:getexe_found
echo %PathToExe%
goto :eof
:: find the default profiles
:doProfile1
if "%1:%2"=="Default:1" (
set "defaultProfile=%inProfile%"
) else if "%1"=="StartWithLastProfile" (
set "defaultStartWithLastProfile=%2"
)
set "line=%1"
if "%line:~0,8%"=="[Profile" (
set /A profileCount=profileCount+1
set "inProfile=%line:~8,-1%"
)
if "%line%"=="Path" set "lastProfilePath=%2"
goto :eof
:: parse the default profile and ignore the rest
:doProfile2
goto :doProfile2_%inProc%
:doProfile2_wait
if "%1"=="[Profile%defaultProfile%]" set inProc=proc
goto :eof
:doProfile2_proc
set "line=%1"
if "%line:~0,1%"=="[" set inProc=done
if "%inProc%"=="done" goto :eof
set "profile%1=%2"
goto :eof
:doProfile2_done
set "line=%1"
goto :eof
:main
if "%1"=="getprofile" goto :getprofile
if not "%1"=="getexe" goto :usage
call :getexe
goto :eof
:getprofile
set "profdir=%APPDATA%\Thunderbird\Profiles"
if not exist "%APPDATA%\Thunderbird\Profiles" goto :main_noprofiles
if not exist "%APPDATA%\Thunderbird\profiles.ini" goto :main_noprofiles
cd "%APPDATA%\Thunderbird" 2>NUL
if not %ERRORLEVEL%==0 goto :main_noprofiles
:: parsing profiles.ini in two steps
for /F "tokens=*" %%A in (profiles.ini) do call :doProfile1 %%A
for /F "tokens=*" %%A in (profiles.ini) do call :doProfile2 %%A
if "%profilePath%"=="NONE" goto :main_nodefault
cd "%profilePath%" 2>NUL
if not %ERRORLEVEL%==0 goto :main_noprofiles
cd
goto :eof
:main_nodefault
if not "%profileCount%"=="1" goto :main_noprofiles
if "%lastProfilePath%"=="" goto :main_noprofiles
cd "%APPDATA%\Thunderbird" 2>NUL
if not %ERRORLEVEL%==0 goto :main_noprofiles
cd "%lastProfilePath%" 2>NUL
if not %ERRORLEVEL%==0 goto :main_noprofiles
cd
goto :eof
:main_noprofiles
echo NO_PROFILE_DIR
exit 1
goto :eof
:usage
echo cmd.exe /c getProfile.bat ^<getexe^|getprofile^>
:eof