@echo off
SETLOCAL EnableExtensions DisableDelayedExpansion
if not "%~1"=="--help" if not "%~1"=="?" if not "%~1"=="/?" goto NoHelp
echo Runs World of Warcraft Server, wows-transmogrify.cmd and Client
echo Syntax: wows-run [reload ^| sqlonly ^| nowow]
exit /b 1

:NoHelp

set RUNASDATE=%~1
REM Syntax like "31\12\2013 01:45:00", stop/reload not supported
if not defined RUNASDATE goto SkipDate
if not "%RUNASDATE:~2,1%"=="\" if not "%RUNASDATE:~5,1%"=="\" if not "%RUNASDATE:~13,1%"==":" if not "%RUNASDATE:~16,1%"==":" set RUNASDATE=& goto SkipDate
shift /1
set "RUNASDATE=RunAsDate.exe /movetime %RUNASDATE%"
echo %RUNASDATE%
:SkipDate

if /I 'reload'=='%1' call "%~dp0wows-stop.cmd" keepsql wait

:CheckSQL
call "%~dp0wows-where.cmd" tasklist.exe& if errorlevel 1 exit /b 1

pushd "%BASEDIR%\_MySQL\bin\"
if errorlevel 1 echo FATAL! Cannot find MySQL directory "%BASEDIR%\_MySQL\bin\" ...& pause>nul& exit /b 1

for %%I in ("%COMSPEC%") do %FOR_TASKLIST% DO (
  SETLOCAL EnableDelayedExpansion
  set WHAT=%%~L
  if defined WHAT set WHAT=!WHAT:  = !& set WHAT=!WHAT:*: =!
  if "!WHAT!"=="MySQL for WoW" (ENDLOCAL& goto DoneMySQL) else (
  if "!WHAT!"=="MySQL.bat" (ENDLOCAL& goto DoneMySQL) else ENDLOCAL )
)

set CNF=my.ini
if not exist %CNF% set CNF=my.cnf
if not exist %CNF% echo FATAL! Cannot find MySQL config file ("%CNF%") ...& pause>nul& exit /b 1
start /min cmd /c "title MySQL for WoW& mysqld --defaults-file=%CNF% --standalone --console --max-allowed-packet=10000000& if errorlevel 1 title QuitErr - WoW MySQL& pause>nul"
echo Waiting for MySQL server is started...
if not exist mysqladmin.exe ping -n 1 -l 2 -w 3000 127.255.255.255 >nul& goto DoneMySQL
:WaitMySQL
ping -n 1 -l 2 -w 300 127.255.255.255 >nul
mysqladmin.exe --user=root --password=ascent ping 2>nul
if errorlevel 1 goto WaitMySQL
:DoneMySQL
popd
if /I 'sqlonly'=='%1' exit /b 0
if /I '/'=='%1' exit /b 0

pushd "%BASEDIR%\"
if errorlevel 1 echo FATAL! Cannot find Repack directory "%BASEDIR%\" ...& pause>nul& exit /b 1
set "AUTHSERVER="
for %%I in (authserver.exe realmd.exe oregon-realm.exe logon.exe trinityrealm.exe hellgroundrealm.exe) DO if not defined AUTHSERVER if exist %%I set AUTHSERVER=%%I
if not defined AUTHSERVER echo FATAL! Cannot find authserver.exe / realmd.exe / ...& pause>nul& exit /b 1
set "WORLDSERVER="
for %%I in (worldserver.exe mangosd.exe oregon-core.exe world.exe trinitycore.exe hellgroundcore.exe) DO if not defined WORLDSERVER if exist %%I set WORLDSERVER=%%I
if not defined WORLDSERVER echo FATAL! Cannot find worldserver.exe / mangosd.exe / ...& pause>nul& exit /b 1

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" (ENDLOCAL& set AUTHSERVER=) else (
  if "!WHAT!"=="worldserver for WoW" (ENDLOCAL& set WORLDSERVER=) else ENDLOCAL )
)
if not defined AUTHSERVER  (echo Warning! authserver for WoW is already running) else start /min cmd /c "TITLE authserver for WoW& %RUNASDATE% "%BASEDIR%\%AUTHSERVER%""
if not defined WORLDSERVER (echo Warning! worldserver for WoW is already running) else del "%BASEDIR%\*Errors.log" 2>nul & start /min cmd /c "TITLE worldserver for WoW& %RUNASDATE% "%BASEDIR%\%WORLDSERVER%"" & echo Wait for beep from %WORLDSERVER% and then submit logon from WoW.exe
popd
if /I 'nowow'=='%1' goto SkipWoWExe
if not "%BASEDIR:-nowow=%"=="%BASEDIR%" goto SkipWoWExe
REM Под WinPE 2003 было такое, что tlist.exe -m WoW.exe (1.12 Classic) выдаёт "not found" (список модулей недоступен даже для ProcessHacker), хотя для других .exe, включая WoW.exe (3.3.5a WotLK) всё работало
for %%I in ("%WOWDIR%\WoW.exe") do %FOR_TASKLIST% DO if "%%~J" GTR "0" if "%%~J" LSS "9876543210" echo Warning! WoW.exe is already running& goto SkipWoWExe
if exist .\wows-transmogrify.cmd call .\wows-transmogrify.cmd
if not exist Screenshots 2>nul (md %BASEDIR%\Screenshots & ncopy -mJ %BASEDIR%\Screenshots Screenshots)
start WoW.exe -console
:SkipWoWExe
exit /b 0
