@Echo Off
Set WD=%~1
Set STR=+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Set STR2================================================================================
Set BIN=BIN
Echo.
Echo    %~1

::   
If Exist "%WD%\Backup" RD /S /Q "%WD%\Backup"
MD "%WD%\Backup"
If Exist "%WD%\Logs" RD /S /Q "%WD%\Logs"
MD "%WD%\Logs"
If Exist "%WD%\Out" RD /S /Q "%WD%\Out"
MD "%WD%\Out"
If Exist "%WD%\Temp" RD /S /Q "%WD%\Temp"
MD "%WD%\Temp"
If Exist Temp\Log\Before (
   Set After=Temp\Log\After
   Set Before=Temp\Log\Before
) Else (
   Set After=Z-Del_Files\After
   Set Before=Z-Del_Files\Before
)
If Not Exist %After% MD %After%
If Not Exist %After%\..\Addons MD %After%\..\Addons

::   Files_UTF  Files_ANSI   
For /F "UseBackQ Delims=" %%I In (`Dir "%WD%\_Settings\Files_*" /B /ON 2^>nul`) Do Call :CLeanFiles %%~I
If Exist "%WD%\_Settings\Files_UTF" (
   If Exist "%WD%\_Settings\Files_ANSI" (
     FindStr /L /I /V /G:"%WD%\_Settings\Files_UTF" "%WD%\_Settings\Files_ANSI" | Sort>"%WD%\File1"
     Move "%WD%\File1" "%WD%\_Settings\Files_ANSI">nul
   )
)

::       Backup  Files_UTF  Files_ANSI
If Exist "%WD%\_Settings\Files_UTF" For /F "Delims=" %%I In (%WD%\_Settings\Files_UTF) Do Call :CopyFiles %%~I
If Exist "%WD%\_Settings\Files_ANSI" For /F "Delims=" %%I In (%WD%\_Settings\Files_ANSI) Do Call :CopyFiles %%~I

::    
If Exist "%WD%\PackFiles" Del "%WD%\PackFiles">nul
For /F "UseBackQ Delims=" %%I In (`Dir "%WD%\Backup\*.??_" /B /ON 2^>nul`) Do Echo %%~I>>"%WD%\PackFiles"

::      
If Exist "%WD%\PackFiles" (
   Echo ᯠ 㯠 䠩  㤠 ਣ
   For /F "Delims=" %%I In (%WD%\PackFiles) Do (
     Expand -r "%WD%\Backup\%%~I" "%WD%\Backup\\">nul
     Del "%WD%\Backup\%%~I">nul
   )
)

::    Files_UTF
Set CP=utf-8
If Exist "%WD%\_Settings\Files_UTF" (
   For /F "Delims=" %%I In (%WD%\_Settings\Files_UTF) Do (
     Echo ࠡ⪠ 䠩 %%I
     If Exist "%WD%\Backup\%%~I" (
       %BIN%\Iconv.exe -c -f utf-16LE -t %CP% "%WD%\Backup\%%~I">"%WD%\Temp\%%~I"
       Call :ModifyFiles %%~I
       %BIN%\Iconv.exe -c -f %CP% -t utf-16LE "%WD%\Temp\%%~I2">"%WD%\Out\%%~I"
     ) Else Call :NotFiles %%~I
   )
)

::    Files_ANSI
Set CP=cp866
If Exist "%WD%\_Settings\Files_ANSI" (
   For /F "Delims=" %%I In (%WD%\_Settings\Files_ANSI) Do (
     Echo ࠡ⪠ 䠩 %%I
     If Exist "%WD%\Backup\%%~I" (
       Copy /Y "%WD%\Backup\%%~I" "%WD%\Temp\">nul
       Call :ModifyFiles %%~I
       Copy /Y "%WD%\Temp\%%~I2" "%WD%\Out\%%~I">nul
     ) Else Call :NotFiles %%~I
   )
)

::      
If Exist "%WD%\PackFiles" (
   Echo  䠩
   For /F "UseBackQ Delims=" %%I In (`Dir "%WD%\Out" /B /ON 2^>nul`) Do Call :PackFiles %%~I
)

