@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Call :SetChcpDos

If "%UsePauseOnExit%"=="" Set UsePauseOnExit=1
Set NormalEnd=

CD /D "%~dp0"

SetLocal EnableDelayedExpansion
Title Набор обновлений для русской Windows XP Professional SP3

Support /start
If Exist Temp RD /S /Q Temp
MD Temp
MD Temp\CABs
MD Temp\1

If "%~1"=="" (
  Set Configure=

  Call :MakeAddonsList

) Else If /I "%~1"=="Configure" (
  Set Configure=%~2

  If "!Configure!"=="" (
    Call :Quit "Ошибка: Необходимо ввести название конфигурации."
  )

  Call :MakeAddonsList

  If Not "!Configure!"=="" (
    If Not Exist Configs MD Configs
    If Not Exist "Configs\!Configure!" (
      Type Temp\Folders.txt>"Configs\!Configure!"
      Call :Quit "Создан файл конфигурации: !Configure!"
    )

    Echo Сборка конфигурации !Configure!

    Rem Проверить наличие входящих в конфигурацию аддонов
    Set AddonsNoFound=
    For /F "UseBackQ Delims=" %%I In ("Configs\!Configure!") Do (
      Set AddonFound=
      For /F "UseBackQ Delims=" %%J In (`Find /I "%%I" Temp\Folders.txt`) Do (
        If /I "%%I"=="%%J" Set AddonFound=%%I
      )
      If "!AddonFound!"=="" (
        Echo Ошибка: дополнение %%I не найдено.
        Set AddonsNoFound=1
      )
    )
    If "!AddonsNoFound!"=="1" Call :Quit

    Rem Собираем конфигурацию
    Type "Configs\!Configure!">Temp\Folders.txt
    Set AddonFound=
  )
) Else (
  Set SeparateAddons=1

:NextParam
  If Not "%~1"=="" (
    If Exist "Addons\%~1\*.*" (
      Echo Addons\%~1>>Temp\Folders.txt
    ) Else (
      Call :Quit "Ошибка: дополнение %~1 не найдено."
    )
    Shift
    GoTo :NextParam
  )
)

Set GUI=NO
Set PACKROOT=.

Call :GetDistrDir
Call :CheckDistr
Call :CheckIntBefore

Set ErrFlag=
Call :CheckRequired
Call :CheckConflict
If "!ErrFlag!"=="1" Call :Quit

Call :CheckFilesDir
Call :SP3BugFix
Call :CheckUpdate
Call :ProcessStartEvent

Echo Сжатие файлов.
Copy Compress Temp\Compress.cmd >nul

Start /Wait Temp\Compress.cmd "%I386%"
If Not "!ErrorLevel!"=="0" Exit

Call :ProcessSpecialCase
Call :CreateLists
Call :CheckDelOC

Set RepackReq=
Call :CheckRepackReq

If "!RepackReq!"=="1" (
  Echo Распаковка CAB-файлов.
  For %%I In ("%I386%\*.CAB") Do Call :UnpackCab "%%~I"

  Call :CheckDelDrv
  For %%I In ("%I386%\*.CAB") Do Call :CheckCab %%~nI
)

Call :CopyFiles
Call :CheckKillWFP

Call :ModifySvcpackInf
Call :ModifySysocInf
Call :ModifyDosnetInf
Call :ModifyTxtsetupSif
Call :CheckHive

Call :CopyIntBefore
Call :CheckNfo
Call :ProcessFinishEvent
Call :CleanEmptyDirs
Support /finish

Set NormalEnd=1
Call :Quit "Скрипт завершил свою работу."
GoTo :EOF

:Quit

  If Exist Temp RD /S /Q Temp
  If Exist sfcfiles.dll.bak Del /F /Q sfcfiles.dll.bak

:Halt
  If Not "%~1"=="" Echo %~1

  Echo.
  If "!NormalEnd!"=="" (
    Pause
    Exit
  )
  If "%UsePauseOnExit%"=="1" Pause
