@echo off
rem QuestAutoShare aktualisieren: laedt die neueste Version und ersetzt den Addon-Ordner.
rem Den AddOns-Ordner findet die Datei selbst (Registry + Suche auf allen Laufwerken) und merkt ihn sich.
setlocal
chcp 65001 >nul
set "BASE=https://wayhearth.quest/downloads"
set "URL=%BASE%/QuestAutoShare.zip"
set "CFG=%APPDATA%\QuestAutoShare-Pfad.txt"
set "ZIP=%TEMP%\QuestAutoShare.zip"
set "FOUND=%TEMP%\qas_addons.txt"

if exist "%CFG%" set /p ADDONS=<"%CFG%"
if defined ADDONS if not exist "%ADDONS%\" set "ADDONS="

if not defined ADDONS (
  echo Suche den AddOns-Ordner ...
  if exist "%FOUND%" del "%FOUND%"
  powershell -NoProfile -ExecutionPolicy Bypass -Command "$s=[IO.File]::ReadAllText('%~f0'); iex $s.Substring($s.IndexOf([char]10+'#PS#')+5)"
  if exist "%FOUND%" set /p ADDONS=<"%FOUND%"
)
if not defined ADDONS (
  echo Nicht gefunden. Wo liegt dein AddOns-Ordner?
  echo Beispiel: C:\Program Files ^(x86^)\World of Warcraft\_classic_\Interface\AddOns
  set /p ADDONS=Pfad:
)
if exist "%ADDONS%\" goto :haveaddons
echo Ordner nicht gefunden: %ADDONS%
pause
exit /b 1
:haveaddons
> "%CFG%" echo %ADDONS%
echo AddOns-Ordner: %ADDONS%

echo Lade QuestAutoShare ...
curl.exe -fsSL -o "%ZIP%" "%URL%"
if errorlevel 1 (echo Download fehlgeschlagen. Internet da? & pause & exit /b 1)

if exist "%ADDONS%\QuestAutoShare" rmdir /s /q "%ADDONS%\QuestAutoShare"
mkdir "%ADDONS%\QuestAutoShare"
"%SystemRoot%\System32\tar.exe" -xf "%ZIP%" -C "%ADDONS%\QuestAutoShare"
if errorlevel 1 (echo Entpacken fehlgeschlagen & pause & exit /b 1)
del "%ZIP%"

rem Shortest Path Forever steckt jetzt im Wayhearth-Ordner: der alte eigene Ordner wuerde doppelt laden
if exist "%ADDONS%\ShortestPathForever" rmdir /s /q "%ADDONS%\ShortestPathForever"

echo.
echo Fertig. QuestAutoShare ist aktuell. Im Spiel /reload tippen.
echo.
echo ===== Neuigkeiten =====
curl.exe -fsSL "%BASE%/QuestAutoShare-News.txt" 2>nul
echo.
pause
exit /b 0

#PS#
$roots = @()
foreach ($k in 'HKLM:\SOFTWARE\WOW6432Node\Blizzard Entertainment\World of Warcraft', 'HKLM:\SOFTWARE\Blizzard Entertainment\World of Warcraft') {
  try { $p = (Get-ItemProperty $k -ErrorAction Stop).InstallPath; if ($p) { $roots += $p } } catch {}
}
foreach ($d in [IO.DriveInfo]::GetDrives()) {
  if ($d.DriveType -eq 'Fixed' -and $d.IsReady) {
    Get-ChildItem $d.Name -Directory -Filter 'World of Warcraft*' -Recurse -Depth 3 -ErrorAction SilentlyContinue |
      ForEach-Object { $roots += $_.FullName }
  }
}
$found = @()
foreach ($r in ($roots | Select-Object -Unique)) {
  Get-ChildItem $r -Directory -Filter '_*_' -ErrorAction SilentlyContinue | ForEach-Object {
    $a = Join-Path $_.FullName 'Interface\AddOns'
    if (Test-Path $a) { $found += $a }
  }
}
$found = @($found | Select-Object -Unique)
$have = @($found | Where-Object { Test-Path (Join-Path $_ 'QuestAutoShare') })
if ($have.Count -ge 1) { $found = $have }
$pick = $null
if ($found.Count -eq 1) { $pick = $found[0] }
elseif ($found.Count -gt 1) {
  for ($i = 0; $i -lt $found.Count; $i++) { Write-Host ("  [{0}] {1}" -f ($i + 1), $found[$i]) }
  $n = Read-Host 'Welcher? Nummer eingeben'
  if ($n -match '^\d+$' -and [int]$n -ge 1 -and [int]$n -le $found.Count) { $pick = $found[[int]$n - 1] }
}
if ($pick) { [IO.File]::WriteAllText("$env:TEMP\qas_addons.txt", $pick) }