::   
Echo ஢ 䠩  㤠 ६ 
XCopy "%WD%\Out\*.*" "%I386%\" /Q /R /Y>nul
XCopy "%WD%\Out\*.*" %After%\ /Q /R /Y>nul

::  
Call :CopyLogs %~1

::      
If Exist "%WD%\Backup" RD /S /Q "%WD%\Backup"
If Exist "%WD%\Out" RD /S /Q "%WD%\Out"
If Exist "%WD%\Temp" RD /S /Q "%WD%\Temp"
If Exist "%WD%\PackFiles" Del "%WD%\PackFiles">nul

::  
If Exist "%WD%\_Settings\FClean.cmd" (
   Echo 譠 ⪠
   Call "%WD%\_Settings\FClean.cmd"
)

Echo 믮   %~1 襭
GoTo :EOF


:CleanFiles
::  
Set I=%~1
FindStr /L /I /V /G:"%WD%\_Settings\BlockFiles" "%WD%\_Settings\%I%" | Sort>"%WD%\File1"
If Exist "%WD%\DelOC.lst" (
   FindStr /I /V /G:"%WD%\DelOC.lst" "%WD%\File1" | Sort>"%WD%\_Settings\%I%"
   Del /F /Q "%WD%\File1"
) Else Move "%WD%\File1" "%WD%\_Settings\%I%">nul
Goto :EOF

:CopyFiles
::  
Set I=%~1
If Exist "%I386%\%I:~0,-1%?" Copy "%I386%\%I:~0,-1%?" "%WD%\Backup\">nul
Goto :EOF

:ModifyFiles
::     
Set I=%~1

::     NAME.INF    NAME.INF.CLN
If Exist "%WD%\_Settings\%I%.CLN" (
   Echo ,     "%I%.CLN":>>"%WD%\Logs\DelStrings_%I%.log"
   For /F "Eol=; Tokens=1" %%K In (%WD%\_Settings\%I%.CLN) Do (
     Findstr /B /E /L /I /X /C:[%%K] "%WD%\Temp\%I%">nul
     If Not Errorlevel 1 (
       %BIN%\IniClean.exe "%WD%\Temp\%I%" %%K
       Echo [%%K]>>"%WD%\Logs\DelStrings_%I%.log"
     ) Else (
       Echo  [%%K],     %I%.CLN,     %I%!>>"%WD%\Logs\!Errors.log"
       Echo       ,        .>>"%WD%\Logs\!Errors.log"
       Echo     Del_Strings_in_All.log.>>"%WD%\Logs\!Errors.log"
     )
   )
   Echo.>>"%WD%\Logs\DelStrings_%I%.log"
)

::       NAME.INF    NAME.INF.DEL
If Exist "%WD%\_Settings\%I%.DEL" (
   Echo ,     "%I%.DEL":>>"%WD%\Logs\DelStrings_%I%.log"
   FindStr /B /E /L /I /N /X /G:"%WD%\_Settings\%I%.DEL" "%WD%\Temp\%I%" | %BIN%\Iconv.exe -c -f %CP% -t cp1251>>"%WD%\Logs\DelStrings_%I%.log"
   Findstr /B /E /L /I /V /G:"%WD%\_Settings\%I%.DEL" "%WD%\Temp\%I%">"%WD%\Temp\%I%2"
   Echo.>>"%WD%\Logs\DelStrings_%I%.log"
)

::        *.INF    DelStrings
If Exist "%WD%\_Settings\DelStrings" (
   Echo ,     "DelStrings":>>"%WD%\Logs\DelStrings_%I%.log"
   If Exist "%WD%\Temp\%I%2" Move "%WD%\Temp\%I%2" "%WD%\Temp\%I%">nul
   If "%I%"=="RUSUPPCK.INF" (
     FindStr /L /I /N /G:"%WD%\_Settings\DelStrings" "%WD%\Temp\%I%">>"%WD%\Logs\DelStrings_%I%.log") Else (
     FindStr /L /I /N /G:"%WD%\_Settings\DelStrings" "%WD%\Temp\%I%" | %BIN%\Iconv.exe -c -f %CP% -t cp1251>>"%WD%\Logs\DelStrings_%I%.log"
   )
   FindStr /L /I /V /G:"%WD%\_Settings\DelStrings" "%WD%\Temp\%I%">"%WD%\Temp\%I%2"
   Echo.>>"%WD%\Logs\DelStrings_%I%.log"
)