GoTo :EOF

:MakeAddonsList
  Set SeparateAddons=

  Echo .>Temp\Folders.txt
  If Exist Addons\*.* (
    For /F "UseBackQ Delims=" %%I In (`Dir Addons /AD /B /ON 2^>nul`) Do (
      Echo Addons\%%~I>>Temp\Folders.txt
    )
  )
GoTo :EOF

:GetDistrDir
  Set SRCDISTR=
  For /F "Delims=" %%I In (Distr.ini) Do Set SRCDISTR=%%~I
  If Not "!SRCDISTR!"=="" (
    If "!SRCDISTR:~-1!"=="\" Set SRCDISTR=!SRCDISTR:~0,-1!
  )
  If "!SRCDISTR!"=="" Call :Quit "Ошибка: не задан путь к дистрибутиву в файле Distr.ini."
  If Not Exist "!SRCDISTR!\*.*" Call :Quit "Ошибка: папка с дистрибутивом не существует."

  Set OutDir=
  If Exist Out.ini For /F "Delims=" %%I In (Out.ini) Do Set OutDir=%%~I

  If Not "!OutDir!"=="" (
    If "!OutDir:~-1!"=="\" Set OutDir=!OutDir:~0,-1!
  )
  If "!OutDir!"=="" (
    Set Distr=!SRCDISTR!
  ) Else (
    Set Distr=!OutDir!
  )
  Set I386=%Distr%\I386
  Call :CheckSystemRoot

  Set CopyReq=
  If Not "%Distr%"=="!SRCDISTR!" (
    If "!SeparateAddons!"=="" (
      Set CopyReq=1
    ) Else (
      If Not Exist "%Distr%" Set CopyReq=1
    )
  )
  If "!CopyReq!"=="1" (
    Echo Копирование дистрибутива.

    If Exist "%Distr%" RD /S /Q "%Distr%"
    MD "%Distr%"
    Start "Копирование дистрибутива" /Wait XCopy "!SRCDISTR!\*.*" "%Distr%\" /E /H /R /Y
  )

  If Not Exist "%I386%\*.*" Call :Quit "Ошибка: неправильная папка с дистрибутивом."
GoTo :EOF

:CheckDistr
  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    If Exist "%%I\ChkDistr" (
      If Exist Temp\ChkDistr.cmd Del /F /Q Temp\ChkDistr.cmd
      Copy "%%I\ChkDistr" Temp\ChkDistr.cmd >nul

      Set Err=
      Call Temp\ChkDistr.cmd "%%I"
      If Not "!Err!"=="" Call :Quit "!Err!"
    )
  )
GoTo :EOF

:CheckRepackReq
  If Exist Temp\DelDrv.lst (
    Set RepackReq=1

  ) Else (
    For /F "Delims=" %%I In (Temp\Folders.txt) Do (
      Dir "%%I\Files\*.*" /A-D /B 2>nul >nul
      If "!ErrorLevel!"=="0" Set RepackReq=1
    )
  )
GoTo :EOF

:CreateLists
  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    If Exist "%%I\KillWFP.lst" (
      Type "%%I\KillWFP.lst" >>Temp\KillWFP.lst
      Echo.>>Temp\KillWFP.lst
    )
    If Exist "%%I\DelDrv.lst" (
      Type "%%I\DelDrv.lst" >>Temp\DelDrv.lst
      Echo.>>Temp\DelDrv.lst
    )
    If Exist "%%I\DelOC.lst" (
      Type "%%I\DelOC.lst" >>Temp\DelOC.lst
      Echo.>>Temp\DelOC.lst
    )
  )
  If Exist Temp\DelDrv.lst (
    Type Temp\DelDrv.lst >>Temp\KillWFP.lst
  )
  If Exist Temp\DelOC.lst (
    Type Temp\DelOC.lst >>Temp\KillWFP.lst
  )
GoTo :EOF

