@echo off
REM ============================================================
REM   Miaopuyun AI Agent Helper - One-click installer V115
REM   ASCII-only source so cmd parses cleanly on any Windows codepage
REM   Auto download + extract + register to installed Chromium browsers
REM ============================================================
chcp 65001 >nul 2>&1
setlocal EnableDelayedExpansion

set "PROD_VER=1.15.0"
set "EXT_ID=pmkcgnkmpcijelkfmmighallfmfmmhae"
set "DEST=%LOCALAPPDATA%\miaopuyun\agent-helper"
set "TMPDIR=%TEMP%\miaopuyun_install_%RANDOM%"
set "ZIPURL=https://ai.miaopuyun.cn/installer/mpy-agent-helper-v115.zip"
set "ZIPFILE=%TMPDIR%\helper.zip"

title Miaopuyun AI Agent Helper - Install V%PROD_VER%

echo.
echo ============================================================
echo   Miaopuyun AI Agent Helper - One-click Install V%PROD_VER%
echo   Auto download + extract + register to your Chromium browser
echo ============================================================
echo.

where curl >nul 2>&1
if errorlevel 1 (
  echo [ERROR] curl not found. Need Windows 10 1803 or newer.
  echo         Manual download: %ZIPURL%
  pause
  exit /b 1
)
if not exist "%SystemRoot%\System32\tar.exe" (
  echo [ERROR] tar not found. Need Windows 10 1803 or newer.
  echo         Manual download: %ZIPURL%
  pause
  exit /b 1
)

mkdir "%TMPDIR%" 2>nul
mkdir "%DEST%" 2>nul
if not exist "%DEST%" (
  echo [ERROR] Cannot create install dir: %DEST%
  pause
  exit /b 1
)

echo [1/4] Downloading agent helper V%PROD_VER% ...
echo       Source: %ZIPURL%
curl -fSL -o "%ZIPFILE%" "%ZIPURL%"
if errorlevel 1 (
  echo.
  echo [ERROR] Download failed. Check network or visit https://ai.miaopuyun.cn
  pause
  rmdir /S /Q "%TMPDIR%" 2>nul
  exit /b 1
)

echo [2/4] Extracting extension files ...
if exist "%DEST%\manifest.json" del /Q "%DEST%\*.json" "%DEST%\*.js" "%DEST%\*.png" 2>nul
REM Windows 10/11 bundled tar accepts ZIP paths directly; the GNU-only
REM force-local option causes the inbox bsdtar to abort before extraction.
tar -xf "%ZIPFILE%" -C "%DEST%"
if errorlevel 1 (
  echo [ERROR] Extract failed: %ZIPFILE%
  pause
  rmdir /S /Q "%TMPDIR%" 2>nul
  exit /b 1
)

if exist "%DEST%\extension\manifest.json" (
  xcopy /Y /Q "%DEST%\extension\*" "%DEST%\" >nul
  rmdir /S /Q "%DEST%\extension" 2>nul
)

if not exist "%DEST%\manifest.json" (
  echo [ERROR] manifest.json missing after extract. Package corrupted.
  pause
  rmdir /S /Q "%TMPDIR%" 2>nul
  exit /b 1
)

echo [3/4] Detecting installed Chromium browsers ...
set "REG_COUNT=0"

if exist "%LOCALAPPDATA%\Microsoft\Edge\User Data" (
  reg add "HKCU\Software\Microsoft\Edge\Extensions\%EXT_ID%" /v "path"    /t REG_SZ /d "%DEST%\manifest.json" /f >nul
  reg add "HKCU\Software\Microsoft\Edge\Extensions\%EXT_ID%" /v "version" /t REG_SZ /d "%PROD_VER%"           /f >nul
  echo   [OK] Microsoft Edge
  set /a REG_COUNT+=1
)

if exist "%LOCALAPPDATA%\Google\Chrome\User Data" (
  reg add "HKCU\Software\Google\Chrome\Extensions\%EXT_ID%"  /v "path"    /t REG_SZ /d "%DEST%\manifest.json" /f >nul
  reg add "HKCU\Software\Google\Chrome\Extensions\%EXT_ID%"  /v "version" /t REG_SZ /d "%PROD_VER%"           /f >nul
  echo   [OK] Google Chrome
  set /a REG_COUNT+=1
)

if exist "%LOCALAPPDATA%\Chromium\User Data" (
  reg add "HKCU\Software\Chromium\Extensions\%EXT_ID%"       /v "path"    /t REG_SZ /d "%DEST%\manifest.json" /f >nul
  reg add "HKCU\Software\Chromium\Extensions\%EXT_ID%"       /v "version" /t REG_SZ /d "%PROD_VER%"           /f >nul
  echo   [OK] Chromium
  set /a REG_COUNT+=1
)

if exist "%LOCALAPPDATA%\360Chrome\Chrome\User Data" (
  reg add "HKCU\Software\360\360Chrome\Extensions\%EXT_ID%"  /v "path"    /t REG_SZ /d "%DEST%\manifest.json" /f >nul
  reg add "HKCU\Software\360\360Chrome\Extensions\%EXT_ID%"  /v "version" /t REG_SZ /d "%PROD_VER%"           /f >nul
  echo   [OK] 360 Speed Browser
  set /a REG_COUNT+=1
)

