@echo off
setlocal
title GOBAO PC Setup
set "GOBAO_ORIGIN=https://gobao.veigo1.top"
set "GOBAO_WORK=%TEMP%\GOBAO-PC-Setup-0.11.9"
set "GOBAO_ZIP=%GOBAO_WORK%\gobao-pc-control-0.10.8.zip"
set "GOBAO_LOG=%USERPROFILE%\Desktop\GOBAO-PC-Setup.log"

echo GOBAO PC setup started > "%GOBAO_LOG%"
echo Work: %GOBAO_WORK% >> "%GOBAO_LOG%"

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command ^
  "$ErrorActionPreference='Stop';" ^
  "New-Item -ItemType Directory -Force -Path '%GOBAO_WORK%' | Out-Null;" ^
  "Invoke-WebRequest -Uri '%GOBAO_ORIGIN%/downloads/gobao-pc-control-0.10.8.zip' -OutFile '%GOBAO_ZIP%' -UseBasicParsing;" ^
  "Expand-Archive -LiteralPath '%GOBAO_ZIP%' -DestinationPath '%GOBAO_WORK%' -Force;" ^
  "$installer=Get-ChildItem -LiteralPath '%GOBAO_WORK%' -Filter 'Install-GOBAO-Companion.ps1' -Recurse | Select-Object -First 1;" ^
  "if(-not $installer){throw 'Install-GOBAO-Companion.ps1 not found'};" ^
  "powershell.exe -NoProfile -ExecutionPolicy Bypass -File $installer.FullName -Mode pc" >> "%GOBAO_LOG%" 2>&1

if errorlevel 1 (
  echo.
  echo Installation failed. Log: %GOBAO_LOG%
  start "" notepad.exe "%GOBAO_LOG%"
  pause
  exit /b 1
)

echo.
echo GOBAO PC setup completed.
echo Log: %GOBAO_LOG%
pause