:CheckKillWFP
  If Exist Temp\KillWFP.lst (
    Echo Патч для Sfcfiles.dll.

    Expand -r "%I386%\SFCFILES.DL_" Temp\ >nul
    SFCPatch Temp\sfcfiles.dll Temp\KillWFP.lst >nul
    Del /F /Q "%I386%\SFCFILES.DL_"
    MakeCab /D CompressionMemory=21 /D CompressionType=LZX /L "%I386%" Temp\sfcfiles.dll SFCFILES.DL_ >nul
  )
GoTo :EOF

:ModifySvcpackInf
  If Exist "%I386%\SVCPACK.IN_" (
    Expand -r "%I386%\SVCPACK.IN_" Temp\1\ >nul
  ) Else (
    If Exist "%I386%\svcpack.inf" Copy "%I386%\svcpack.inf" Temp\1\ >nul
  )
  If Exist Temp\1\svcpack.inf (
    Call :SetChcpWin
    Type Temp\1\svcpack.inf>Temp\svc.inf
    Call :SetChcpDos
  )
  Call :CreateSvcpackInf
GoTo :EOF

:CreateSvcpackInf
  Set SvcPrev=Temp\svc.inf
  Set SvcModified=

  If Exist !SvcPrev! (
    Echo Изменение Svcpack.inf.
  ) Else (
    Echo Создание Svcpack.inf.
  )
  Set SVC=Temp\svcpack.inf

  Copy SvcTmpl !SVC! >nul
  Echo.>>!SVC!
  Echo [ProductCatalogsToInstall]>>!SVC!

  If Exist "%I386%\SVCPACK\*.*" (
    Call :CheckDelCAT

    For /F "Delims=" %%I In (Temp\Folders.txt) Do (
      If Exist "%%I\Files\ForceCopy\SVCPACK\*.CAT" Set SvcModified=1
      If Exist "%%I\Files\ForceCopy\SVCPACK\*.CA_" Set SvcModified=1
    )
    For /F "UseBackQ Delims=" %%K In (`Dir "%I386%\SVCPACK\*.CA_" "%I386%\SVCPACK\*.CAT" /A-D /B /ON 2^>nul`) Do (
      Echo %%~nK.CAT>>!SVC!
    )
  )
  Echo.>>!SVC!
  Echo [SetupHotfixesToRun]>>!SVC!
  If Exist !SvcPrev! GETSect !SvcPrev! SetupHotfixesToRun >>!SVC!

  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    If Exist "%%I\Run" (
      Type "%%I\Run" >>!SVC!
      Set SvcModified=1
    )
  )
  If "!SvcModified!"=="1" (
    If Exist "%I386%\SVCPACK.INF" Del /F /Q "%I386%\SVCPACK.INF"
    If Exist "%I386%\SVCPACK.IN_" Del /F /Q "%I386%\SVCPACK.IN_"

    MakeCab /L "%I386%" !SVC! SVCPACK.IN_ >nul
  )
GoTo :EOF

:CheckDelCAT
  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    If Exist "%%I\DelCAT" (
      For /F "UseBackQ Delims=" %%K In ("%%I\DelCAT") Do (
        If Exist "%I386%\SVCPACK\%%~K.CAT" (
          Del /F /Q "%I386%\SVCPACK\%%~K.CAT"
          Set SvcModified=1
        )
        If Exist "%I386%\SVCPACK\%%~K.CA_" (
          Del /F /Q "%I386%\SVCPACK\%%~K.CA_"
          Set SvcModified=1
        )
      )
    )
  )
GoTo :EOF

