@echo off
REM STOP Auto-Update -- install / update the Lotus Notes export tools on this PC.
REM Downloaded from the web app as "StopAutoUpdate-Setup.cmd". Double-click it.
REM It fetches install.ps1 from the web app, checks it, and runs it (see that
REM file for details). Safe to run again any time: that is how the scripts get
REM updated. Exit code: 0 = OK, non-zero = something failed (see messages).

setlocal
set "BASE=https://stop-auto-update.supasiao.workers.dev/tools"

echo Downloading installer from %BASE% ...
REM Same checks install.ps1 applies to every other file: not empty, not an HTML
REM page (e.g. a corporate proxy "blocked" page), and parses as PowerShell.
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 } catch { }; try { [Net.WebRequest]::DefaultWebProxy.Credentials = [Net.CredentialCache]::DefaultNetworkCredentials } catch { }; $o = Join-Path $env:TEMP 'StopAutoUpdate-install.ps1'; try { Invoke-WebRequest -UseBasicParsing -Uri ('%BASE%/install.ps1?v=' + [DateTime]::UtcNow.Ticks) -OutFile $o -ErrorAction Stop } catch { Write-Host ('FAILED to download: ' + $_.Exception.Message) -ForegroundColor Red; exit 1 }; $t = [IO.File]::ReadAllText($o); if ($t.Trim().Length -eq 0) { Write-Host 'FAILED: the downloaded installer is empty.' -ForegroundColor Red; exit 1 }; if ($t.TrimStart().StartsWith('<')) { Write-Host 'FAILED: got a web page instead of the installer (blocked by proxy/firewall?).' -ForegroundColor Red; exit 1 }; $e = $null; [void][System.Management.Automation.Language.Parser]::ParseInput($t, [ref]$null, [ref]$e); if ($e -and $e.Count -gt 0) { Write-Host ('FAILED: the downloaded installer is damaged: ' + $e[0].Message) -ForegroundColor Red; exit 1 }; exit 0"
if errorlevel 1 (
    echo.
    echo Could not get a valid installer. Check the internet/proxy connection and try again.
    echo Nothing on this PC was changed.
    echo.
    pause
    exit /b 1
)

powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%TEMP%\StopAutoUpdate-install.ps1" -BaseUrl "%BASE%" %*
set "RC=%ERRORLEVEL%"

echo.
pause
exit /b %RC%
