@echo off
::mode con:cols=90 lines=30
::-----------------------------------------------
set ver=21.7.1
set src1=http://185.17.127.71:8880/
set src1p=185.17.127.71
set src2=http://tucv0175.hostlife.link/presets/
set src2p=tucv0175.hostlife.link
set src3=http://intorq-russia.ru/tmp/
set src3p=intorq-russia.ru
set src4=http://nzxtua.ddns.net/tcf/
set src4p=nzxtua.ddns.net
::-----------------------------------------------
echo tcfpkg-bootstrap v.%ver%
echo.

goto next
:freemen
chcp 866
cls
echo.
::echo   T O T A L   C O M M A N D E R         M E S H
echo   TOTAL COMMANDER              MESH
echo.
echo   █████▒██▀███  ▓█████ ▓█████  ███▄ ▄███▓▓█████  ███▄    █
echo ▓██   ▒▓██ ▒ ██▒▓█   ▀ ▓█   ▀ ▓██▒▀█▀ ██▒▓█   ▀  ██ ▀█   █ 
echo ▒████ ░▓██ ░▄█ ▒▒███   ▒███   ▓██    ▓██░▒███   ▓██  ▀█ ██▒
echo ░▓█▒  ░▒██▀▀█▄  ▒▓█  ▄ ▒▓█  ▄ ▒██    ▒██ ▒▓█  ▄ ▓██▒  ▐▌██▒
echo ░▒█░   ░██▓ ▒██▒░▒████▒░▒████▒▒██▒   ░██▒░▒████▒▒██░   ▓██░
echo  ▒ ░   ░ ▒▓ ░▒▓░░░ ▒░ ░░░ ▒░ ░░ ▒░   ░  ░░░ ▒░ ░░ ▒░   ▒ ▒ 
echo  ░       ░▒ ░ ▒░ ░ ░  ░ ░ ░  ░░  ░      ░ ░ ░  ░░ ░░   ░ ▒░
echo  ░ ░     ░░   ░    ░      ░   ░      ░      ░      ░   ░ ░ 
echo           ░        ░  ░   ░  ░       ░      ░  ░         ░ 
if %ver% GTR 0 (
	echo v.%ver% connect server [%repo%] %source%
	) else (
	echo v.%ver%
	)
echo.
if %pos%==0 goto add2
if %pos%==1 goto err2_
if %pos%==2 goto install862
if %pos%==3 goto install642
if %pos%==10 goto err_
if %pos%==11 goto err2_
if %pos%==12 goto err3_
if %pos%==13 goto err4_
if %pos%==14 goto err5_
if %pos%==15 goto err6_
if %pos%==16 goto err7_

::-----------------------------------------------
:next
:: [SET, IF]
::SETLOCAL ENABLEDELAYEDEXPANSION
set rx=0
set "KEY=HKEY_CURRENT_USER\Control Panel\International"
for /F "usebackq tokens=3" %%a IN (`reg query "%KEY%" ^| find /i "LocaleName"`) do set Language=%%a
set "lng=%LANGUAGE:~0,2%"
if %lng%==ru set rx=1
if %lng%==ua set rx=1

if exist %windir%\Microsoft.NET\Framework\v4* (
	set net=1
	) else (
	set net=0
	)

if defined PROGRAMFILES(X86) (
	::x64=1
	set os=1
	) else (
	::x86=0
	set os=0
	)

if exist "%~dp0user\tcfpkg.exe" (
	set utpkg=1
	) else (
	set utpkg=0
	)
	
if exist "%~dp0tcfpkg.exe" (
	set tpkg=1
	) else (
	set tpkg=0
	)

if exist "%~dp0Totalcmd64.exe" (
	set total=1
	) else (
	set total=0
	)

:: count folders & files
for /F %%i in ('dir /B /AD ^| find /c /v "~~~"') do set fd=%%i
for /F %%i in ('dir /A-D /B ^| find /c /v "~~~"') do set fs=%%i