:ModifySysocInf
  Echo Добавление строк в Sysoc.inf.

  Expand -r "%I386%\SYSOC.IN_" Temp\ >nul
  Call :SetChcpWin
  Type Temp\sysoc.inf>Temp\sysoc0.inf
  Call :SetChcpDos
  Del /F /Q Temp\sysoc.inf
  Ren Temp\sysoc0.inf sysoc.inf

  Set First=
  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    If Exist "%%I\Sysoc" (
      If "!First!"=="" (
        Set First=1

        Echo.>>Temp\sysoc.inf
        Echo [Components]>>Temp\sysoc.inf
      )
      Type "%%I\Sysoc" >>Temp\sysoc.inf
    )
  )
  If "!First!"=="1" (
    Del /F /Q "%I386%\SYSOC.IN_"
    MakeCab /L "%I386%" Temp\sysoc.inf SYSOC.IN_ >nul
  )
GoTo :EOF

:ModifyDosnetInf
  Echo Добавление строк в Dosnet.inf.
  Attrib -R "%I386%\DOSNET.INF"

  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    If Exist "%%I\Dosnet" (
      Echo.>>"%I386%\DOSNET.INF"
      Type "%%I\Dosnet" >>"%I386%\DOSNET.INF"
    )
  )
  If Exist "%I386%\SVCPACK\*.*" Call :AddDosnetOptionalSvcpack
GoTo :EOF

:AddDosnetOptionalSvcpack
  GETSect "%I386%\DOSNET.INF" OptionalSrcDirs | FindStr /L /I /X /C:"SVCPACK" >nul

  If Not "!ErrorLevel!"=="0" (
    Echo.>>"%I386%\DOSNET.INF"
    Echo [OptionalSrcDirs]>>"%I386%\DOSNET.INF"
    Echo SVCPACK>>"%I386%\DOSNET.INF"
  )
GoTo :EOF

:ModifyTxtsetupSif
  Echo Добавление строк в Txtsetup.sif.
  Attrib -R "%I386%\TXTSETUP.SIF"

  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    If Exist "%%I\Txtsetup" (
      Echo.>>"%I386%\TXTSETUP.SIF"
      Type "%%I\Txtsetup" >>"%I386%\TXTSETUP.SIF"
    )
  )
GoTo :EOF

:CheckHive
  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    If Exist "%%I\Hive" (
      Type "%%I\Hive" >>Temp\Hive
      Echo.>>Temp\Hive
    )
  )
  If Not Exist Temp\Hive GoTo :EOF
  Echo Создание Hivepck.inf.

  Echo [Version]>Temp\HIVEPCK.INF
  Echo Signature = "$Windows NT$">>Temp\HIVEPCK.INF
  Echo.>>Temp\HIVEPCK.INF

  Echo [AddReg]>>Temp\HIVEPCK.INF
  Set HiveExist=
  If Exist "%I386%\HIVEPCK.INF" (
    Set HiveExist=1
    If "!SeparateAddons!"=="1" GETSect "%I386%\HIVEPCK.INF" AddReg >>Temp\HIVEPCK.INF
  )
  Type Temp\Hive >>Temp\HIVEPCK.INF
  XCopy Temp\HIVEPCK.INF "%I386%\" /Q /R /Y >nul
  If "!HiveExist!"=="1" GoTo :EOF

  Echo.>>"%I386%\DOSNET.INF"
  Echo [Files]>>"%I386%\DOSNET.INF"
  Echo d1,hivepck.inf>>"%I386%\DOSNET.INF"

  Echo.>>"%I386%\TXTSETUP.SIF"
  Echo [SourceDisksFiles]>>"%I386%\TXTSETUP.SIF"
  Echo hivepck.inf  = 100,,,,,,_x,,3,3 >>"%I386%\TXTSETUP.SIF"
  Echo.>>"%I386%\TXTSETUP.SIF"
  Echo [HiveInfs.Fresh]>>"%I386%\TXTSETUP.SIF"
  Echo AddReg = hivepck.inf,AddReg>>"%I386%\TXTSETUP.SIF"
GoTo :EOF

:UnpackCab
  MD Temp\CABs\%~n1
  Extrac32 /L "Temp\CABs\%~n1" /E %1 >nul

  If Not "!ErrorLevel!"=="0" Call :Halt "Ошибка при распаковке %~nx1."
