@echo off
:: --------------------------------------------------------
:: LetUsMsg - Windows Permission Diagnostic Launcher
:: Double-click this file to run the diagnostic script.
:: --------------------------------------------------------
::
:: Usage:
::   check_permissions.bat                          -- default data dir (Documents\lum0)
::   check_permissions.bat /log                     -- also save log file
::   check_permissions.bat /dir "D:\LumData\lum0"   -- custom data dir
::   check_permissions.bat /app "C:\Tools\LetUsMsg"  -- custom app dir
::   check_permissions.bat /dir "D:\lum0" /log      -- combined
::
:: The script must be in the same folder as check_permissions.ps1.

setlocal EnableDelayedExpansion

set "SCRIPT_DIR=%~dp0"
set "PS1=%SCRIPT_DIR%check_permissions.ps1"
set "PS_ARGS="
set "SAVE_LOG=0"
set "DATA_DIR="
set "APP_DIR="

:: Parse arguments
:parse_args
if "%~1"=="" goto check_admin
if /I "%~1"=="/log"  ( set "SAVE_LOG=1"       & shift & goto parse_args )
if /I "%~1"=="/dir"  ( set "DATA_DIR=%~2"      & shift & shift & goto parse_args )
if /I "%~1"=="/app"  ( set "APP_DIR=%~2"       & shift & shift & goto parse_args )
shift
goto parse_args

:: ---------------------------------------------------------------------------
:: Check if already running as Administrator
:: ---------------------------------------------------------------------------
:check_admin
net session >nul 2>&1
if %errorlevel% == 0 goto run

:: Not admin - ask user if they want to re-launch elevated.
:: This matters because the CFA auto-fix requires admin rights.
echo.
echo  LetUsMsg Permission Diagnostic
echo  ================================
echo.
echo  NOTE: You are NOT running as Administrator.
echo  The script can still diagnose issues, but it cannot auto-fix
echo  Controlled Folder Access (the most common cause of write failures)
echo  unless it runs with admin rights.
echo.
echo  Would you like to re-launch this script as Administrator?
echo.
echo    [1]  Yes - re-launch as Administrator (recommended)
echo    [2]  No  - continue as standard user (diagnosis only)
echo.
set /p "CHOICE=Enter 1 or 2: "
if "!CHOICE!"=="1" goto elevate
goto run

:elevate
:: Re-launch this exact BAT file with the same arguments via PowerShell Start-Process -Verb RunAs
:: We pass the original args through an environment variable to avoid quoting hell.
set "BAT_ARGS=/log" 
if not "!DATA_DIR!"=="" set "BAT_ARGS=!BAT_ARGS! /dir "!DATA_DIR!""
if not "!APP_DIR!"==""  set "BAT_ARGS=!BAT_ARGS! /app "!APP_DIR!""
if "!SAVE_LOG!"=="1"    set "BAT_ARGS=!BAT_ARGS! /log"
powershell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath '%~f0' -ArgumentList '!BAT_ARGS!' -Verb RunAs"
exit /b 0

:run
:: Build PowerShell argument string
if not "!DATA_DIR!"=="" set "PS_ARGS=!PS_ARGS! -DataDir '!DATA_DIR!'"
if not "!APP_DIR!"==""  set "PS_ARGS=!PS_ARGS! -AppDir  '!APP_DIR!'"
if "!SAVE_LOG!"=="1"    set "PS_ARGS=!PS_ARGS! -SaveLog"

echo.
echo  LetUsMsg Permission Diagnostic
echo  ================================
echo  Script : %PS1%
echo.

:: Check PowerShell is available
where powershell.exe >nul 2>&1
if errorlevel 1 (
    echo  ERROR: PowerShell not found. Please install PowerShell and try again.
    pause
    exit /b 1
)

:: Run the PS1 with ExecutionPolicy Bypass so no profile changes are needed
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%PS1%"%PS_ARGS%

set "EXIT_CODE=%errorlevel%"
echo.
echo  Done. Press any key to close.
pause >nul
exit /b %EXIT_CODE%
