@echo off
setlocal enabledelayedexpansion

:: Check for Administrator privileges
net session >nul 2>&1
if %errorLevel% neq 0 (
    echo [!] ERROR: Please right-click and 'Run as Administrator'.
    pause
    exit /b
)

:MENU
cls
echo ======================================================
echo          USB READ-ONLY MANAGER (DISKPART)
echo ======================================================
echo.
(echo list disk) | diskpart
echo.
echo [1] Lock USB (Set Read-Only)
echo [2] Unlock USB (Make Writable)
echo [3] Check Current Status (Default)
echo [4] Exit
echo.

:: Default choice logic
set "choice=3"
set /p "choice=Choose an option (1-4) [Default: 3]: "

if "%choice%"=="4" exit /b

:: Set the action based on choice
if "%choice%"=="1" (
    set "action=set readonly"
) else if "%choice%"=="2" (
    set "action=clear readonly"
) else (
    set "action=status"
    echo [i] Proceeding with Status Check...
)

echo.
set "disknum="
set /p "disknum=Enter the DISK NUMBER: "

:: Validate disk number input
if "%disknum%"=="" (
    echo [!] Error: You must enter a disk number.
    pause
    goto MENU
)

:: Create temporary script
echo select disk %disknum% > %temp%\dp_manage.txt
if "%action%"=="status" (
    echo detail disk >> %temp%\dp_manage.txt
) else (
    echo attributes disk %action% >> %temp%\dp_manage.txt
    echo detail disk >> %temp%\dp_manage.txt
)

echo.
echo Executing command %action% for Disk %disknum%...
echo.

:: Run diskpart and filter for the Read-only status line
diskpart /s %temp%\dp_manage.txt

del %temp%\dp_manage.txt

echo.
echo ======================================================
echo Command completed.
echo ======================================================
pause
goto MENU