GoTo :EOF

:CheckCab
  Set RepackFlag=
  If Exist Temp\%1.flg Set RepackFlag=1
  If /I "%1"=="DRIVER" Call :CheckDrvIndex

  For /F "Delims=" %%J In (Temp\Folders.txt) Do (
    For %%K In ("%%J\Files\*.*") Do (
      If Exist "Temp\CABs\%1\%%~nxK" (
        Set RepackFlag=1
        XCopy "%%~K" Temp\CABs\%1\ /Q /R /Y >nul
      )
    )
  )
  If "!RepackFlag!"=="1" Call :RepackCab %1
GoTo :EOF

:CheckDrvIndex
  Dir /B Temp\CABs\DRIVER\*.* >Temp\D_all.lst
  GETSect "%I386%\DRVINDEX.INF" Driver >Temp\D_idx.lst

  For /F "UseBackQ Delims=" %%K In (`FindStr /L /I /X /V /G:Temp\D_idx.lst Temp\D_all.lst`) Do (
    Set RepackFlag=1
    Del /F /Q "Temp\CABs\DRIVER\%%~K"
  )
GoTo :EOF

:RepackCab
  Echo Обновление %1.CAB. Подождите, пожалуйста.

  Attrib -R "%I386%\%1.CAB"
  Set DDF=Temp\CABs\%1.DDF

  Echo .OPTION EXPLICIT >!DDF!
  Echo .Set InfFileName=NUL >>!DDF!
  Echo .Set RptFileName="Temp\CABs\%1.RPT" >>!DDF!
  Echo .Set DestinationDir= >>!DDF!
  Echo .Set CabinetName1="%1.CAB" >>!DDF!
  Echo .Set CabinetNameTemplate="%1*.CAB" >>!DDF!
  Echo .Set DiskDirectoryTemplate="%I386%\" >>!DDF!
  Echo .Set UniqueFiles=Off >>!DDF!
  Echo .Set FolderSizeThreshold=5000000 >>!DDF!
  Echo .Set MaxDiskSize=0 >>!DDF!
  Echo .Set Cabinet=On >>!DDF!
  Echo .Set Compress=On >>!DDF!
  Echo .Set CompressionType=LZX >>!DDF!
  Echo .Set CompressionMemory=21 >>!DDF!

  If /I "%1"=="DRIVER" (
    Set CABLst="Temp\D_idx.lst"
  ) Else (
    Set CABLst=`Dir /B /O-E Temp\CABs\%1\*.* 2^>nul`
  )
  For /F "UseBackQ Delims=" %%K In (!CABLst!) Do Echo "Temp\CABs\%1\%%K" "%%K" >>!DDF!
  Start "Перепаковка %1.CAB" /Wait MAKECAB.EXE /V1 /F !DDF!

  If Not "!ErrorLevel!"=="0" Call :Halt "Ошибка при обновлении %1.CAB."
GoTo :EOF

:CopyFiles
  Echo Копирование новых файлов.

  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    XCopy "%%I\Files\*.*" "%I386%\" /Q /R /U /Y >nul
    XCopy "%%I\Files\Compressed\*.*" "%I386%\" /Q /R /U /Y >nul

    If Exist "%%I\Files\ForceCopy\*.*" XCopy "%%I\Files\ForceCopy\*.*" "%I386%\" /E /Q /R /Y >nul
  )
GoTo :EOF

:ProcessStartEvent
  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    If Exist "%%I\Start" (
      If Exist Temp\Start.cmd Del /F /Q Temp\Start.cmd

      Copy "%%I\Start" Temp\Start.cmd >nul
      Call Temp\Start.cmd "%%I"
    )
  )
GoTo :EOF

:ProcessSpecialCase
  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    If Exist "%%I\SpecCase" (
      If Exist Temp\SC.cmd Del /F /Q Temp\SC.cmd

      Copy "%%I\SpecCase" Temp\SC.cmd >nul
      Call Temp\SC.cmd "%%I"
    )
  )
