@echo off
chcp 65001 >nul
setlocal EnableDelayedExpansion

for /f %%A in ('copy /Z "%~f0" nul') do set "CR=%%A"
set "TARGET_VERSION="

if not exist "paths.xml" if not exist "WorldOfTanks.exe" (
    cls
    echo =================================================================================
    echo                                      ERROR / ОШИБКА
    echo =================================================================================
    echo  It looks like you ran this directly from a ZIP/RAR  / Похоже, вы запустили это прямо из архива ZIP/RAR
    echo  archive, or placed it in the wrong folder.          / или поместили скрипт не в ту папку.
    echo.
    echo  1. EXTRACT all files from the downloaded archive.   / 1. ИЗВЛЕКИТЕ все файлы из скачанного архива.
    echo  2. Move them to your main World of Tanks folder.    / 2. Переместите их в главную папку World of Tanks.
    echo  3. Run install.bat from there.                      / 3. Запустите install.bat оттуда.
    echo =================================================================================
    pause >nul
    exit /b
)

if exist "paths.xml" (
    for /f "tokens=3 delims=><" %%A in ('findstr /C:"./res_mods/" paths.xml 2^>nul') do (
        set "RAW_PATH=%%A"
        set "TARGET_VERSION=!RAW_PATH:./res_mods/=!"
    )
)

if "!TARGET_VERSION!"=="" (
    for /f "delims=" %%V in ('dir /b /ad /o-n "res_mods" 2^>nul') do (
        if exist "mods\%%V\" (
            set "TARGET_VERSION=%%V"
            goto :version_found
        )
    )
)

:version_found
if "!TARGET_VERSION!"=="" (
    cls
    echo =================================================================================
    echo                                      ERROR / ОШИБКА
    echo =================================================================================
    echo  Game version could not be detected.                 / Не удалось определить версию игры.
    echo  Make sure this script is in the game folder.        / Убедитесь, что скрипт находится в папке с игрой.
    echo =================================================================================
    pause >nul
    exit /b
)

if not exist "res_mods\!TARGET_VERSION!" mkdir "res_mods\!TARGET_VERSION!"
if not exist "mods\!TARGET_VERSION!" mkdir "mods\!TARGET_VERSION!"

set "ARCHIVE=ByForMOD.tar"
set "DEST_DIR=res_mods\!TARGET_VERSION!"

if not exist "!ARCHIVE!" (
    cls
    echo =================================================================================
    echo                                      ERROR / ОШИБКА
    echo =================================================================================
    echo  Archive !ARCHIVE! was not found!                    / Архив !ARCHIVE! не найден!
    echo.
    echo  Did you only extract the install.bat file?          / Вы извлекли только файл install.bat?
    echo  Please make sure BOTH the script and the .tar       / Убедитесь, что И скрипт, И архив .tar
    echo  archive are placed in the game folder.              / находятся в главной папке с игрой.
    echo =================================================================================
    pause >nul
    exit /b
)

cls
echo Analyzing archive, please wait... / Анализ архива, подождите...
set /a total_files=0
for /f %%A in ('tar -tf "!ARCHIVE!" ^| find /c /v ""') do set total_files=%%A

if !total_files! EQU 0 (
    cls
    echo =================================================================================
    echo                                      ERROR / ОШИБКА
    echo =================================================================================
    echo  Archive is empty or corrupted.                      / Архив пуст или поврежден.
    echo =================================================================================
    pause >nul
    exit /b
)

cls
echo =================================================================================
echo                            ByForMOD Installer / Установщик
echo =================================================================================
echo  Game Version / Версия игры : !TARGET_VERSION!
echo  Archive / Архив            : !ARCHIVE!
echo  Total Files / Всего файлов : !total_files!
echo =================================================================================
echo.

set /a current=0
set "FULL_BAR=########################################"
set "EMPTY_BAR=----------------------------------------"

for /f "delims=" %%F in ('tar -xvf "!ARCHIVE!" -C "!DEST_DIR!" 2^>nul') do (
    set /a current+=1
    set /a percent=current*100/total_files
    
    set /a blocks=percent*40/100
    set /a spaces=40-blocks
    
    set "DRAW_FULL="
    if !blocks! GTR 0 for %%B in (!blocks!) do set "DRAW_FULL=!FULL_BAR:~0,%%B!"
    
    set "DRAW_EMPTY="
    if !spaces! GTR 0 for %%S in (!spaces!) do set "DRAW_EMPTY=!EMPTY_BAR:~0,%%S!"
    
    <nul set /p ="!CR! Progress / Прогресс: [!DRAW_FULL!!DRAW_EMPTY!] !percent!%% (!current!/!total_files!)      "
)

echo.
echo.
echo =================================================================================
echo  SUCCESS: Installation completed! / УСПЕХ: Установка успешно завершена!
echo =================================================================================
echo Press any key to exit... / Нажмите любую клавишу для выхода...
pause >nul