@echo off
SETLOCAL EnableExtensions DisableDelayedExpansion
if not "%~1"=="--help" if not "%~1"=="?" if not "%~1"=="/?" goto NoHelp
echo Unpacks World of Warcraft Repack (deletes Client Cache in the process)...
echo ... and optionally unpacks additional Databases (for development purposes)
echo Syntax: wows-unpack.cmd [_repack_name] [_database_name ...] [/ ^| //]
echo When run without arguments it unpacks the default repack
echo /   Do not unpack mmaps
echo //  Do not unpack DBC, maps, vmaps, mmaps and do not clear WDB cache
echo Attribute Read-Only on WDB or Cache folder disables its auto-delete.
echo Example, unpack the default repack and many other databases:
echo   wows-unpack "" tbc ytdb USE world_bliz Q:\Work udb psdb tc WAIT
exit /b 1

:NoHelp
if defined FARLANG set FARLANG=& start cmd /c; %0 %*& goto :EOF
set "REPACK=%~1"
if defined REPACK if /I not "%REPACK:~0,4%"=="wow-" (
   echo Unpacking additional databases...
   call "%~dp0wows-where.cmd" arc.exe || exit /b 1
   call .\wows-run.cmd sqlonly
   goto CheckDB
)
call "%~dp0wows-where.cmd" create 7z.exe || exit /b 1
cd /D "%WOWDIR%\repack" || (echo Error! Cannot chdir to "%WOWDIR%\repack\" folder& pause>nul)

if not defined REPACK (
 if "%WOWVER%"=="1.12" set "REPACK=WoW-1.12-cMangos"
 if "%WOWVER%"=="2.4.3" set "REPACK=WoW-2.4.3-HGcore"
 if "%WOWVER%"=="3.3.5a" set "REPACK=WoW-3.3.5a-Sunwell"
 if "%WOWVER%"=="4.0.6a" set "REPACK=WoW-4.0.6a-Farbod-rv400a"
 if "%WOWVER%"=="4.3.4" set "REPACK=WoW-4.3.4-WowEmu"
 if not defined REPACK echo Error! Cannot determine default REPACK name from WoW version& echo Look into "WowServerRepacks.7z" archive and manually specify repack name as command line argument ...& pause>nul& exit /b 1
)

7z x -o"%BASEDIR%" "WowServerRepacks.7z" "%REPACK%\*"
if  "%ERRORLEVEL%"=="9009" echo FATAL! cannot execute 7z.exe& pause>nul& exit /b 1
if not "%ERRORLEVEL%"=="0" echo FATAL! cannot extract from repack archive "%REPACK%" ...& pause>nul& exit /b 1

set MAPS=
set VMAPS=
set MMAPS=
for /D %%D in ("%BASEDIR%\%REPACK%") DO (
 set "BASEDIR=%%~fD"
 if /I "%%~nxD"=="WoW-1.12-AlterWoW-3.5" set "MAPS=maps 1.12 [mangoszero]"& set "VMAPS=vmaps 1.12 [mangoszero]"
 if /I "%%~nxD"=="WoW-1.12-cMangos"      set "MAPS=maps 1.12 [cMangos]"& set "VMAPS=vmaps 1.12 [cMangos]"& set "MMaps=mmaps-1.12-cMangos.7z"
 if /I "%%~nxD"=="WoW-1.12-Nostalrius"   set "MAPS=maps 1.12 [Nostalrius]"& set "VMAPS=vmaps 1.12 [Nostalrius]"& set "MMaps=mmaps-1.12-Nostalrius.7z"
 if /I "%%~nxD"=="WoW-2.4.3-EpicBlood-v2"       set "MAPS=maps 2.4.3 [EpicBlood Oscarlius]"& set "VMAPS=vmaps 2.4.3 [EpicBlood]"
 if /I "%%~nxD"=="WoW-2.4.3-Persian-2013-06-19" set "MAPS=maps 2.4.3 [EpicBlood Oscarlius]"& set "VMAPS=vmaps 2.4.3 [EpicBlood]"
 if /I "%%~nxD"=="WoW-2.4.3-CMaNGOS"            set "MAPS=maps 2.4.3 [CMaNGOS]"& set "VMAPS=vmaps 2.4.3 [CMaNGOS]"& set "mmaps=mmaps-2.4.3-CMaNGOS.7z"
 if /I "%%~nxD"=="WoW-2.4.3-HGcore"             set "MAPS=maps 2.4.3 [HGcore]"& set "VMAPS=vmaps 2.4.3 [HGcore]"& set "MMaps=mmaps-2.4.3-HGcore.7z"
 if /I "%%~nxD"=="WoW-3.3.5a-CNA-Lite-v2"  set "MAPS=maps 3.3.5a [jeutie 9.6.0-9.6.7 JZY 1.3]"&  set "VMAPS=vmaps 3.3.5a [JZY]"
 if /I "%%~nxD"=="WoW-3.3.5a-MangosR2-Staford.rev6" set "MAPS=maps 3.3.5a [MangosR2]"& set "VMAPS=vmaps 3.3.5a [MangosR2]"& set "mmaps=mmaps-3.3.5a-MangosR2-Staford.rev7.7z"
 if /I "%%~nxD"=="WoW-3.3.5a-MangosR2-Staford.rev7" set "MAPS=maps 3.3.5a [MangosR2]"& set "VMAPS=vmaps 3.3.5a [MangosR2]"& set "mmaps=mmaps-3.3.5a-MangosR2-Staford.rev7.7z"
 if /I "%%~nxD"=="WoW-3.3.5a-Quantum-11.5.QDB.8.0"  set "MAPS=maps 3.3.5a [jeutie 9.6.8]"& set "VMAPS=vmaps 3.3.5a [jeutie 9.6.8]"
 if /I "%%~nxD"=="WoW-3.3.5a-Sunwell"      set "MAPS=maps 3.3.5a [jeutie 9.6.8]"& set "VMAPS=vmaps 3.3.5a [acweb 0.6]"& set "MMaps=mmaps-3.3.5a-Sunwell.7z"
 if /I "%%~nxD"=="WoW-3.3.5a-JZY-v3.13"    set "MAPS=maps 3.3.5a [jeutie 9.6.0-9.6.7 JZY 1.3]"&  set "VMAPS=vmaps 3.3.5a [JZY]"
 if /I "%%~nxD"=="WoW-3.3.5a-Jeutie-9.6.7" set "MAPS=maps 3.3.5a [jeutie 9.6.0-9.6.7 JZY 1.3]"& set "VMAPS=vmaps 3.3.5a [jeutie 9.6.6]"
 if /I "%%~nxD"=="WoW-3.3.5a-Jeutie-9.6.8" set "MAPS=maps 3.3.5a [jeutie 9.6.8]"& set "VMAPS=vmaps 3.3.5a [jeutie 9.6.8]"
 if /I "%%~nxD"=="WoW-3.3.5a-Jeutie-9.7.0.1" set "MAPS=maps 3.3.5a [jeutie 9.6.8]"& set "VMAPS=vmaps 3.3.5a [jeutie 9.6.8]"
 if /I "%%~nxD"=="WoW-3.3.5a-Jeutie-9.7.5" set "MAPS=maps 3.3.5a [jeutie 9.6.8]"& set "VMAPS=vmaps 3.3.5a [jeutie 9.6.8]"
 if /I "%%~nxD"=="WoW-3.3.5a-Jeutie-9.7.8" set "MAPS=maps 3.3.5a [jeutie 9.7.6]"& set "VMAPS=vmaps 3.3.5a [jeutie 9.7.6]"& set "MMAPS=mmaps-3.3.5a-Jeutie-9.7.6.7z"
 if /I "%%~nxD"=="WoW-3.3.5a-Jeutie-9.8.4" set "MAPS=maps 3.3.5a [jeutie 9.7.6]"& set "VMAPS=vmaps 3.3.5a [jeutie 9.8.0]"& set "MMAPS=mmaps-3.3.5a-Jeutie-9.8.0.7z"
 if /I "%%~nxD"=="WoW-3.3.5a-Jeutie-0.9.0" set "MAPS=maps 3.3.5a [jeutie 9.7.6]"& set "VMAPS=vmaps 3.3.5a [jeutie 9.8.0]"& set "MMAPS=mmaps-3.3.5a-Jeutie-9.8.5.7z"
 if /I "%%~nxD"=="WoW-3.3.5a-Jeutie-r1.0"  set "MAPS=maps 3.3.5a [jeutie r1.0]"& set "VMAPS=vmaps 3.3.5a [acweb 0.6]"& set "MMAPS=mmaps-3.3.5a-acweb-0.6.7z"
 if /I "%%~nxD"=="WoW-3.3.5a-Jeutie-r1.7"  set "MAPS=maps 3.3.5a [jeutie r1.0]"& set "VMAPS=vmaps 3.3.5a [acweb 0.6]"& set "MMAPS=mmaps-3.3.5a-acweb-0.6.7z"
 if /I "%%~nxD"=="WoW-3.3.5a-acweb-0.6"    set "MAPS=maps 3.3.5a [acweb 0.6]"&   set "VMAPS=vmaps 3.3.5a [acweb 0.6]"& set "MMAPS=mmaps-3.3.5a-acweb-0.6.7z"
 if /I "%%~nxD"=="WoW-3.3.5a-ElunaBotCore-V8"   set "MAPS=maps 3.3.5a [jeutie r1.0]"& set "VMAPS=vmaps 3.3.5a [acweb 0.6]"& set "mmaps-3.3.5a-MangosR2-Staford.rev7.7z"&REM mmaps v4 (1852m; 9.8.0?), но не v5 (acweb 0.6)
 if /I "%%~nxD"=="WoW-3.3.5a-NpcBot-2014-06-22" set "MAPS=maps 3.3.5a [jeutie r1.0]"& set "VMAPS=vmaps 3.3.5a [acweb 0.6]"& set "MMAPS=mmaps-3.3.5a-acweb-0.6.7z"
 if /I "%%~nxD"=="WoW-4.0.6a-Eternion"         set "MAPS=maps 4.0.6a [Guldemor SolidWow Bgtracker]"& set "VMAPS=vmaps 4.0.6a [Skyfire-2012-06-03]"
 if /I "%%~nxD"=="WoW-4.0.6a-Deepshjir"        set "MAPS=maps 4.0.6a [Guldemor SolidWow Bgtracker]"& set "VMAPS=vmaps 4.0.6a [Skyfire-2012-06-03]"
 if /I "%%~nxD"=="WoW-4.0.6a-Solid-2012-02-20" set "MAPS=maps 4.0.6a [Guldemor SolidWow Bgtracker]"& set "VMAPS=vmaps 4.0.6a [Skyfire-2012-06-03]"
 if /I "%%~nxD"=="WoW-4.0.6a-Solid-2012-03-12" set "MAPS=maps 4.0.6a [jenascore SolidWoW]"& set "VMAPS=vmaps 4.0.6a [Skyfire-2012-06-03]"
 if /I "%%~nxD"=="WoW-4.0.6a-Guldemor-v16"     set "MAPS=maps 4.0.6a [Guldemor SolidWow Bgtracker]"& set "VMAPS=vmaps 4.0.6a [Skyfire-2012-06-03]"
 if /I "%%~nxD"=="WoW-4.0.6a-Farbod-rv400a"    set "MAPS=maps 4.0.6a [jenascore SolidWoW]"& set "VMAPS=vmaps 4.0.6a [Skyfire-2012-06-03]"
 if /I "%%~nxD"=="WoW-4.3.4-Holystone-3.0.0"   set "MAPS=maps 4.3.4 [zdroid]"& set "VMAPS=vmaps 4.3.4 [zdroid]"& set "mmaps=mmaps-4.3.4-Holystone-2.0.0.7z"
 if /I "%%~nxD"=="WoW-4.3.4-Farbod-2013-06-19" set "MAPS=maps 4.3.4 [zdroid]"& set "VMAPS=vmaps 4.3.4 [zdroid]"& set "mmaps=mmaps-4.3.4-BotCoreV7.7z"
 if /I "%%~nxD"=="WoW-4.3.4-BotCore-V7"        set "MAPS=maps 4.3.4 [zdroid]"& set "VMAPS=vmaps 4.3.4 [zdroid]"& set "mmaps=mmaps-4.3.4-BotCoreV7.7z"
 if /I "%%~nxD"=="WoW-4.3.4-BotCore-Redux"     set "MAPS=maps 4.3.4 [zdroid]"& set "VMAPS=vmaps 4.3.4 [zdroid]"& set "mmaps=mmaps-4.3.4-BotCoreV7.7z"
 if /I "%%~nxD"=="WoW-4.3.4-ScreenEmu-r5"      set "MAPS=maps 4.3.4 [zdroid]"& set "VMAPS=vmaps 4.3.4 [zdroid]"
 if /I "%%~nxD"=="WoW-4.3.4-zdroid-5.1.1.0"    set "MAPS=maps 4.3.4 [zdroid]"& set "VMAPS=vmaps 4.3.4 [zdroid]"
 if /I "%%~nxD"=="WoW-4.3.4-WowEmu"            set "MAPS=maps 4.3.4 [zdroid]"& set "VMAPS=vmaps 4.3.4 [zdroid]"& set "MMAPS=mmaps-4.3.4-EmuCoach.7z"
 if /I "%%~nxD"=="WoW-4.3.4-EmuCoach"          set "MAPS=maps 4.3.4 [zdroid]"& set "VMAPS=vmaps 4.3.4 [zdroid]"& set "MMAPS=mmaps-4.3.4-EmuCoach.7z"
)

REM При удалении излишних .dll подразумевается, что все сборки и .dll для них - 32-битные
REM Удаление msvc?90.dll - только на XP/2003
if exist "%BASEDIR%\msvc?90.dll" if exist "%SystemRoot%\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_6f74963e\msvc?90.dll" del "%BASEDIR%\msvc?90.dll"
for %%I in ("%BASEDIR%\msvc*.dll") DO ^
if exist "%SystemRoot%\SysWOW64\" (if exist "%SystemRoot%\SysWOW64\%%~nxI" echo Found "%SystemRoot%\SysWOW64\%%~nxI", so deleting "%%~fI"& del "%%~fI") else ^
if exist "%SystemRoot%\System32\%%~nxI" if not defined ProgramW6432 echo Found "%SystemRoot%\System32\%%~nxI", so deleting "%%~fI"& del "%%~fI"

if _%2==_// shift /2& goto SkipData
if _%2==_/ shift /2& set MMAPS=
if not defined MAPS echo Error! Cannot determine MAPS name from REPACK name
if not defined VMAPS echo Error! Cannot determine VMAPS name from REPACK name
if not exist "%BASEDIR%\" echo Error! "%BASEDIR%\" is not created ...& pause>nul& exit /b 1
if not defined MAPS pause& exit /b 1
if not defined VMAPS pause& exit /b 1

REM arc быстрее и сильнее(против стандартного максимального 7z со словарём 64МБ) сжимает repack и maps, 7z чуть сильнее сжимает vmaps, и заметно сильнее - mmaps
REM 7z v9.20 сжимает vmaps чуть лучше, чем v16.02
REM при использовании 7z для mmaps нет выигрыша от объединения в один архив 3.3.5a и 4.0.6a

echo s|7z e -o"%BASEDIR%\DBC" -xr!*\*\* dbc.7z "dbc %WOWVER%\*"& (if not exist "%BASEDIR%\DBC\*.dbc" verify ERR1 2>nul) || (echo Error! failed extracting dbc.7z ...& pause>nul)
REM rar x -ep1 maps.rar "%MAPS%\*" "%BASEDIR%\maps\"
echo s|7z e -o"%BASEDIR%\maps" -xr!*\*\* maps.7z "%MAPS%\*"& (if not exist "%BASEDIR%\maps\*.map*" verify ERR1 2>nul) || (echo Error! failed extracting maps.7z ...& pause>nul)
echo s|7z e -o"%BASEDIR%\vmaps" -xr!*\*\*  vmaps.7z "%VMAPS%\*"& (if not exist "%BASEDIR%\vmaps\*.vm*" verify ERR1 2>nul) || (echo Error! failed extracting vmaps.7z ...& pause>nul)

if not exist "%MMAPS%" (FOR /F "delims==" %%N IN ('set MMAPS 2^>nul') DO if "%%N"=="MMaps" echo Missing MMaps "%MMaps%"...& pause>nul) ^
else echo s|7z e -o"%BASEDIR%\mmaps" "%MMAPS%"& (if not exist "%BASEDIR%\mmaps\*.mm*" verify ERR1 2>nul) || (echo Warning! failed extracting "%MMAPS%" ...& pause>nul)

if /I "%WOWVER:~0,1%" GEQ "4" (
   rd /q /s "..\Data\Cache" 2>nul
   REM "..\Data\Cache" может быть файлом нулевого размера, для запрета MPQ-кэширования
   if not exist "..\Data\Cache" md "%BASEDIR%\DataCache"& ncopy.exe -mJ "%BASEDIR%\DataCache" "..\Data\Cache"
)
REM WDB-кэш клиента WoW можно удалять и вручную - тогда, когда меняется сборка.
REM Если wow.exe уже запущен прежде этого .cmd файла, то WDB от прежней
REM сборки останется, и это может приводить к некорректной работе клиента
for %%I in (..\WDB ..\Cache) DO (
 if "%%~aI" GEQ "dr" if "%%~aI" LEQ "drz" goto doneWDB
 if not "%BASEDIR:1.12-cMangos=%%BASEDIR:3.3.5a-Sunwell=%"=="%BASEDIR%%BASEDIR%" (
    if exist "%%~I0\" (
       rd /q /s "..\Cache" 2>nul & rd /q /s "..\WDB" 2>nul
       ren "%%~I0\" "%%~nxI"& REM Восстановить кэш основной сборки из WDB0 или Cache0
       goto doneWDB
    )
    if exist "%%~I" goto doneWDB
 ) else if exist "%%~I\*" (
       if not exist "%%~I0" ren "%%~I\" "%%~nxI0"& REM Кэш основной сборки не удаляется, а переименовывается в WDB0 или Cache0
       if exist "..\Cache\*" rd /q /s "..\Cache" else if exist "..\WDB\*.wdb" rd /q /s "..\WDB"
       goto doneWDB
 )
)
:doneWDB

:SkipData
if exist "%BASEDIR%\_Server\setup_xampp.bat" (
   REM Needed for WoW-4.0.6a-Guldemor-v11
   pushd "%BASEDIR%\_Server"
   call setup_xampp.bat
   popd
)

set WORLDSERVER=worldserver
if exist "%BASEDIR%\mangosd.exe" set WORLDSERVER=mangosd
if exist "%BASEDIR%\oregon-core.exe" set WORLDSERVER=oregoncore
if exist "%BASEDIR%\trinitycore.exe" set WORLDSERVER=trinitycore
if exist "%BASEDIR%\hellgroundcore.exe" set WORLDSERVER=hellgroundcore
if exist "%REPACK%-%WORLDSERVER%.exe" (
 echo Note: using main executable override %REPACK%-%WORLDSERVER%.exe
 copy /Y "%REPACK%-%WORLDSERVER%.exe" "%BASEDIR%\%WORLDSERVER%.exe" 1>nul
)
if exist "%REPACK%-%WORLDSERVER%.conf" (
 echo Note: using config override %REPACK%-%WORLDSERVER%.conf
 ren "%BASEDIR%\%WORLDSERVER%.conf" "%WORLDSERVER%.conf#orig"
 copy /Y "%REPACK%-%WORLDSERVER%.conf" "%BASEDIR%\%WORLDSERVER%.conf" 1>nul || (echo Error! copying %REPACK%-%WORLDSERVER%.conf ...& pause>nul& exit /b 1)
) else if exist "%WORLDSERVER%.conf" (
 echo Note: using config override %REPACK%-%WORLDSERVER%.conf
 ren "%BASEDIR%\%WORLDSERVER%.conf" "%WORLDSERVER%.conf#orig"
 copy "%WORLDSERVER%.conf" "%BASEDIR%\" 1>nul || (echo Error! copying %WORLDSERVER%.conf ...& pause>nul& exit /b 1)
) else (
 echo Note: using original %WORLDSERVER%.conf
)
cd ..& FOR /F "tokens=1* delims=" %%F IN ('dir /b /aa-h /on wows-*patch*.cmd 2^>nul') DO if not errorlevel 1 call "%%F" INITIAL || (echo Patch Error! Press a key to continue...& pause>nul)
echo World of Warcraft server is unpacked to "%BASEDIR%\"
echo    DBC:    "dbc %WOWVER%"
echo    Maps:   "%MAPS%"
echo    Vmaps:  "%VMAPS%"
if defined MMAPS echo   Mmaps:  "%MMAPS%"
:NextDB
shift /1
:CheckDB
set WHAT=
if "%~1"=="" exit /b 0
if /I "%~1"=="sdb"    call :UnpackDB world_sdb    "arc e -ap=SDB-2007-02-04 %WOWDIR%\stuff\_devel\CoreMangos-src.arc *.sql"
if /I "%~1"=="mz2011" call :UnpackDB world_mz2011 "arc e -ap=mangoszero-2011-11-18\_MZDB\zp_world %WOWDIR%\stuff\_devel\CoreMangos-src.arc *.sql"
if /I "%~1"=="mz"     call :UnpackDB world_mz     "arc e -ap=mangoszero-2014-Rel19\_MZDB-Rel19 %WOWDIR%\stuff\_devel\CoreMangos-src.arc _full_db\*.sql"
if /I "%~1"=="cldb0"  call :UnpackDB world_cldb0  "arc e -ap=cmangos-2013-10-07\_ClassicDB -x*\*\* %WOWDIR%\stuff\_devel\CoreMangos-src.arc Full_DB\*.sql"
if /I "%~1"=="cldb"   call :UnpackDB world_cldb   "arc e -ap=cmangos-2013-10-07\_ClassicDB -x*\*\* %WOWDIR%\stuff\_devel\CoreMangos-src.arc Full_DB\*.sql updates\*.sql"
if /I "%~1"=="cldbn"  call :UnpackDB world_cldbn  "arc e -ap=cmangos-2013-10-07\_ClassicDB -x*\*\* %WOWDIR%\stuff\_devel\CoreMangos-src.arc Full_DB\*.sql updates.bak\*.sql cldbn_revert.sql"
if /I "%~1"=="nost"   call :UnpackDB world_nost   "arc e -ap=Nostalrius-Brotalnia\_DB %WOWDIR%\stuff\_devel\CoreMangos-src.arc WORLD_FULL_DB_11_03_2018.sql"
if /I "%~1"=="nostn"  call :UnpackDB world_nostn  "arc e -ap=Nostalrius-Brotalnia-v10\_DB %WOWDIR%\stuff\_devel\CoreMangos-src.arc FULL_WORLD_DB_09_05_2018.sql"
if /I "%~1"=="tbc230" call :UnpackDB world_tbc230 "arc x -ap=TBC230  -x*\* %WOWDIR%\stuff\_devel\YTDB-YTDBTC-UDB-PSDB-TBCDB.arc *.sql"
if /I "%~1"=="tbc"    call :UnpackDB world_tbc    "arc x -ap=TBCDB   -x*\* %WOWDIR%\stuff\_devel\YTDB-YTDBTC-UDB-PSDB-TBCDB.arc *.sql"
if /I "%~1"=="bliz"   call :UnpackDB world_bliz   "arc e -ap=CMangos-TBC-2013\sql\development\world %WOWDIR%\stuff\_devel\CoreTBC-src.arc *.sql"
if /I "%~1"=="hgc"    call :UnpackDB world_hgc    "arc e -ap=HGcore-2014-07-19\HGDB -x*\*\*\* %WOWDIR%\stuff\_devel\CoreTBC-src.arc structures\world_struct_data.sql updates\world\*.sql"
if /I "%~1"=="oreg0"  call :UnpackDB world_oreg0  "arc e -ap=OregonCore-2012-05-29\sql %WOWDIR%\stuff\_devel\CoreTBC-src.arc oregon_database.sql updates\world\*.sql"
if /I "%~1"=="oreg"   call :UnpackDB world_oreg   "arc e -ap=OregonCore-2016-02-08\sql %WOWDIR%\stuff\_devel\CoreTBC-src.arc oregon_database.sql updates\world\*.sql"
if /I "%~1"=="loe"    call :UnpackDB world_loe    "arc x -ap=db-landofelves\arcemu_world %WOWDIR%\stuff\_devel\ArcEmu-src.arc *.sql"
if /I "%~1"=="udb"    call :UnpackDB world_udb    "arc x -ap=UDB     -x*\* %WOWDIR%\stuff\_devel\YTDB-YTDBTC-UDB-PSDB-TBCDB.arc *_mangos*.sql"
if /I "%~1"=="psdb"   call :UnpackDB world_psdb   "arc x -ap=PSDB    -x*\* %WOWDIR%\stuff\_devel\YTDB-YTDBTC-UDB-PSDB-TBCDB.arc *.sql"
if /I "%~1"=="ytdb"   call :UnpackDB world_ytdb   "arc x -ap=YTDB    -x*\* %WOWDIR%\stuff\_devel\YTDB-YTDBTC-UDB-PSDB-TBCDB.arc ytdb*.sql *_mangos*.sql"
if /I "%~1"=="ytdbtc" call :UnpackDB world_ytdbtc "arc x -ap=YTDB-TC -x*\* %WOWDIR%\stuff\_devel\YTDB-YTDBTC-UDB-PSDB-TBCDB.arc *.sql -x*_auth_*.sql"
if /I "%~1"=="jzy"    call :UnpackDB world_jzy    "arc e -ap=JZY-Database-3.13 %WOWDIR%\stuff\_devel\CoreWotLK-src.arc world-V3.13.sql"
if /I "%~1"=="tc975"  call :UnpackDB world_tc975  "arc e -ap=TrinityCore-Jeutie-9.7.5\sql %WOWDIR%\stuff\_devel\CoreWotLK-src.arc base\*TDB_full_*.sql updates\world\*.sql"
if /I "%~1"=="tc"     call :UnpackDB world_tc     "arc e -ap=TrinityCore-Jeutie-r1.0\sql %WOWDIR%\stuff\_devel\CoreWotLK-src.arc base\*TDB_full_*.sql updates\world\*.sql" "TDB_full_*.sql"
if /I "%~1"=="sw"     call :UnpackDB world_sw     "arc e -ap=SunwellCore-acweb\sql %WOWDIR%\stuff\_devel\CoreWotLK-src.arc databases_without_world_updates\world.sql world*updates\*.sql"
if /I "%~1"=="WAIT" echo Waiting... press a key to continue...& pause>nul& goto NextDB
if /I "%~1"=="USE"  call :UnpackDB %2 "cd /D" %3& shift /1& shift /1& goto NextDB& REM Распаковывает в заданную директорию для дальнейшего использования
if not defined WHAT echo Warning! Cannot recognize "%~1" as DB name or command...& pause>nul
goto NextDB

:UnpackDB
set WHAT=true
SETLOCAL
cd /D %TMP%
rd /s /q "%TMP%\$$UNPDB$$.TMP" 2>nul
md $$UNPDB$$.TMP
cd $$UNPDB$$.TMP || (echo Error! mkdir/chdir...& pause>nul& goto :EOF)
echo on
%~2
@if errorlevel 1 echo Error! source SQL files not obtained...& pause>nul& goto :EOF
echo DROP DATABASE IF EXISTS %1; CREATE DATABASE %1 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; | "%BASEDIR%\_MySQL\bin\mysql.exe" --user=root --password=ascent || @(echo Error! mysql.exe @ DB drop/create...& pause>nul& goto :EOF)
@echo off
set COND=GEQ
if not _%3==_ FOR %%F IN (%3) DO echo %%F  [%%~zF]& "%BASEDIR%\_MySQL\bin\mysql.exe" --user=root --password=ascent --default-character-set=utf8 %1 <%%F && (attrib +H %%F || (echo Error! cannot set attrib +H...& pause>nul)) || (echo Error! mysql.exe ...& pause>nul)
REM сначала грузится основная база (файл большого размера), потом дополнения/исправления
:NextPass
FOR /F "tokens=1* delims=" %%F IN ('dir /b /one *.sql 2^>nul') DO if %%~zF %COND% 20000000 echo %%F  [%%~zF]& "%BASEDIR%\_MySQL\bin\mysql.exe" --user=root --password=ascent --default-character-set=utf8 %1 <%%F || (echo Error! mysql.exe ...& pause>nul)
if "%COND%"=="GEQ" set COND=LSS& goto NextPass
cd /D %TMP%
rd /s /q %TMP%\$$UNPDB$$.TMP
goto :EOF