GoTo :EOF

:ProcessFinishEvent
  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    If Exist "%%I\Finish" (
      If Exist Temp\Finish.cmd Del /F /Q Temp\Finish.cmd

      Copy "%%I\Finish" Temp\Finish.cmd >nul
      Call Temp\Finish.cmd "%%I"
    )
  )
GoTo :EOF

:CleanEmptyDirs
  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    If Exist "%%I\Files\Compressed" RD "%%I\Files\Compressed" 2>nul
  )
  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    If Exist "%%I\Files" RD "%%I\Files" 2>nul
  )
GoTo :EOF

:CheckFilesDir
  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    If Not Exist "%%I\Files" MD "%%I\Files"
  )
GoTo :EOF

:CheckUpdate
  Set Restart=

  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    If Exist "%%I\Upd" (
      If "%%I"=="." (
        Echo Обновление.
      ) Else (
        Echo Обновление %%I.
      )
      PushD "%%I"

      If Exist Files\Compressed RD /S /Q Files\Compressed
      If Exist Files\Compressed Call :Halt "Ошибка: не удалось удалить папку %%I\Files\Compressed"

      If Exist Upd.cmd Del /F /Q Upd.cmd
      If Exist Upd.cmd Call :Halt "Ошибка: не удалось удалить файл Upd.cmd"
      Ren Upd Upd.cmd
      If Exist Upd Call :Halt "Ошибка: не удалось выполнить Upd"
      Call Upd.cmd
      Del /F /Q Upd.cmd
      If Exist Upd.cmd Echo Не удалось удалить файл Upd.cmd

      PopD
    )
  )
  If "!Restart!"=="1" Call :Quit "Вспомогательные операции завершены, перезапустите скрипт."
GoTo :EOF

:CheckSystemRoot
  If /I Not "%Distr%"=="%SystemRoot%" (
    Echo %Distr% | Find /I "%SystemRoot%\" >nul
    If Not "!ErrorLevel!"=="0" GoTo :EOF
  )
  Echo Внимание: активизирован режим дуракоустойчивости.
  Echo Путь к дистрибутиву:  %Distr%
  Echo Системная директория: %SystemRoot%
  Echo.
  Echo По-видимому, Вы пытаетесь применить пакет к уже установленной системе.

  Call :Quit "Данный пакет предназначен только для интеграции в дистрибутив."
GoTo :EOF

:CheckDelDrv
  If Not Exist Temp\DelDrv.lst GoTo :EOF
  Echo Удаление драйверов.

  For /F "Delims=" %%I In (Temp\DelDrv.lst) Do (
    Set V=%%~xI
    If "!V!"=="" Set V=.
    Set V=!V:~0,3!_

    Set LineIsAdded=
    For %%K In ("%I386%\*.CAB") Do (
      If Exist "Temp\CABs\%%~nK\%%I" (
        If Not Exist Temp\%%~nK.flg Echo.>Temp\%%~nK.flg
        Del /F /Q "Temp\CABs\%%~nK\%%I"

        If "!LineIsAdded!"=="" (
          Echo d1,%%I>>Temp\dosnet.lst
          Echo %%I>>Temp\txtsetup.lst
          Echo %%I>>Temp\deldrv2.lst

          Set LineIsAdded=1
        )
      )
    )
    If Exist "%I386%\%%I" (
      Del /F /Q "%I386%\%%I"

      If "!LineIsAdded!"=="" (
        Echo d1,%%I>>Temp\dosnet.lst
        Echo %%I>>Temp\txtsetup.lst
        Set LineIsAdded=1
      )
    )
    If Exist "%I386%\%%~nI!V!" (
      Del /F /Q "%I386%\%%~nI!V!"

      If "!LineIsAdded!"=="" (
        Echo d1,%%I>>Temp\dosnet.lst
        Echo %%I>>Temp\txtsetup.lst
        Set LineIsAdded=1
      )
    )
  )
  If Exist Temp\dosnet.lst (
    FindStr /B /L /I /V /G:Temp\dosnet.lst "%I386%\DOSNET.INF" >Temp\DOSNET.INF
    XCopy Temp\DOSNET.INF "%I386%\" /Q /R /Y >nul

    FindStr /B /L /I /V /G:Temp\txtsetup.lst "%I386%\TXTSETUP.SIF" >Temp\TXTSETUP.SIF
    XCopy Temp\TXTSETUP.SIF "%I386%\" /Q /R /Y >nul
  )
  If Exist Temp\deldrv2.lst (
    FindStr /B /L /I /V /G:Temp\deldrv2.lst "%I386%\DRVINDEX.INF" >Temp\DRVINDEX.INF
    XCopy Temp\DRVINDEX.INF "%I386%\" /Q /R /Y >nul
  )