set ff=0
if %fd% GTR 5 set ff=1
if %fs% GTR 60 set ff=1
if %ff%==1 goto err5

if %tpkg%==0 if %utpkg%==0 (
	set ss=0
	goto SS
	)

:load_

if exist "%~dp0tcfpkg.exe" (
	set tpkg=1
	) else (
	set tpkg=0
	)

::goto install64
if %os%==1 if %total%==0 if %tpkg%==1 if %utpkg%==0  (
	set ss=1
	goto SS
	)

::goto install86	
if %os%==0 if %total%==0 if %tpkg%==1 if %utpkg%==0 (
	set ss=2
	goto SS
	)

if %total%==1 if %tpkg%==0 if %utpkg%==1  (
	goto add
	)
	
if %tpkg%==0 if %utpkg%==0 (
	goto err2
	)

if %total%==0 if %utpkg%==1 (
	set ss=1
	goto SS
	)
	
if %total%==0 (
	goto err3
	)

if %total%==1 if %tpkg%==1 if %utpkg%==0  (
	move "%~dp0tcfpkg.exe" "%~dp0user\tcfpkg.exe"
	goto add
	)

::-----------------------------------------------

:: [INSTALL]
:install86
set pos=2
goto freemen
:install862
color 0E
if %rx%==1 (
	echo Обнаружена X86 архитектура! Total Commander - Freemen сборка исключительно для X64 архитектуры
	echo Сборка будет успешно распакована и вы сможете перенести ее на любой X64 компьютер
	) else (
	echo X86 architecture detected! Total Commander - Freemen assembly exclusively for X64 architecture
	echo The assembly will be successfully unpacked and you can transfer it to any X64 computer
	)
pause
goto install642

:install64
set pos=3
goto freemen
:install642
color 08
taskkill /F /IM Totalcmd64.exe >nul 2>&1
timeout 2 > nul
color 0F
echo   ONLINE INSTALL
set result=0

if %rx%==1 (
	echo +---------------------------------------------------------+
	echo ^| Сборка будет скачана и распакована в текущую директорию ^|
	echo +---------------------------------------------------------+
	) else (
	echo The assembly will be downloaded and unpacked into the current directory
	)
pause
color 02
mkdir user
move "%~dp0tcfpkg.exe" "%~dp0user\tcfpkg.exe" >nul 2>&1

color 0E
"%~dp0user\tcfpkg.exe" -u %source%
timeout 4  > nul
color 02
"%~dp0user\tcfpkg.exe" -i totalcmd
timeout 1  > nul
if not exist "%~dp0Totalcmd64.exe" (
	goto err
	) else (
	color 0F
	)
set total=1	
:: %windir%\Microsoft.NET\Framework\v4*
if %rx%==1 (
	set netnet="Предупреждение! .NET4 не обнаружен, требуется для GUI Репозитория! Альтернатива: использование Репозитория через CMD"
	) else (
	set netnet="Warning! .NET4 not detected, required for Repository GUI to work! Alternative: using the Repository via CMD"
	)
if %net%==0 (
	color 0E
	echo %netnet%
	echo.
	pause
	)
goto add

::-----------------------------------------------

:: [ADD]
:add
set pos=0
goto freemen
:add2
color 0F
if not exist "%~dp0user\tcfpkg.exe" (
	set pos=1
	goto freemen
	)

set plg=набор плагинов
set soft=набор софта
set I2=Plugins Standart
set I3=Plugins Lite
set I4=Soft Standart
set I5=Soft Lite

if %rx%==1 (
	echo  1. Обновить
	) else (
	echo  1. Update
	)
echo.

echo  Установить %plg%
echo  2. %I2%
set P2=catalogmaker gif iclread iso multiarc zstd audioinfo exeformat exif mediainfowdx shelldetails xpdfsearch adb diskinternals_reader procview registry services2 sftp4tc startups uninstaller64 webdav dirsizecalccharts svgview cudalister 7zip fileinfo iclview ebookinfowlx office2007 akfont photoviewer tctorrent sumatrapdf

