@echo off
SETLOCAL EnableExtensions DisableDelayedExpansion
if not "%~1"=="--help" if not "%~1"=="?" if not "%~1"=="/?" goto NoHelp
echo Stops World of Warcraft Server, optionally keeping MySQL alive
echo Syntax: wows-stop [keepsql] [wait]
exit /b 1

:NoHelp
call "%~dp0wows-where.cmd" tasklist.exe taskkill.exe

set KEEPSQL=
if /I _%1==_keepsql shift /1& set KEEPSQL=yes
if /I _%1==_/ shift /1& set KEEPSQL=yes
if not defined KEEPSQL if defined WOWDIR rd "%WOWDIR%\Screenshots" 2>nul &REM Удаление Junction на RAM-диск
REM mangosd.exe может падать, если выполняет нормальное закрытие в то время,
REM когда MySQL выполняет своё собственное закрытие и поэтому выдаёт ошибки

set WAITLIST="authserver for WoW" "worldserver for WoW"
for %%I in ("%COMSPEC%") do %FOR_TASKLIST% DO (
  SETLOCAL EnableDelayedExpansion
  set WHAT=%%~L
  if defined WHAT set WHAT=!WHAT:  = !& set WHAT=!WHAT:*: =!
  if "!WHAT!"=="authserver for WoW" (
    if not defined KEEPSQL (
      call :AuthKill
    ) else (
    REM Persian authserver надо посылать сигнал завершения два раза (или сразу убивать)
    for %%F in ("%BASEDIR%\authserver.exe") do if "%%~zF"=="422400" %TASKKILL_CLOSE_J%
    %TASKKILL_CLOSE_J%
    )
    ENDLOCAL

  ) else if "!WHAT!"=="worldserver for WoW" (
    if not defined KEEPSQL (
      call :ServerKill
    ) else (
    REM StafordRev7 и Hellground приходится убивать сразу, иначе они падают на нормальном завершении
    for %%F in ("%BASEDIR%\mangosd.exe") do if "%%~zF"=="11597824" set WHAT=& %TASKKILL_FORCE% "mangosd.exe" 2>nul
    if exist "%BASEDIR%\hellgroundcore.exe" set WHAT=& %TASKKILL_FORCE% "hellgroundcore.exe" 2>nul
    if defined WHAT %TASKKILL_CLOSE_J%
    )
    ENDLOCAL

  ) else if not defined KEEPSQL (

  if "!WHAT!"=="MySQL.bat" (
    %TASKKILL_CLOSE_J%
    ENDLOCAL
    set WAITLIST=%WAITLIST% "MySQL.bat"

  ) else if "!WHAT!"=="MySQL for WoW" (
    "%BASEDIR%\_MySQL\bin\mysqladmin" --user=root --password=ascent shutdown
    if not errorlevel 1 echo Performing MySQL safe shutdown
    if errorlevel 1 %TASKKILL_CLOSE_J%
    ENDLOCAL
    set WAITLIST=%WAITLIST% "MySQL for WoW"
  ) else ENDLOCAL

  ) else ENDLOCAL
)


if /I not _%1==_wait exit /b 0
echo Wait until windows are closed - %WAITLIST% ...
set COUNT=0
:Wait
if %COUNT% GEQ 0 set /A COUNT=(COUNT+1)
ping -n 1 -l 2 -w 300 127.255.255.255 >nul
for %%I in ("%COMSPEC%") do %FOR_TASKLIST% DO (
  SETLOCAL EnableDelayedExpansion
  set WHAT=%%~L
  if defined WHAT set WHAT=!WHAT:  = !& set WHAT=!WHAT:*: =!
  for %%T in (%WAITLIST%) DO if "!WHAT!"==%%T (
    echo   [!WHAT!]
    if %COUNT% GEQ 8 (
      set COUNT=-1
      if "!WHAT!"=="authserver for WoW"  call :AuthKill
      if "!WHAT!"=="worldserver for WoW" call :ServerKill
    )
    ENDLOCAL& goto Wait
  )
  ENDLOCAL
)
exit /b 0


:AuthKill
for %%F in (realmd.exe authserver.exe hellgroundrealm.exe trinityrealm.exe oregon-realm.exe logon.exe) DO if exist "%BASEDIR%\%%F" %TASKKILL_FORCE% "%%F" 2>nul
exit /b

:ServerKill
for %%F in (mangosd.exe worldserver.exe hellgroundcore.exe trinitycore.exe oregon-core.exe world.exe) DO if exist "%BASEDIR%\%%F" %TASKKILL_FORCE% "%%F" 2>nul
exit /b
