@echo off
setlocal EnableExtensions EnableDelayedExpansion

set "SCRIPT_DIR=%~dp0"
for %%I in ("%SCRIPT_DIR%..\..\..\..") do set "LOCAL_SOURCE_ROOT=%%~fI"
set "LOCAL_INSTALL_PS1=%LOCAL_SOURCE_ROOT%\installers\windows\Install-BioDeck-LocalEngine.ps1"
set "INSTALL_URL_1=https://biodeck.pages.dev/installers/windows/Install-BioDeck-LocalEngine.ps1"
set "INSTALL_URL_2=https://universal-ai-drug-discovery-platform.pages.dev/installers/windows/Install-BioDeck-LocalEngine.ps1"
set "INSTALL_URL_3=https://raw.githubusercontent.com/Bio-JS/Universal-AI-Drug-Discovery-Platform/main/frontend/public/installers/windows/Install-BioDeck-LocalEngine.ps1"
set "INSTALL_URL_LOCAL_3001=http://127.0.0.1:3001/installers/windows/Install-BioDeck-LocalEngine.ps1"
set "INSTALL_URL_LOCAL_3000=http://127.0.0.1:3000/installers/windows/Install-BioDeck-LocalEngine.ps1"
set "SOURCE_ZIP_URL_1=https://biodeck.pages.dev/installers/windows/BioDeckLocalEngineSource.zip"
set "SOURCE_ZIP_URL_2=https://universal-ai-drug-discovery-platform.pages.dev/installers/windows/BioDeckLocalEngineSource.zip"
set "SOURCE_ZIP_URL_3=https://raw.githubusercontent.com/Bio-JS/Universal-AI-Drug-Discovery-Platform/main/frontend/public/installers/windows/BioDeckLocalEngineSource.zip"
set "SOURCE_ZIP_URL_LOCAL_3001=http://127.0.0.1:3001/installers/windows/BioDeckLocalEngineSource.zip"
set "SOURCE_ZIP_URL_LOCAL_3000=http://127.0.0.1:3000/installers/windows/BioDeckLocalEngineSource.zip"
set "INSTALL_PS1=%TEMP%\Install-BioDeck-LocalEngine.ps1"
set "SOURCE_ZIP_URL=%SOURCE_ZIP_URL_1%"

if exist "%LOCAL_INSTALL_PS1%" (
  echo Running local BioDeck Local Engine installer...
  powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%LOCAL_INSTALL_PS1%" -SourceRoot "%LOCAL_SOURCE_ROOT%" -ForceManagedPython
  set "INSTALL_EXIT=!ERRORLEVEL!"
  if not "!INSTALL_EXIT!"=="0" (
    echo.
    echo BioDeck Local Engine installer failed with exit code !INSTALL_EXIT!.
    pause
    exit /b !INSTALL_EXIT!
  )
  echo.
  echo BioDeck Local Engine installer finished.
  pause
  exit /b 0
)

echo Downloading BioDeck Local Engine installer...
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "$pairs = @(@('%INSTALL_URL_1%', '%SOURCE_ZIP_URL_1%'), @('%INSTALL_URL_2%', '%SOURCE_ZIP_URL_2%'), @('%INSTALL_URL_3%', '%SOURCE_ZIP_URL_3%'), @('%INSTALL_URL_LOCAL_3001%', '%SOURCE_ZIP_URL_LOCAL_3001%'), @('%INSTALL_URL_LOCAL_3000%', '%SOURCE_ZIP_URL_LOCAL_3000%')); $selected = ''; foreach ($pair in $pairs) { try { Write-Host ('Trying ' + $pair[0]); Invoke-WebRequest -Uri $pair[0] -OutFile '%INSTALL_PS1%' -UseBasicParsing -TimeoutSec 5 -ErrorAction Stop; $selected = $pair[1]; break } catch { Write-Host ('Failed: ' + $_.Exception.Message) } }; if (-not $selected) { exit 1 }; Set-Content -Path '%TEMP%\BioDeckLocalEngineSourceUrl.txt' -Value $selected -Encoding ASCII"
set "DOWNLOAD_EXIT=!ERRORLEVEL!"
if not "!DOWNLOAD_EXIT!"=="0" (
  echo.
  echo Failed to download the installer.
  pause
  exit /b !DOWNLOAD_EXIT!
)
if exist "%TEMP%\BioDeckLocalEngineSourceUrl.txt" (
  set /p SOURCE_ZIP_URL=<"%TEMP%\BioDeckLocalEngineSourceUrl.txt"
)

powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%INSTALL_PS1%" -SourceZipUrl "%SOURCE_ZIP_URL%" -ForceManagedPython
set "INSTALL_EXIT=!ERRORLEVEL!"
if not "!INSTALL_EXIT!"=="0" (
  echo.
  echo BioDeck Local Engine installer failed with exit code !INSTALL_EXIT!.
  pause
  exit /b !INSTALL_EXIT!
)

echo.
echo BioDeck Local Engine installer finished.
pause
exit /b 0
