@echo off
SETLOCAL EnableExtensions DisableDelayedExpansion
if not "%~1"=="--help" if not "%~1"=="?" if not "%~1"=="/?" goto NoHelp
echo Saves World of Warcraft current state into .rar archive with automatically increasing number
echo Syntax: wows-save [safe]
exit /b 1

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

REM поддерживаются номера в пределах 1-100000
set NUM=1000
for /L %%n in (99,-1,1) DO (
 for %%F in ("wows-%WOWVER%-%%n???@.rar" "wows-%WOWVER%-%%n???-*@.rar") DO set /A NUM=%%n000+1000& goto FoundLimit
)
:FoundLimit
set NUM=1& for /L %%n in (%NUM%,-1,1) DO (
 for %%F in ("wows-%WOWVER%-%%n@.rar?" "wows-%WOWVER%-%%n-*@.rar") DO set /A NUM=%%n + 1&goto EndNum
)
:EndNum

if /I _%1==_safe call .\wows-stop.cmd keepsql wait
call .\wows-run.cmd sqlonly

pushd "%BASEDIR%"
del auth_characters.sql 2>nul
set AUTH=auth
if not exist "_MySQL\data\%AUTH%\*" set AUTH=realmd
_MySQL\bin\mysqldump.exe --user=root --password=ascent --single-transaction --add-drop-database --databases --result-file=auth_characters.sql %AUTH% characters
if errorlevel 1 echo Error! dumping DB auth_characters.sql ...& pause>nul& exit /b 1
popd
echo on
rar a -r -s -idq -ep1 -xWTF\*#*.wtf -xWTF\*#*.xml "wows-%WOWVER%-%NUM%@.rar" "%BASEDIR%\auth_characters.sql" WTF
@REM -x*#*.wtf -x*#*.xml 
@echo off
if errorlevel 1 echo Error! rar.exe reported ERRORLEVEL %ERRORLEVEL% ...& pause>nul& exit /b 1
echo Done: World of Warcraft state has been saved to "wows-%WOWVER%-%NUM%@.rar"
del "%BASEDIR%\auth_characters.sql"
exit /b 0