GoTo :EOF

:CheckDelOC
  If Not Exist Temp\DelOC.lst GoTo :EOF
  Echo Удаление компонентов.

  For /F "Delims=" %%I In (Temp\DelOC.lst) Do (
    Set V=%%~xI
    If "!V!"=="" Set V=.
    Set V=!V:~0,3!_

    If Exist "%I386%\%%I" (
      Del /F /Q "%I386%\%%I"

      Echo d1,%%I>>Temp\dn2.lst
      Echo %%I>>Temp\ts2.lst
    )
    If Exist "%I386%\%%~nI!V!" (
      Del /F /Q "%I386%\%%~nI!V!"

      Echo d1,%%I>>Temp\dn2.lst
      Echo %%I>>Temp\ts2.lst
    )
    If /I "%%~xI"==".inf" Echo ,%%I,>>Temp\sysoc.lst
  )
  If Exist Temp\dn2.lst (
    FindStr /B /L /I /V /G:Temp\dn2.lst "%I386%\DOSNET.INF" >Temp\DOSNET.INF
    XCopy Temp\DOSNET.INF "%I386%\" /Q /R /Y >nul

    FindStr /B /L /I /V /G:Temp\ts2.lst "%I386%\TXTSETUP.SIF" >Temp\TXTSETUP.SIF
    XCopy Temp\TXTSETUP.SIF "%I386%\" /Q /R /Y >nul
  )
  If Exist Temp\sysoc.lst (
    Expand -r "%I386%\SYSOC.IN_" Temp\1\ >nul
    Call :SetChcpWin
    Type Temp\1\sysoc.inf>Temp\sysoc0.inf
    Call :SetChcpDos

    FindStr /L /I /V /G:Temp\sysoc.lst Temp\sysoc0.inf >Temp\sysoc.inf
    Del /F /Q "%I386%\SYSOC.IN_"
    MakeCab /L "%I386%" Temp\sysoc.inf SYSOC.IN_ >nul

    Del /F /Q Temp\sysoc.inf
  )
GoTo :EOF

:CheckRequired
  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    If Exist "%%I\Required" (
      For /F "UseBackQ Delims=" %%K In ("%%I\Required") Do (
        FindStr /L /I /X /C:"Addons\%%~K" Temp\AllFolders.txt >nul

        If Not "!ErrorLevel!"=="0" (
          Echo Ошибка: для дополнения %%~nxI требуется дополнение %%K.
          Set ErrFlag=1
        )
      )
    )
  )
GoTo :EOF

:CheckConflict
  For /F "Delims=" %%I In (Temp\AllFolders.txt) Do (
    If Exist "%%I\Conflict" (
      For /F "UseBackQ Delims=" %%K In ("%%I\Conflict") Do (
        FindStr /L /I /X /C:"Addons\%%~K" Temp\AllFolders.txt >nul

        If "!ErrorLevel!"=="0" (
          Echo Ошибка: дополнение %%~nxI несовместимо с дополнением %%K.
          Set ErrFlag=1
        )
      )
    )
  )
