@echo off
rem ============================================================
rem  sandpie for Word and Excel - installer
rem
rem  Installs the add-ins through a "shared folder" catalog, the
rem  only per-user install that keeps the sandpie button on the
rem  Home ribbon after Word/Excel restart. (The old WEF\Developer
rem  registry sideload is a developer-test install: Office drops
rem  the button on every restart.)
rem
rem  1. downloads the Word and Excel manifests to
rem     %APPDATA%\SandPie\catalog
rem  2. makes that folder reachable as a network path (Office only
rem     accepts \\server\share catalogs): \\localhost\C$\... when
rem     your account can use it, otherwise it creates a hidden
rem     read-only share SandPieAddin$ (one Windows admin prompt)
rem  3. registers it under Office's Trusted Add-in Catalogs and
rem     removes old sandpie sideload entries
rem  Then, in Word and/or Excel: Home > Add-ins > Advanced... >
rem  SHARED FOLDER > sandpie > Add  (once; it stays after that).
rem ============================================================
setlocal EnableExtensions
set "MANIFEST_URL=https://sandpie.app/office/manifest.xml"
set "XL_MANIFEST_URL=https://sandpie.app/office/manifest-excel.xml"
set "CAT=%APPDATA%\SandPie\catalog"
set "CATID={5A9D1E3C-7B2F-4C68-9E41-0D3B6F8A2C17}"
set "WEF=HKCU\Software\Microsoft\Office\16.0\WEF"
set "SHARE=SandPieAddin$"

echo.
echo   [sandpie] installing the Word and Excel add-ins...
echo.

:check_open
set "OPEN="
tasklist /fi "imagename eq WINWORD.EXE" 2>nul | find /i "WINWORD.EXE" >nul && set "OPEN= Word"
tasklist /fi "imagename eq EXCEL.EXE" 2>nul | find /i "EXCEL.EXE" >nul && set "OPEN=%OPEN% Excel"
if defined OPEN (
    echo   Still open:%OPEN%. Close ALL Word and Excel windows, then press any key.
    pause >nul
    goto :check_open
)

rem -- 1. download the manifests (bitsadmin ships with Windows; curl as fallback)
if not exist "%CAT%" mkdir "%CAT%"
if exist "%CAT%\manifest.xml" del /f /q "%CAT%\manifest.xml"
bitsadmin /transfer "sandpie" /download /priority foreground "%MANIFEST_URL%" "%CAT%\manifest.xml" >nul 2>&1
if not exist "%CAT%\manifest.xml" curl -fsSL -o "%CAT%\manifest.xml" "%MANIFEST_URL%" 2>nul
if not exist "%CAT%\manifest.xml" (
    echo   [X] Could not download the manifest. Check your internet
    echo       connection and that https://sandpie.app is reachable.
    pause
    exit /b 1
)
if exist "%CAT%\manifest-excel.xml" del /f /q "%CAT%\manifest-excel.xml"
bitsadmin /transfer "sandpie-xl" /download /priority foreground "%XL_MANIFEST_URL%" "%CAT%\manifest-excel.xml" >nul 2>&1
if not exist "%CAT%\manifest-excel.xml" curl -fsSL -o "%CAT%\manifest-excel.xml" "%XL_MANIFEST_URL%" 2>nul
if not exist "%CAT%\manifest-excel.xml" echo   [!] Could not download the Excel manifest - Word only for now.
echo   [OK] Manifests downloaded to %CAT%

rem -- 2. a network path Office accepts as a catalog
set "DRV=%CAT:~0,1%"
set "UNC=\\localhost\%DRV%$%CAT:~2%"
if exist "%UNC%\manifest.xml" goto :have_unc
set "UNC=\\localhost\%SHARE%"
if exist "%UNC%\manifest.xml" goto :have_unc

echo   Creating a private read-only share for the add-in folder.
echo   Windows will ask for admin permission once - choose Yes.
> "%TEMP%\sandpie-share.cmd" (
    echo @echo off
    echo net share %SHARE% /delete /y ^>nul 2^>^&1
    echo net share %SHARE%="%CAT%" /grant:"%USERDOMAIN%\%USERNAME%",READ /remark:"sandpie Office add-in catalog"
)
powershell -NoProfile -Command "Start-Process -FilePath '%TEMP%\sandpie-share.cmd' -Verb RunAs -Wait -WindowStyle Hidden" >nul 2>&1
del /f /q "%TEMP%\sandpie-share.cmd" >nul 2>&1
if not exist "%UNC%\manifest.xml" (
    echo   [X] Could not make the add-in folder reachable as a network
    echo       path. Run this file again and allow the admin prompt,
    echo       or ask IT to share %CAT% read-only.
    pause
    exit /b 1
)
:have_unc
echo   [OK] Catalog path: %UNC%

rem -- 3. register the catalog; clear old sandpie entries first
reg delete "%WEF%\Developer" /v SandPie /f >nul 2>&1
powershell -NoProfile -Command "Get-ChildItem 'HKCU:\Software\Microsoft\Office\16.0\WEF\TrustedCatalogs' -ErrorAction SilentlyContinue | Where-Object { (Get-ItemProperty $_.PSPath).Url -match 'sandpie' } | Remove-Item -Recurse -Force" >nul 2>&1
reg add "%WEF%\TrustedCatalogs\%CATID%" /v Id /t REG_SZ /d "%CATID%" /f >nul
reg add "%WEF%\TrustedCatalogs\%CATID%" /v Url /t REG_SZ /d "%UNC%" /f >nul
reg add "%WEF%\TrustedCatalogs\%CATID%" /v Flags /t REG_DWORD /d 1 /f >nul
if errorlevel 1 (
    echo   [X] Could not register the catalog in the registry.
    pause
    exit /b 1
)
echo   [OK] Registered as a trusted add-in catalog for your user

echo.
echo   Last step, once in each app you want it in (Word, Excel):
echo     1. Open Word or Excel.
echo     2. Home tab ^> Add-ins ^> click "Advanced..." at the bottom
echo        of the menu (NOT "More Add-ins").
echo     3. Pick the SHARED FOLDER tab ^> sandpie ^> Add.
echo     4. The sandpie button is now on the Home ribbon. Click it,
echo        then "Sign in with sandpie" and approve in your browser.
echo.
echo   This window can be closed now.
pause >nul
endlocal