::   
Echo %STR%>>"%WD%\Logs\DelStrings__in_all_files.log"
Echo %I% -       :>>"%WD%\Logs\DelStrings__in_all_files.log"
Echo %STR%>>"%WD%\Logs\DelStrings__in_all_files.log"
Type "%WD%\Logs\DelStrings_%I%.log">>"%WD%\Logs\DelStrings__in_all_files.log"

::      NAME.INF    NAME.INF.MOD
If Exist "%WD%\_Settings\%I%.MOD" (
   Echo Before:>>"%WD%\Logs\ModStrings_%I%.log"
   For /F "Delims=^> Tokens=1,2" %%K In (%WD%\_Settings\%I%.MOD) Do (
     Set STRIN=%%K
     Set STROUT=%%L
     If Exist "%WD%\Temp\%I%2" Move "%WD%\Temp\%I%2" "%WD%\Temp\%I%">nul
     FindStr /L /I /N /C:"%%K" "%WD%\Temp\%I%">"%WD%\Logs\ModStrings_%I%1"
     Type "%WD%\Logs\ModStrings_%I%1">>"%WD%\Logs\ModStrings_%I%.log"
     Call :StringChange
   )
   Echo After:>>"%WD%\Logs\ModStrings_%I%.log"
   Type "%WD%\Logs\ModStrings_%I%3">>"%WD%\Logs\ModStrings_%I%.log"
   Echo %STR%>>"%WD%\Logs\ModStrings__in_all_files.log"
   Echo %I% -     :>>"%WD%\Logs\ModStrings__in_all_files.log"
   Echo %STR%>>"%WD%\Logs\ModStrings__in_all_files.log"
   Type "%WD%\Logs\ModStrings_%I%.log">>"%WD%\Logs\ModStrings__in_all_files.log"
   If Exist "%WD%\Logs\ModStrings_%I%?" Del /F /Q "%WD%\Logs\ModStrings_%I%?"
)

::     NAME.INF    NAME.INF.ADD
If Exist "%WD%\_Settings\%I%.ADD" (
   Echo.>>"%WD%\Temp\%I%2"
   %BIN%\Iconv.exe -c -f cp1251 -t %CP% "%WD%\_Settings\%I%.ADD">>"%WD%\Temp\%I%2"
   Type "%WD%\_Settings\%I%.ADD">>"%WD%\Logs\AddStrings_%I%.log"
   Echo %STR%>>"%WD%\Logs\AddStrings__in_all_files.log"
   Echo %I% -     :>>"%WD%\Logs\AddStrings__in_all_files.log"
   Echo %STR%>>"%WD%\Logs\AddStrings__in_all_files.log"
   Type "%WD%\_Settings\%I%.ADD">>"%WD%\Logs\AddStrings__in_all_files.log"
   Echo.>>"%WD%\Logs\AddStrings__in_all_files.log"
)
Goto :EOF

:NotFiles
::  -   
Set I=%~1
If Not Exist "%Before%\%I:~0,-1%?" (
   Echo 訡: 䠩  %I%    	   ਡ⨢! ஢ ਡ⨢!
   Echo :   %I%    	   !  !>>"%WD%\Logs\!Errors.log"
)
If Not Exist "%WD%\Temp\%I%" (
   Echo   %I%    	   ࠡ祩 !
   Echo   %I%    	    !>>"%WD%\Logs\!Errors.log"
   If Exist "%Before%\%I:~0,-1%?" (
     Echo ᫨ 䠩  㤠   ⥣樨,  ⢨   訡
     Echo ஢ ᢮  㤠 Del_Files_in_All.log
     Echo       ,     .>>"%WD%\Logs\!Errors.log"
     Echo     Del_Files_in_All.log.>>"%WD%\Logs\!Errors.log"
   )
)
Echo %STR%>>"%WD%\Logs\!Errors.log"
Goto :EOF

:PackFiles
::  
Set I=%~1
For /F "Delims=" %%J In (%WD%\PackFiles) Do If /I %%~J==%I:~0,-1%_ (
   MakeCab /D CompressionMemory=21 /D CompressionType=LZX /L "%WD%\Out" "%WD%\Out\%I%" %I:~0,-1%_>nul
   Del /F /Q "%WD%\Out\%I%">nul
)
Goto :EOF