GoTo :EOF

:CheckNfo
  For /F "Delims=" %%I In (Temp\Folders.txt) Do (
    If Exist "%%I\Addon.nfo" (
      Type "%%I\Addon.nfo" >>Temp\ADDONS.TXT
      Echo -------------------------------------------------->>Temp\ADDONS.TXT
    )
  )
  If Exist Temp\ADDONS.TXT (
    If Exist "%Distr%\ADDONS.TXT" (
      Attrib -R "%Distr%\ADDONS.TXT"
      Type Temp\ADDONS.TXT >>"%Distr%\ADDONS.TXT"
    ) Else (
      Copy Temp\ADDONS.TXT "%Distr%\" >nul
    )
  )
GoTo :EOF

:CheckIntBefore
  If Not Exist "%I386%\ADDONS.LST" (
    Copy /Y Temp\Folders.txt Temp\AllFolders.txt >nul
    GoTo :EOF
  )
  Set SeparateAddons=1
  For /F "UseBackQ Delims=" %%I In (`FindStr /L /I /X /G:Temp\Folders.txt "%I386%\ADDONS.LST"`) Do (
    If "%%~I"=="." (
      Echo Внимание: набор обновлений уже был интегрирован в данный дистрибутив.
    ) Else (
      Echo Внимание: дополнение %%~nxI уже было интегрировано в данный дистрибутив.
    )
  )
  FindStr /L /I /X /V /G:"%I386%\ADDONS.LST" Temp\Folders.txt >Temp\F2.txt
  Copy /Y Temp\F2.txt Temp\Folders.txt >nul

  Attrib -R "%I386%\ADDONS.LST"
  Copy /Y "%I386%\ADDONS.LST" Temp\AllFolders.txt >nul
  Type Temp\Folders.txt >>Temp\AllFolders.txt
GoTo :EOF

:CopyIntBefore
  Copy /Y Temp\AllFolders.txt "%I386%\ADDONS.LST" >nul
GoTo :EOF

:SP3BugFix
If Exist "%I386%\SP3.CAT" (
Echo Подготовка дистрибутива.
  Expand -r "%I386%\WBEMOC.IN_" Temp\1\ >nul
  If Exist Temp\1\wbemoc.inf (
    Call :SetChcpWin
    Type Temp\1\wbemoc.inf>Temp\wbemoc.tmp
    Call :SetChcpDos
    Call :WBEM_ReplMOFs
  )
)
If Exist "Temp\wbemoc.inf" (
     Del /F /Q "%I386%\WBEMOC.IN_"
     MakeCab /D CompressionMemory=21 /D CompressionType=LZX /L "%I386%" "Temp\wbemoc.inf" WBEMOC.IN_ > nul
)
GoTo :EOF

:WBEM_ReplMOFs
 Set Repl=0
 For /F "Delims=<!|&> EOL=>" %%I In (Temp\wbemoc.tmp) Do (
   Set Line=%%I
   If "!Line:~0,1!"=="[" (
     If /I "!Line!"=="[WBEM.CopyMOFs]" (Set Repl=1) Else (Set Repl=0)
     Echo.>>"Temp\wbemoc.inf"
   )
   If "!Repl!"=="1" (
     If /I "!Line!"=="napclientprov.mof" Set Line=;!Line! 
     If /I "!Line!"=="napclientschema.mof" Set Line=;!Line! 
     If /I "!Line!"=="rsop.mfl" Set Line=;!Line! 
     If /I "!Line!"=="rsop.mof" Set Line=;!Line! 
     If /I "!Line!"=="wscenter.mof" Set Line=;!Line! 
   )
   Echo !Line!>>"Temp\wbemoc.inf"
 )
 Set Line=
 Set Repl=
GoTo :EOF

:SetChcpWin
Chcp 1251 >nul
GoTo :EOF

:SetChcpDos
Chcp 866 >nul
GoTo :EOF