if exist "%APPDATA%\360se6" (
  reg add "HKCU\Software\360\360se6\Extensions\%EXT_ID%"     /v "path"    /t REG_SZ /d "%DEST%\manifest.json" /f >nul
  reg add "HKCU\Software\360\360se6\Extensions\%EXT_ID%"     /v "version" /t REG_SZ /d "%PROD_VER%"           /f >nul
  echo   [OK] 360 Secure Browser 360SE6
  set /a REG_COUNT+=1
)
if exist "%APPDATA%\360se" (
  reg add "HKCU\Software\360\360SE\Extensions\%EXT_ID%"      /v "path"    /t REG_SZ /d "%DEST%\manifest.json" /f >nul
  reg add "HKCU\Software\360\360SE\Extensions\%EXT_ID%"      /v "version" /t REG_SZ /d "%PROD_VER%"           /f >nul
  echo   [OK] 360 Secure Browser 360SE
  set /a REG_COUNT+=1
)

if exist "%LOCALAPPDATA%\Tencent\QQBrowser\User Data" (
  reg add "HKCU\Software\Tencent\QQBrowser\Extensions\%EXT_ID%" /v "path"    /t REG_SZ /d "%DEST%\manifest.json" /f >nul
  reg add "HKCU\Software\Tencent\QQBrowser\Extensions\%EXT_ID%" /v "version" /t REG_SZ /d "%PROD_VER%"           /f >nul
  echo   [OK] QQ Browser
  set /a REG_COUNT+=1
)

if exist "%LOCALAPPDATA%\UCBrowser" (
  reg add "HKCU\Software\UCBrowser\Extensions\%EXT_ID%"      /v "path"    /t REG_SZ /d "%DEST%\manifest.json" /f >nul
  reg add "HKCU\Software\UCBrowser\Extensions\%EXT_ID%"      /v "version" /t REG_SZ /d "%PROD_VER%"           /f >nul
  echo   [OK] UC Browser
  set /a REG_COUNT+=1
)

if exist "%LOCALAPPDATA%\Quark" (
  reg add "HKCU\Software\Quark\Extensions\%EXT_ID%"          /v "path"    /t REG_SZ /d "%DEST%\manifest.json" /f >nul
  reg add "HKCU\Software\Quark\Extensions\%EXT_ID%"          /v "version" /t REG_SZ /d "%PROD_VER%"           /f >nul
  echo   [OK] Quark Browser
  set /a REG_COUNT+=1
)

if exist "%LOCALAPPDATA%\BraveSoftware\Brave-Browser\User Data" (
  reg add "HKCU\Software\BraveSoftware\Brave-Browser\Extensions\%EXT_ID%" /v "path"    /t REG_SZ /d "%DEST%\manifest.json" /f >nul
  reg add "HKCU\Software\BraveSoftware\Brave-Browser\Extensions\%EXT_ID%" /v "version" /t REG_SZ /d "%PROD_VER%"           /f >nul
  echo   [OK] Brave
  set /a REG_COUNT+=1
)

if exist "%LOCALAPPDATA%\Vivaldi\User Data" (
  reg add "HKCU\Software\Vivaldi\Extensions\%EXT_ID%"        /v "path"    /t REG_SZ /d "%DEST%\manifest.json" /f >nul
  reg add "HKCU\Software\Vivaldi\Extensions\%EXT_ID%"        /v "version" /t REG_SZ /d "%PROD_VER%"           /f >nul
  echo   [OK] Vivaldi
  set /a REG_COUNT+=1
)

if exist "%APPDATA%\SogouExplorer" (
  reg add "HKCU\Software\SogouExplorer\Extensions\%EXT_ID%"  /v "path"    /t REG_SZ /d "%DEST%\manifest.json" /f >nul
  reg add "HKCU\Software\SogouExplorer\Extensions\%EXT_ID%"  /v "version" /t REG_SZ /d "%PROD_VER%"           /f >nul
  echo   [OK] Sogou Browser
  set /a REG_COUNT+=1
)

if "!REG_COUNT!"=="0" (
  echo.
  echo [INFO] No Chromium-based browser detected on this machine.
  echo        Extension extracted to: %DEST%
  echo        Open your browser, go to extensions page, enable Developer mode,
  echo        click "Load unpacked" and select the folder above.
)

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall\MiaopuyunAgentHelper" /v "DisplayName"     /t REG_SZ /d "Miaopuyun AI Agent Helper" /f >nul
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall\MiaopuyunAgentHelper" /v "DisplayVersion"  /t REG_SZ /d "%PROD_VER%"               /f >nul
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall\MiaopuyunAgentHelper" /v "Publisher"       /t REG_SZ /d "Miaopuyun AI"             /f >nul
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall\MiaopuyunAgentHelper" /v "InstallLocation" /t REG_SZ /d "%DEST%"                   /f >nul

echo [4/4] Cleaning up temp files ...
rmdir /S /Q "%TMPDIR%" 2>nul

echo.
echo ============================================================
echo   Install complete. Helper V%PROD_VER% registered to !REG_COUNT! browser^(s^).
echo.
echo   * Next time you start the browser, you will see "Enable extension"
echo     prompt. Click Enable to activate.
echo   * After enabled:
echo     - Works silently in the background
echo     - Uses hidden window for link extraction
echo     - Auto upgrade, no reinstall needed
echo ============================================================
echo.
timeout /t 5 /nobreak >nul
endlocal
exit /b 0
