@echo off
setlocal

title Activate Office LTSC Professional Plus 2021
cls

echo ============================================================================
echo  Office LTSC Professional Plus 2021 VL Activation Script
echo ============================================================================
echo.
echo This script will attempt to convert a Retail version of Office 2021
echo to Volume License and activate it using a public KMS server.
echo.
echo Please run this script as an Administrator.
echo ============================================================================
echo.

rem --- Find Office installation path (Office16 for 2016/2019/2021) ---
set "OfficePath="
if exist "%ProgramFiles%\Microsoft Office\Office16\ospp.vbs" set "OfficePath=%ProgramFiles%\Microsoft Office\Office16"
if exist "%ProgramFiles(x86)%\Microsoft Office\Office16\ospp.vbs" set "OfficePath=%ProgramFiles(x86)%\Microsoft Office\Office16"

if not defined OfficePath (
    echo [ERROR] Office 2016/2019/2021 installation not found.
    echo Script cannot continue.
    goto :end
)

echo [INFO] Found Office installation at: "%OfficePath%"
cd /d "%OfficePath%"
echo.

rem --- Set variables for Office LTSC Professional Plus 2021 ---
set "GVLK_KEY=FXYTK-NJJ8C-GB6DW-3DYQT-6F7TH"
set "ProductName=Office LTSC Pro Plus 2021"
set "LicensePattern=ProPlus2021VL_KMS*.xrm-ms"

echo ============================================================================
echo  Starting activation for: %ProductName%
echo ============================================================================
echo.

rem --- Step 1: Uninstall any existing product keys to prevent conflicts ---
echo [1/5] Removing existing product keys (Retail/Trial)...
for /f "tokens=2 delims=:" %%a in ('cscript //nologo ospp.vbs /dstatus ^| findstr /c:"Last 5 characters of installed product key" /c:"Последние 5 знаков установленного ключа продукта"') do (
    for /f "tokens=*" %%b in ("%%a") do (
        cscript //nologo ospp.vbs /unpkey:%%b >nul
    )
)
echo      Done.
echo.

rem --- Step 2: Install Volume License to convert edition ---
echo [2/5] Converting Office to Volume License...
set "LicenseFound="
rem The path ..\root\Licenses16 is relative to the current dir (%OfficePath%)
for /f "delims=" %%i in ('dir /b "..\root\Licenses16\%LicensePattern%"') do (
    cscript ospp.vbs /inslic:"..\root\Licenses16\%%i" >nul
    set LicenseFound=1
)
if not defined LicenseFound (
    echo [ERROR] No Volume License files found for '%LicensePattern%'.
    echo This script can only convert official installations of Office.
    goto :end
)
echo      Conversion successful.
echo.

rem --- Step 3: Install GVLK Key ---
echo [3/5] Installing new product key...
cscript ospp.vbs /inpkey:%GVLK_KEY% >nul
echo      Done.
echo.

rem --- Step 4: Set KMS Host ---
echo [4/5] Setting KMS server address...
cscript ospp.vbs /sethst:kms.digiboy.ir >nul
echo      Done.
echo.

rem --- Step 5: Activate ---
echo [5/5] Attempting to activate...
cscript ospp.vbs /act
echo.

:end
echo ============================================================================
echo Activation attempt finished. Check the status messages above.
echo ============================================================================
echo.
pause