echo  3. %I3%
set P3=catalogmaker audioinfo exeformat exif mediainfowdx shelldetails procview registry services2 startups uninstaller64 dirsizecalccharts cudalister 7zip fileinfo office2007 photoviewer sumatrapdf
echo.

echo  Установить %soft%
echo  4. %I4%
set P4=nppmini fscapture fsviewer gdb5 mtpw12 victoria qtorrent telegram mpcbe xmplay netscan putty smoffice sumatrapdf 7zip restorator rufus tcpu ultraiso usbimagetools keepass oosu10 aida64ex autoruns cpuz gpuz procexp procmon ssdlife tfttest bcompare4 chkdskgui defraggler everything geek rdpwrap

echo  5. %I5%
set P5=nppmini fscapture fsviewer qtorrent mpcbe xmplay putty sumatrapdf 7zip tcpu ultraiso oosu10 hwi autoruns procexp everything geek
echo.

if %rx%==1 (
	echo  8. Удалить все
	) else (
	echo  8. Uninstall all
	)

if %rx%==1 (
	echo  9. Подробная информация
	) else (
	echo  9. Detailed information
	)

if %rx%==1 (
	echo  0. Выйти
	) else (
	echo  0. Exit
	)
echo.
if %rx%==1 (
	echo Если какие-то плагины или софт из наборов уже установлены, они будут пропущены
	) else (
	echo If some plugins or software from the sets are already installed, they will be skipped
	)
echo.

if %rx%==1 (
	set what=Что будем делать
	) else (
	set what=Choose the option
	)
	
set /p choice=%what%: 

if '%choice%'=='1' goto UP

if '%choice%'=='2' (
	set run=%P2%
	goto INS
	)
	
if '%choice%'=='3' (
	set run=%P3%
	goto INS
	)
	
if '%choice%'=='4' (
	set run=%P4%
	goto INS
	)
	
if '%choice%'=='5' (
	set run=%P5%
	goto INS
	)

if '%choice%'=='8' (
	goto DELALL
	)
	
if '%choice%'=='9' (
	goto INSINFO
	)

if '%choice%'=='0' goto end


if %rx%==1 (
	set nos=Нет такого варианта
	) else (
	set nos=No such option
	)
echo.
color 0E
::if '%choice%'=='' echo "%choice%" Нет такого варианта (no such option)
echo "%choice%" %nos%
timeout 2 > nul

echo.
goto add

::-----------------------------------------------
:: [INSINFO]
:INSINFO
cls
echo %plg%
echo ---------------------
echo 2. %I2%: %P2%
echo.
echo 3. %I3%: %P3%
echo.
echo %soft%
echo ---------------------
echo 4. %I4%: %P4%
echo.
echo 5. %I5%: %P5% 
echo.
pause
goto add

::-----------------------------------------------
:: [DELALL]
:DELALL

if %rx%==1 (
	echo.
	echo Вы действительно хотите удалить Total Commander - Freemen и все его компоненты?
	) else (
	echo.
	echo Are you sure you want to remove Total Commander - Freemen and all its components?
	)
echo.
pause
color 0C
if %rx%==1 (
	echo.
	echo Подтверждаете?
	pause
	) else (
	echo.
	echo Do you confirm?
	pause
	)
echo.

call "%~dp0user\tcfconfig.bat" DELALL
exit

::-----------------------------------------------
:: [SERVER SEARCH]
:SS
set repo=0

ping %src1p% -n 1 -w 1000>nul
if %errorlevel%==0 (
	set source=%src1%
	set repo=1
	goto ssend
	)

ping %src2p% -n 1 -w 1000>nul
if %errorlevel%==0 (
	set source=%src2%
	set repo=2
	goto ssend
	)
	
ping %src3p% -n 1 -w 1000>nul
if %errorlevel%==0 (
	set source=%src3%
	set repo=3
	goto ssend
	)
	
