@echo off
title Lawcentr Word Add-in Installer
echo.
echo  ============================================
echo    Lawcentr Word Add-in Installer
echo  ============================================
echo.

REM Close Word if running
tasklist /FI "IMAGENAME eq WINWORD.EXE" 2>NUL | find /I "WINWORD.EXE" >NUL
if %ERRORLEVEL% == 0 (
    echo  Microsoft Word is currently open.
    echo  Please save your work. Word will be closed to complete installation.
    echo.
    pause
    taskkill /IM WINWORD.EXE /F >NUL 2>&1
    timeout /t 2 >NUL
)

REM Create installation directory
set "INSTALL_DIR=%APPDATA%\Lawcentr\WordAddin"
if not exist "%INSTALL_DIR%" mkdir "%INSTALL_DIR%"

echo  Downloading manifest...
powershell -Command "Invoke-WebRequest -Uri 'https://addin.lawcentr.com/manifest.xml' -OutFile '%INSTALL_DIR%\manifest.xml'" 2>NUL

if not exist "%INSTALL_DIR%\manifest.xml" (
    echo.
    echo  ERROR: Could not download the manifest file.
    echo  Please check your internet connection and try again.
    echo.
    pause
    exit /b 1
)

echo  Registering add-in with Microsoft Word...
reg add "HKCU\Software\Microsoft\Office\16.0\WEF\Developer" /v "LawcentrAddin" /t REG_SZ /d "%INSTALL_DIR%\manifest.xml" /f >NUL 2>&1

if %ERRORLEVEL% NEQ 0 (
    echo.
    echo  ERROR: Could not register the add-in.
    echo.
    pause
    exit /b 1
)

echo.
echo  ============================================
echo    Installation Complete!
echo  ============================================
echo.
echo  The Lawcentr add-in has been installed.
echo  Open Microsoft Word and look for the
echo  "Lawcentr" button in the Home tab.
echo.
pause
