@echo off
chcp 65001 >nul
setlocal EnableExtensions
title Banker妙笔 WPS 安装
color 0A

echo.
echo ============================================================
echo   Banker妙笔 — WPS 插件安装
echo ============================================================
echo.
echo  请先完全退出 WPS（任务栏右下角托盘图标也要退出）
echo.
pause
echo.

set "VER=1.2.3"
set "ONLINE=https://addin.bankerai.cn/wps/"
set "ZIPURL=https://addin.bankerai.cn/releases/bankerai-wps-offline-%VER%.zip"
set "DIR=%APPDATA%\kingsoft\wps\jsaddons"
set "DIR2=%APPDATA%\kingsoft\wps\addons\jsaddons"
set "PLUGIN=%DIR%\bankerai"
set "TMP=%TEMP%\bankerai-wps-offline.zip"
set "ERR=0"

echo [1/3] 创建目录
if not exist "%DIR%" mkdir "%DIR%"
if not exist "%DIR2%" mkdir "%DIR2%"
if not exist "%PLUGIN%" mkdir "%PLUGIN%"
echo       %DIR%
echo.

echo [2/3] 下载离线插件文件
powershell -NoProfile -ExecutionPolicy Bypass -Command "try { Invoke-WebRequest -UseBasicParsing -Uri '%ZIPURL%' -OutFile '%TMP%'; if (Test-Path '%PLUGIN%') { Get-ChildItem '%PLUGIN%' -Force | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue }; Expand-Archive -Force -Path '%TMP%' -DestinationPath '%PLUGIN%'; if (-not (Test-Path '%PLUGIN%\ribbon.xml')) { throw '解压后缺少 ribbon.xml' }; Write-Host 'OK' } catch { Write-Host ('FAIL: ' + $_.Exception.Message); exit 1 }"
if errorlevel 1 (
  echo       [失败] 无法下载/解压离线包，请检查网络后重试
  set "ERR=1"
  goto FINISH
)
echo       [成功] 已解压到 %PLUGIN%
echo.

echo [3/3] 写入 WPS 加载配置 publish.xml
rem Prefer local folder path so WPS does not need to fetch HTTPS at startup.
rem Also keep online URL as comment-free second write target for older builds.
powershell -NoProfile -ExecutionPolicy Bypass -Command "$plugin=(Resolve-Path '%PLUGIN%').Path.Replace('\','/'); if ($plugin -notmatch '/$') { $plugin += '/' }; $xmlLocal='<?xml version=\"1.0\" encoding=\"UTF-8\"?><jsplugins><jspluginonline name=\"bankerai\" type=\"wps\" url=\"'+$plugin+'\" version=\"%VER%\" enable=\"true\" install=\"true\"/></jsplugins>'; $xmlOnline='<?xml version=\"1.0\" encoding=\"UTF-8\"?><jsplugins><jspluginonline name=\"bankerai\" type=\"wps\" url=\"%ONLINE%\" version=\"%VER%\" enable=\"true\" install=\"true\"/></jsplugins>'; $utf8=New-Object System.Text.UTF8Encoding $false; [IO.File]::WriteAllText('%DIR%\publish.xml',$xmlLocal,$utf8); [IO.File]::WriteAllText('%DIR2%\publish.xml',$xmlOnline,$utf8); Write-Host ('local url = '+$plugin); Write-Host 'OK'"
if errorlevel 1 (
  echo       [失败] 无法写入 publish.xml
  set "ERR=1"
  goto FINISH
)
echo       [成功] %DIR%\publish.xml
echo.

:FINISH
echo ------------------------------------------------------------
if "%ERR%"=="0" (
  echo  安装完成。
  echo.
  echo  接下来请：
  echo    1. 重新打开 WPS 文字
  echo    2. 打开任意文档
  echo    3. 在「开始」选项卡查看「Banker妙笔」
  echo.
  echo  正在打开配置目录，请确认里面有 publish.xml 和 bankerai 文件夹...
  explorer "%DIR%"
) else (
  echo  安装未成功。请把本窗口文字截图发给支持人员。
)
echo ------------------------------------------------------------
echo.
echo 按任意键关闭本窗口...
pause >nul
endlocal
exit /b %ERR%
