# find-doom2.ps1 - locate the user's own doom2.wad (Steam/GOG/common spots) and
# copy it into the HELLHOUSE folder. Called by PLAY HELLHOUSE.bat. Arg = dest folder.
param([string]$Dest = ".")
$ErrorActionPreference = "SilentlyContinue"

# already there?
if ((Test-Path (Join-Path $Dest "doom2.wad")) -or (Test-Path (Join-Path $Dest "DOOM2.WAD"))) { return }

$candidates = @(
  "C:\Program Files (x86)\Steam\steamapps\common\Doom 2\base\DOOM2.WAD",
  "C:\Program Files (x86)\Steam\steamapps\common\Doom 2\masterbase\DOOM2.WAD",
  "C:\Program Files (x86)\Steam\steamapps\common\Doom 2\finaldoombase\DOOM2.WAD",
  "C:\Program Files (x86)\Steam\steamapps\common\DOOM 2\rerelease\DOOM II_LV\DOOM2.WAD",
  "C:\Program Files (x86)\Steam\steamapps\common\Ultimate Doom\rerelease\DOOM2.WAD",
  "C:\GOG Games\DOOM II\doom2.wad",
  "C:\GOG Games\Doom 2\DOOM2.WAD",
  "C:\DOOM2\DOOM2.WAD",
  (Join-Path $env:USERPROFILE "Downloads\doom2.wad"),
  (Join-Path $env:USERPROFILE "Desktop\doom2.wad")
)

# also resolve Steam's real library path from the registry + scan alternate libraries
$steam = (Get-ItemProperty "HKCU:\Software\Valve\Steam" -Name SteamPath).SteamPath
if ($steam) {
  $steam = $steam -replace "/","\"
  foreach ($sub in @("steamapps\common\Doom 2\base\DOOM2.WAD","steamapps\common\Doom 2\masterbase\DOOM2.WAD")) {
    $candidates += (Join-Path $steam $sub)
  }
}
foreach ($drive in @("D:","E:","F:")) {
  foreach ($lib in @("SteamLibrary\steamapps\common\Doom 2\base\DOOM2.WAD","Steam\steamapps\common\Doom 2\base\DOOM2.WAD")) {
    $candidates += "$drive\$lib"
  }
}

foreach ($c in $candidates) {
  if ($c -and (Test-Path $c)) {
    try {
      Copy-Item $c (Join-Path $Dest "doom2.wad") -Force
      Write-Host "Found and copied your doom2.wad - you're ready to play!"
      return
    } catch {}
  }
}
Write-Host "Couldn't auto-find doom2.wad - you'll need to drop your own copy into this folder."
