@echo off
rem PCR Capture Custom - install or update. Double-click to run; run it again for every update.
rem Downloads the latest zip and copies it into %USERPROFILE%\PCR Capture Custom (same folder every
rem time, so Chrome keeps the same extension and your settings). Uses only Windows' curl, tar, robocopy.
setlocal
if not defined PCRC_SITE set "PCRC_SITE=https://pcr-capture-custom.pages.dev"
if not defined PCRC_TARGET set "PCRC_TARGET=%USERPROFILE%\PCR Capture Custom"
set "TMPD=%TEMP%\pcrc-%RANDOM%%RANDOM%"

echo Downloading PCR Capture Custom...
mkdir "%TMPD%\x" || goto fail
curl.exe -fsSL -o "%TMPD%\pcrc.zip" "%PCRC_SITE%/download/pcr-capture-custom.zip" || goto fail
tar.exe -xf "%TMPD%\pcrc.zip" -C "%TMPD%\x" || goto fail
if not exist "%TMPD%\x\PCR Capture Custom\manifest.json" goto fail

set "ISNEW=1"
if exist "%PCRC_TARGET%\manifest.json" set "ISNEW=0"
robocopy "%TMPD%\x\PCR Capture Custom" "%PCRC_TARGET%" /MIR /NFL /NDL /NJH /NJS /NP >nul
if errorlevel 8 goto fail
rd /s /q "%TMPD%" 2>nul

set "VER="
if exist "%PCRC_TARGET%\VERSION.txt" set /p VER=<"%PCRC_TARGET%\VERSION.txt"
echo.
echo Done: version %VER% is in "%PCRC_TARGET%"
echo.
if "%ISNEW%"=="1" goto firstinstall
echo Last step: open the PCR Capture Custom popup in Chrome and click "Reload now".
goto end

:firstinstall
echo First install - finish in Chrome:
echo   1. In the Chrome tab that opens (chrome://extensions), turn on "Developer mode" (top right).
echo   2. Click "Load unpacked" and choose this folder (the path is copied, so you can paste it):
echo      %PCRC_TARGET%
<nul set /p "=%PCRC_TARGET%"| clip
if not defined PCRC_TEST start "" chrome "chrome://extensions"
goto end

:fail
echo.
echo Install failed. Check your internet connection and try again, or use the .zip from
echo %PCRC_SITE%
if exist "%TMPD%" rd /s /q "%TMPD%" 2>nul

:end
echo.
if not defined PCRC_TEST pause
endlocal
