@echo off
title EasierSBC installer

rem Administrator, or nothing happens. Writing a Chrome policy needs it,
rem and a plain .reg that is refused says nothing at all - which is the
rem failure this file exists to make impossible.
net session >nul 2>&1
if not "%errorlevel%"=="0" (
  echo Asking for administrator rights...
  powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
  exit /b
)

echo.
echo   EasierSBC - installing
echo.

rem A leftover from the first installer, which wrote a policy Chrome
rem ignores. Cleared so no machine holds two answers about one extension.
reg delete "HKLM\SOFTWARE\Policies\Google\Chrome\ExtensionSettings\blfnjnpnjkdbbndfeliimlhmldgaefhe" /f >nul 2>&1

rem Every slot already pointing at this extension goes first.
rem
rem The list is keyed by slot name, so writing a new slot leaves the old
rem one beside it - a machine that ran both installers ended up with the
rem same extension listed twice, under "1" and under "9137". Chrome
rem reports that as a policy conflict rather than installing anything,
rem and every future installer would have added another. Measured on a
rem real machine where nothing had installed and both slots were there.
for /f "tokens=1" %%A in ('reg query "HKLM\SOFTWARE\Policies\Google\Chrome\ExtensionInstallForcelist" 2^>nul ^| findstr /i "blfnjnpnjkdbbndfeliimlhmldgaefhe"') do (
  reg delete "HKLM\SOFTWARE\Policies\Google\Chrome\ExtensionInstallForcelist" /v %%A /f >nul 2>&1
)

reg add "HKLM\SOFTWARE\Policies\Google\Chrome\ExtensionInstallForcelist" /v 9137 /t REG_SZ /d "blfnjnpnjkdbbndfeliimlhmldgaefhe;https://easiersbc.pages.dev/downloads/updates.xml" /f >nul
if not "%errorlevel%"=="0" (
  echo   FAILED to write the policy. Nothing was changed.
  echo   Right-click this file and choose "Run as administrator".
  echo.
  pause
  exit /b 1
)

rem Read it back. "reg add reported success" and "the value is there" are
rem not the same claim, and only the second is worth printing.
reg query "HKLM\SOFTWARE\Policies\Google\Chrome\ExtensionInstallForcelist" /v 9137 >nul 2>&1
if not "%errorlevel%"=="0" (
  echo   The policy did not stick. Nothing was changed.
  echo.
  pause
  exit /b 1
)

echo   Policy written:
echo     blfnjnpnjkdbbndfeliimlhmldgaefhe
echo     https://easiersbc.pages.dev/downloads/updates.xml
echo.
echo   Now close Chrome completely - every window, and anything left in
echo   the tray - then open it again. Chrome reads policy only at startup,
echo   so nothing changes until it restarts.
echo.
echo   Then open the EasierSBC page: the line at the top says, in green,
echo   whether the extension is there.
echo.
pause