ping %src4p% -n 1 -w 1000>nul
if %errorlevel%==0 (
	set source=%src4%
	set repo=4
	goto ssend
	)
:ssend

if %repo%==0 goto err6

if %ss%==0 goto LOAD
if %ss%==1 goto install64
if %ss%==2 goto install86
if %ss%==3 goto ins_
if %ss%==4 goto up_
exit

::-----------------------------------------------
:: [LOADING]
:LOAD
if not '%fs%'=='1' goto err5
color 02
echo loading...
powershell -c "(new-object System.Net.WebClient).DownloadFile('%source%/000000000000905652d4011626f8518578846346','tcfpkg.exe')"
if errorlevel 1 goto err4
goto load_

::-----------------------------------------------
:: [UPDATE]
:UP
set ss=4
goto SS
:up_
cls
color 0E
echo [UPDATE]
"%~dp0user\tcfpkg.exe" -u %source%
timeout 4 > nul
color 02
"%~dp0user\tcfpkg.exe" -i totalcmd
color 0A
echo UPDATE [OK]
echo.
timeout 2 > nul
chcp 866
goto add

::-----------------------------------------------
:: [PLUGINS, SOFT]
:INS
set ss=3
goto SS
:ins_
cls
color 0E
echo [PACKAGES]
"%~dp0user\tcfpkg.exe" -u %source%
timeout 4 > nul
color 02
"%~dp0user\tcfpkg.exe" -i %run%
color 0A
echo PACKAGES [OK]
echo.
timeout 2 > nul
chcp 866
goto add

::-----------------------------------------------
:: [ERRORS]

:err
set pos=10
goto freemen
:err_
color 0C
if %rx%==1 (
	echo Ошибка! Что-то пошло не так, возможно проблемы с интернетом или Totalcmd64.exe не обнаружен...
	) else (
	echo Error! Something went wrong, maybe a problem with the internet or Totalcmd64.exe not found...
	)
echo.
pause
exit

:err2
set pos=11
goto freemen
:err2_
color 0C
if %rx%==1 (
	echo Ошибка! tcfpkg.exe не обнаружен!
	) else
	echo Error! tcfpkg.exe not found!
	)
echo.
pause
exit

:err3
set pos=12
goto freemen
:err3_
color 0C
if %rx%==1 (
	echo Ошибка! Что-то пошло не так Totalcmd64.exe не обнаружен... 
	) else (
	echo Error! Something went wrong Totalcmd64.exe not found...
	)
echo.
pause
exit

:err4
set pos=13
goto freemen
:err4_
color 0C
if %rx%==1 (
	echo Ошибка! не возможно загрузить tcfpkg.exe, возможно проблемы с интернетом
	) else (
	echo Error! it is not possible to load tcfpkg.exe, maybe internet problems
	)
echo.
pause
exit

:err5
set pos=14
goto freemen
:err5_
color 0C
if %rx%==1 (
	echo Внимание! 
	echo totalcmd-bootstrap.bat должен находится в пустой папке, Total Commander будет 
	echo установлен рядом с totalcmd-bootstrap.bat в эту же папку. Проверьте правильность запуска!
	) else (
	echo Attention! 
	echo totalcmd-bootstrap.bat should be in an empty folder, Total Commander will
	echo is installed next to totalcmd-bootstrap.bat in the same folder. Check if it starts up correctly!
	)
echo.
pause
exit

:err6
set pos=15
goto freemen
:err6_
color 0C
if %rx%==1 (
	echo Ошибка! не найден сервер Репозитория! Проверьте подключение с интернетом, либо обновите totalcmd-bootstrap.bat
	) else (
	echo Error! no Repository server found! Check your internet connection or update totalcmd-bootstrap.bat
	)
echo.
pause
exit

::-----------------------------------------------

:end
tasklist | find "Totalcmd64.exe"
set tgi=%errorlevel%

if %total%==1 if not %tgi%==0 start "" "Totalcmd64.exe"

exit