:StringChange
::   
%BIN%\SrchRep.exe "%WD%\Temp\%I%" "%WD%\Temp\%I%2" "%STRIN%" "%STROUT%">nul
%BIN%\SrchRep.exe "%WD%\Logs\ModStrings_%I%1" "%WD%\Logs\ModStrings_%I%2" "%STRIN%" "%STROUT%">nul
Type "%WD%\Logs\ModStrings_%I%2">>"%WD%\Logs\ModStrings_%I%3"
Goto :EOF

:CopyLogs
::  
If Exist "%WD%\Logs\AddStrings__in_all_files.log" (
   Copy /Y "%WD%\Logs\AddStrings__in_all_files.log" %After%\..\%~1_Add_Strings.log>nul
   Echo %STR2%>>%After%\..\Addons\Add_Strings_in_All.log
   Echo       %~1:>>%After%\..\Addons\Add_Strings_in_All.log
   Type "%WD%\Logs\AddStrings__in_all_files.log">>%After%\..\Addons\Add_Strings_in_All.log
   Echo.>>%After%\..\Addons\Add_Strings_in_All.log
)
If Exist "%WD%\Logs\DelStrings__in_all_files.log" (
   Copy /Y "%WD%\Logs\DelStrings__in_all_files.log" %After%\..\%~1_Del_Strings.log>nul
   Echo %STR2%>>%After%\..\Addons\Del_Strings_in_All.log
   Echo       %~1:>>%After%\..\Addons\Del_Strings_in_All.log
   Type "%WD%\Logs\DelStrings__in_all_files.log">>%After%\..\Addons\Del_Strings_in_All.log
   Echo.>>%After%\..\Addons\Del_Strings_in_All.log
)
If Exist "%WD%\Logs\ModStrings__in_all_files.log" (
   Copy /Y "%WD%\Logs\ModStrings__in_all_files.log" %After%\..\%~1_Mod_Strings.log>nul
   Echo %STR2%>>%After%\..\Addons\Mod_Strings_in_All.log
   Echo       %~1:>>%After%\..\Addons\Mod_Strings_in_All.log
   Type "%WD%\Logs\ModStrings__in_all_files.log">>%After%\..\Addons\Mod_Strings_in_All.log
   Echo.>>%After%\..\Addons\Mod_Strings_in_All.log
)
If Exist "%WD%\Logs\!Errors.log" (
   Copy /Y "%WD%\Logs\!Errors.log" %After%\..\%~1_ERRORS.log>nul
   Echo %STR2%>>%After%\..\Addons\!ERRORS_in_All.log
   Echo     %~1:>>%After%\..\Addons\!ERRORS_in_All.log
   Echo %STR%>>%After%\..\Addons\!ERRORS_in_All.log
   Type "%WD%\Logs\!Errors.log">>%After%\..\Addons\!ERRORS_in_All.log
   Echo.>>%After%\..\Addons\!ERRORS_in_All.log
)
If Exist "%WD%\DelDRV.lst" (
   Echo %STR2%>>%After%\..\Addons\Del_Files_in_All.log
   Echo       %~1:>>%After%\..\Addons\Del_Files_in_All.log
   Echo %STR%>>%After%\..\Addons\Del_Files_in_All.log
   For /F "Eol=; Tokens=1" %%K In (%WD%\DelDRV.lst) Do (
     Echo %%K>>%After%\..\Addons\Del_Files_in_All.log
   )
   Echo.>>%After%\..\Addons\Del_Files_in_All.log
)
If Exist "%WD%\DelOC.lst" (
   Echo %STR2%>>%After%\..\Addons\Del_Files_in_All.log
   Echo       %~1:>>%After%\..\Addons\Del_Files_in_All.log
   Echo %STR%>>%After%\..\Addons\Del_Files_in_All.log
   For /F "Eol=; Tokens=1" %%K In (%WD%\DelOC.lst) Do (
     Echo %%K>>%After%\..\Addons\Del_Files_in_All.log
   )
   Echo.>>%After%\..\Addons\Del_Files_in_All.log
)
Goto :EOF
