@echo off
setlocal EnableExtensions
title TaskbarHero Speed Assistant

set "APPDIR=%~dp0"
set "LOG=%APPDIR%startup-log.txt"
cd /d "%APPDIR%" || (
  echo Cannot enter app folder.
  pause
  exit /b 1
)

echo [%date% %time%] Starting TaskbarHero Speed Assistant>"%LOG%"
echo APPDIR=%APPDIR%>>"%LOG%"

if exist "%APPDIR%TaskbarHero-Speed-Assistant.ps1" (
  set "SCRIPT=%APPDIR%TaskbarHero-Speed-Assistant.ps1"
) else if exist "%APPDIR%Assistant.ps1" (
  set "SCRIPT=%APPDIR%Assistant.ps1"
) else (
  echo Missing assistant script.
  echo Please extract the whole zip package first, then run Run.bat again.
  echo Missing assistant script>>"%LOG%"
  pause
  exit /b 1
)

if not exist "%APPDIR%payload\TaskbarHeroSpeedMod.dll" (
  if exist "%APPDIR%TaskbarHeroSpeedMod.dll" (
    mkdir "%APPDIR%payload" >nul 2>nul
    copy /y "%APPDIR%TaskbarHeroSpeedMod.dll" "%APPDIR%payload\TaskbarHeroSpeedMod.dll" >nul
  )
)

if not exist "%APPDIR%payload\TaskbarHeroSpeedMod.dll" (
  echo Missing payload\TaskbarHeroSpeedMod.dll
  echo Please extract the whole zip package first, then run Run.bat again.
  echo Missing payload\TaskbarHeroSpeedMod.dll>>"%LOG%"
  pause
  exit /b 1
)

set "PS=%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"
if not exist "%PS%" set "PS=powershell.exe"

"%PS%" -NoLogo -NoProfile -STA -ExecutionPolicy Bypass -File "%SCRIPT%" >>"%LOG%" 2>&1
set "ERR=%ERRORLEVEL%"
if not "%ERR%"=="0" (
  echo.
  echo The assistant did not start correctly.
  echo Error code: %ERR%
  echo Please send startup-log.txt to support.
  echo Failed with errorlevel %ERR%>>"%LOG%"
  pause
  exit /b %ERR%
)
