@echo off
chcp 65001>nul 2>&1
powershell -NoProfile -ExecutionPolicy Bypass -Command "$s='%~f0';$t=[IO.File]::ReadAllText($s,[Text.Encoding]::UTF8);$k=[char]35+'PLUSPSBODY';iex $t.Substring($t.IndexOf($k)+$k.Length)"
echo.
pause
exit /b
#PLUSPSBODY
# ============================================================================
#  EllesmereUI_koKR_Plus 설치기 (추가기능 애드온) — 더블클릭 실행
#   1) WoW Interface\AddOns 폴더 자동 탐지
#   2) 이 폴더(EllesmereUI_koKR_Plus)를 AddOns 로 복사
#  ※ 폰트는 안 건드림(본체 koKR 또는 스크립트가 처리). 한글/영문 자동 구분 출력.
# ============================================================================
$ErrorActionPreference = 'Stop'
$Root = Split-Path -Parent $s
$KO = ((Get-UICulture).Name -like 'ko*') -or ((Get-Culture).Name -like 'ko*')
function M($en, $ko) { if ($KO) { $ko } else { $en } }
function Say($en, $ko, $c) { Write-Host (M $en $ko) -ForegroundColor $c }

Write-Host ''
Say '==  EllesmereUI_koKR_Plus  installer  ==' '==  EllesmereUI_koKR_Plus  설치  ==' Cyan
Write-Host ''

function Test-EUI($d) { return ($d -and (Test-Path (Join-Path $d 'EllesmereUI.toc'))) }
function FromWoWBase($wow) {
  if (-not $wow) { return $null }
  foreach ($fl in @('_retail_', '_classic_', '_classic_era_', '_ptr_', '_xptr_', '_beta_')) {
    $c = Join-Path $wow "$fl\Interface\AddOns\EllesmereUI"
    if (Test-EUI $c) { return [IO.Path]::GetFullPath($c) }
  }
  return $null
}
function Resolve-EUI {
  try {
    foreach ($pn in 'Wow', 'WowClassic', 'WowT', 'WowB') {
      $p = Get-Process -Name $pn -ErrorAction SilentlyContinue | Select-Object -First 1
      if ($p -and $p.Path) {
        $fd = Split-Path $p.Path -Parent
        $r = FromWoWBase (Split-Path $fd -Parent); if ($r) { return $r }
        $r = Join-Path $fd 'Interface\AddOns\EllesmereUI'; if (Test-EUI $r) { return [IO.Path]::GetFullPath($r) }
      }
    }
  } catch {}
  try {
    foreach ($k in 'HKLM:\SOFTWARE\WOW6432Node\Blizzard Entertainment\World of Warcraft', 'HKLM:\SOFTWARE\Blizzard Entertainment\World of Warcraft', 'HKCU:\SOFTWARE\Blizzard Entertainment\World of Warcraft') {
      $ip = (Get-ItemProperty -Path $k -ErrorAction SilentlyContinue).InstallPath
      if ($ip) {
        $r = Join-Path $ip 'Interface\AddOns\EllesmereUI'; if (Test-EUI $r) { return [IO.Path]::GetFullPath($r) }
        $r = FromWoWBase (Split-Path $ip -Parent); if ($r) { return $r }
      }
    }
  } catch {}
  $roots = @($env:ProgramFiles, ${env:ProgramFiles(x86)}, 'C:\Program Files (x86)', 'C:\Program Files', 'C:\Games', 'D:\Games')
  foreach ($dv in ([IO.DriveInfo]::GetDrives() | Where-Object { $_.IsReady -and $_.DriveType -eq 'Fixed' })) {
    $roots += $dv.RootDirectory.FullName; $roots += (Join-Path $dv.RootDirectory.FullName 'Games')
  }
  foreach ($rt in ($roots | Select-Object -Unique)) { if ($rt) { $r = FromWoWBase (Join-Path $rt 'World of Warcraft'); if ($r) { return $r } } }
  $d = $Root
  for ($i = 0; $i -lt 8 -and $d; $i++) {
    if (Test-EUI (Join-Path $d 'EllesmereUI')) { return [IO.Path]::GetFullPath((Join-Path $d 'EllesmereUI')) }
    $d = Split-Path $d -Parent
  }
  return $null
}

$eui = Resolve-EUI
if (-not $eui) {
  Say 'EllesmereUI not found. Install EllesmereUI first, then re-run.' 'EllesmereUI 를 찾지 못했습니다. EllesmereUI 를 먼저 설치한 뒤 다시 실행하세요.' Red
  return
}
$addons = Split-Path $eui -Parent
Write-Host ("  AddOns : " + $addons) -ForegroundColor DarkGray
Write-Host ''

# ---- 권한: 대상 폴더에 쓸 수 없고 관리자도 아니면 → 관리자로 자동 재실행(UAC). 쓸 수 있으면 그냥 진행 ----
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
$canWrite = $false
try { $tf = Join-Path $addons ('.wtest_' + [IO.Path]::GetRandomFileName()); Set-Content -LiteralPath $tf -Value 'x' -ErrorAction Stop; Remove-Item -LiteralPath $tf -Force; $canWrite = $true } catch {}
if (-not $canWrite -and -not $isAdmin) {
  Say 'This game folder needs admin rights. A Windows prompt appears - click Yes.' '게임 폴더에 쓰려면 관리자 권한이 필요합니다. 잠시 후 뜨는 [예]를 누르세요.' Yellow
  try { Start-Process -FilePath $env:ComSpec -ArgumentList '/c', ('"' + $s + '"') -Verb RunAs | Out-Null }
  catch { Say 'Cancelled - right-click this file and pick "Run as administrator".' '취소됨 - 이 파일을 우클릭하고 "관리자 권한으로 실행"을 선택하세요.' Red }
  return
}

# ---- 폴더가 있어도 강제 덮어쓰기 (기존 삭제 후 새로 복사) ----
$dest = Join-Path $addons 'EllesmereUI_koKR_Plus'
$srcFull = [IO.Path]::GetFullPath($Root).TrimEnd('\')
$dstFull = [IO.Path]::GetFullPath($dest).TrimEnd('\')
if ($srcFull -ieq $dstFull) {
  Say 'Already running from the AddOns folder (no copy needed).' 'AddOns 폴더에서 실행 중 (복사 불필요).' Yellow
} else {
  try {
    if (Test-Path $dest) { Remove-Item $dest -Recurse -Force -ErrorAction SilentlyContinue }   # 기존 폴더 강제 제거(잠긴 파일 무시)
    Copy-Item $Root $dest -Recurse -Force                                                        # 강제 덮어쓰기 복사
    Get-ChildItem $dest -Filter *.bat -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue
    Say ('Copied  ->  ' + $dest) ('복사 완료  ->  ' + $dest) Green
  } catch {
    Say 'Copy failed - close WoW completely and run this installer again.' '복사 실패 - WoW를 완전히 종료한 뒤 이 설치기를 다시 실행하세요.' Red
    return
  }
}

Write-Host ''
Say 'DONE. In-game: enable "EllesmereUI_koKR_Plus" in the AddOns list, then /reload.' '완료. 게임에서 애드온 목록의 "EllesmereUI_koKR_Plus" 를 켜고 /reload 하세요.' Cyan
