if not Defined WS_USE_FAKE_SETUP Set WS_USE_FAKE_SETUP=0

SET  WDir=%~1
If "%VOLNAME%"=="" SET  VOLNAME=WinXP_SP2
If ~%MAKEISO%~==~~ SET  MAKEISO=0
If ~%BaseTargetDistrib%~==~~ Set BaseTargetDistrib=%Distr%

ECHO  ⨧㧮筮 ᪠.

REM 室 ஢ ୮ ⥣樨 

Attrib -R "%I386%\ADDONS.LST"
FindStr /L /I /V /C:"ZZ_MultiBoot" "%I386%\ADDONS.LST"> "%I386%\ADDONS.TMP"
DEL "%I386%\ADDONS.LST"
REN "%I386%\ADDONS.TMP" *.LST

REM  । ⮪ ⥣樨

Rem  ६ VOLM_Name, VOLD_Name, VOLS_Name  ᮤঠ ஢ 4 ᪨ ᨬ 
If ~%VOLD_Name%~==~~ set VOLD_Name=VOLD
If ~%VOLS_Name%~==~~ set VOLS_Name=VOLS
If ~%VOLM_Name%~==~~ set VOLM_Name=VOLM
If ~%UserInfoFileName%~==~~ set UserInfoFileName=userinfo.ini

Set SifDirEnum=VOLD VOLS VOLM
Set BootDirEnum=%VOLD_Name% %VOLS_Name% %VOLM_Name%

Call :DelBootDir

IF EXIST "%BaseTargetDistrib%\BCDW\" RD /s /q "%BaseTargetDistrib%\BCDW" > nul
IF EXIST "%BaseTargetDistrib%\BOOT\" RD /s /q "%BaseTargetDistrib%\BOOT" > nul
IF EXIST "%BaseTargetDistrib%\ISO\" RD /s /q "%BaseTargetDistrib%\ISO" > nul
IF EXIST "%BaseTargetDistrib%\..\ISO\" RD /s /q "%BaseTargetDistrib%\..\ISO" > nul

Call :MakeBootDir

REM  ᮭ   䠩 業ਥ

Call :AddPersonalInfo

XCopy "%WDir%\BCDW\*.*" "%BaseTargetDistrib%\BCDW\" /E /H /R /Y > nul
XCopy "%WDir%\BOOT\*.*" "%BaseTargetDistrib%\BOOT\" /E /H /R /Y > nul

REM  ᮢ襭 㦭   䠩.
REM ।஢  . 

IF EXIST "%Distr%\SUPPORT" RD /s /q "%Distr%\SUPPORT" > nul
IF EXIST "%Distr%\VALUEADD" RD /s /q "%Distr%\VALUEADD" > nul
IF EXIST "%Distr%\DOTNETFX" RD /s /q "%Distr%\DOTNETFX" > nul
rem IF EXIST "%Distr%\DOCS" RD /s /q "%Distr%\DOCS" > nul
rem IF EXIST "%Distr%\AUTORUN.INF" DEl /F /Q "%Distr%\AUTORUN.INF" > nul
rem IF EXIST "%Distr%\README.HTM" DEl /F /Q "%Distr%\README.HTM" > nul
rem IF EXIST "%Distr%\SETUPXP.HTM" DEl /F /Q "%Distr%\SETUPXP.HTM" > nul
rem IF EXIST "%Distr%\SETUP.EXE" DEl /F /Q "%Distr%\SETUP.EXE" > nul

REM  㦭 . 
REM  ⮩ ⠭  㤠 .

IF EXIST "%I386%\LANG" RD /S /Q "%I386%\LANG" > nul
rem IF EXIST "%I386%\WIN9XMIG" RD /S /Q "%I386%\WIN9XMIG" > nul
rem IF EXIST "%I386%\WIN9XUPG" RD /S /Q "%I386%\WIN9XUPG" > nul
rem IF EXIST "%I386%\WINNTUPG" RD /S /Q "%I386%\WINNTUPG" > nul
rem IF EXIST "%I386%\COMPDATA" RD /S /Q "%I386%\COMPDATA" > nul
rem IF EXIST "%I386%\DRW" RD /S /Q "%I386%\DRW" > nul

Call :CopySifInfFile

REM  SETUPLDR.BIN  ⨧㧪.
Call :PathSETUPLDR_BIN

REM  ᥩ  ࠩ MASS storage.
%WDir%\DpsDel.exe "%BaseTargetDistrib%\%VOLD_Name%\" -Q > nul 

REM  ᥩ  ࠩ MASS storage  ⨫.
%WDir%\DpsDel.exe "%BaseTargetDistrib%\%VOLS_Name%\" -Q > nul 

REM 室 䥩 ⠯  ⠭  ࠩ஢.
if not WS_USE_FAKE_SETUP==0 (set txtsetup_utl=txtsetup.utl2) else (set txtsetup_utl=txtsetup.utl)
  if Exist %WDir%\%txtsetup_utl% (
    FindStr /B /I /V /G:%WDir%\%txtsetup_utl% "%BaseTargetDistrib%\%VOLS_Name%\TXTSETUP.SIF" > "%BaseTargetDistrib%\%VOLS_Name%\txtsetup1.sif"
    DEL "%BaseTargetDistrib%\%VOLS_Name%\TXTSETUP.SIF" > nul
    REN "%BaseTargetDistrib%\%VOLS_Name%\txtsetup1.sif" "TXTSETUP.SIF" > nul
  )


ECHO [SourceDisksFiles]>>"%BaseTargetDistrib%\%VOLS_name%\TXTSETUP.SIF"

if [WS_USE_FAKE_SETUP]==[0] (
  ECHO setup.org = 100,,,,,,,2,0,0,setup.exe >>"%BaseTargetDistrib%\%VOLS_Name%\TXTSETUP.SIF"
)


%WDir%\INFCLEAN "%BaseTargetDistrib%\%VOLS_Name%\TXTSETUP.SIF" > nul
%WDir%\INFCLEAN "%BaseTargetDistrib%\%VOLD_Name%\TXTSETUP.SIF" > nul

REM ஢ Boot 䠩    $WIN_NT$.~BT
Call :CopyFilesFrom_Dos_Net

IF EXIST %I386%\$OEM$ MOVE %I386%\$OEM$ %BaseTargetDistrib% > nul
COPY "%BaseTargetDistrib%\BCDW\Loader.bin" "%BaseTargetDistrib%\bootsec.bin" > nul

if %MAKEISO%==1 (
ECHO  ࠧ ISO.
MD "%Distr%\..\ISO"
start /wait %WDir%\OSCDIMG.EXE -b%BaseTargetDistrib%\bootsec.bin -n -h -l%VOLNAME% -ois -m  %BaseTargetDistrib%\ %BaseTargetDistrib%\..\ISO\WINXP.ISO 
)

GOTO :EOF

:DelBootDir
  For %%I in (%BootDirEnum%) do IF EXIST "%BaseTargetDistrib%\%%I\" RD /s /q "%BaseTargetDistrib%\%%I" > nul
goto :eof

:MakeBootDir
  For %%I in (%BootDirEnum%) do MD "%BaseTargetDistrib%\%%I"
Goto :eof

:AddPersonalInfo
Rem *************************************************************************
For %%I in (%SifDirEnum%) do (
 Setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION 
   If "%%I"=="VOLD" set DD=%VOLD_Name%
   If "%%I"=="VOLM" set DD=%VOLM_Name%
   If "%%I"=="VOLS" set DD=%VOLS_Name%
   Copy /b "%WDir%\%%I\WINNT.SIF" + "%WDir%\%UserInfoFileName%" "%BaseTargetDistrib%\!DD!\WINNT.SIF" > nul
   Copy /b "%WDir%\%%I\WINNT.SIE" + "%WDir%\%UserInfoFileName%" "%BaseTargetDistrib%\!DD!\WINNT.SIE" > nul
   Copy /b "%WDir%\%%I\WINNT.SID" + "%WDir%\%UserInfoFileName%" "%BaseTargetDistrib%\!DD!\WINNT.SID" > nul
   Copy /b "%WDir%\%%I\WINNT.SIC" + "%WDir%\%UserInfoFileName%" "%BaseTargetDistrib%\!DD!\WINNT.SIC" > nul
   Copy /b "%WDir%\%%I\WINNT.SIB" + "%WDir%\%UserInfoFileName%" "%BaseTargetDistrib%\!DD!\WINNT.SIB" > nul
   Copy /b "%WDir%\%%I\WINNT.SIA" + "%WDir%\%UserInfoFileName%" "%BaseTargetDistrib%\!DD!\WINNT.SIA" > nul
  endlocal
)
goto :eof
Rem *************************************************************************

:CopySifInfFile
Rem *************************************************************************
For %%I in (%BootDirEnum%) do (
  COPY "%I386%\TXTSETUP.SIF" "%BaseTargetDistrib%\%%I\" > nul
  COPY "%I386%\dosnet.inf" "%BaseTargetDistrib%\%%I\" > nul
  REM VVS ⠭   ਡ⨢ (SetupSourcePath  TXTSETUP.SIF)
  Call :SetupSourcePath "%BaseTargetDistrib%\%%I\TXTSETUP.SIF"
)
Rem VVS ஢ થ 䠩
Call :CopyMarkerFile "%BaseTargetDistrib%"

Goto :eof
Rem *************************************************************************


:PathSETUPLDR_BIN
 For %%I in (%BootDirEnum%) do (
  COPY "%I386%\SETUPLDR.BIN" "%BaseTargetDistrib%\%%I\SETUPLDA.BIN" > nul 
  COPY "%I386%\SETUPLDR.BIN" "%BaseTargetDistrib%\%%I\SETUPLDB.BIN" > nul
  COPY "%I386%\SETUPLDR.BIN" "%BaseTargetDistrib%\%%I\SETUPLDC.BIN" > nul
  COPY "%I386%\SETUPLDR.BIN" "%BaseTargetDistrib%\%%I\SETUPLDD.BIN" > nul
  COPY "%I386%\SETUPLDR.BIN" "%BaseTargetDistrib%\%%I\SETUPLDE.BIN" > nul
  COPY "%I386%\SETUPLDR.BIN" "%BaseTargetDistrib%\%%I\SETUPLDR.BIN" > nul

  %WDir%\LdrPatch.exe "%BaseTargetDistrib%\%%I\SETUPLDA.BIN" /d:%%I /u:winnt.sia > nul 
  %WDir%\LdrPatch.exe "%BaseTargetDistrib%\%%I\SETUPLDB.BIN" /d:%%I /u:winnt.sib > nul
  %WDir%\LdrPatch.exe "%BaseTargetDistrib%\%%I\SETUPLDC.BIN" /d:%%I /u:winnt.sic > nul
  %WDir%\LdrPatch.exe "%BaseTargetDistrib%\%%I\SETUPLDD.BIN" /d:%%I /u:winnt.sid > nul
  %WDir%\LdrPatch.exe "%BaseTargetDistrib%\%%I\SETUPLDE.BIN" /d:%%I /u:winnt.sie > nul

  DEL /Q "%BaseTargetDistrib%\%%I\*.log" > nul
  DEL /Q "%BaseTargetDistrib%\%%I\*.bak" > nul
)
goto :Eof

:CopyFilesFrom_Dos_Net
For %%I in (%BootDirEnum%) do (
  %WDir%\MakeVol.exe "%BaseTargetDistrib%\%%I\dosnet.inf" "%I386%" "%BaseTargetDistrib%\%%I" > nul
  DEL "%BaseTargetDistrib%\%%I\dosnet.inf"
)
Goto :eof



:GetStringLen
Rem *********************************************************************
Rem * ᯮ짮 :GetStringLen "ப ᨬ" VariableName        *
Rem * %1 ("ப ᨬ") - cப  ன 㦭 ।    *
Rem *    (ᥣ  窠)                                            *
Rem * %2 (VariableName)-  ६ ன 㦭 ᢮         *
Rem *     祭                                                 *
Rem *********************************************************************
Echo %1>tmp
for %%a in (tmp) do (
 set /a %2=%%~za-2
 Del tmp
)
goto :eof
Rem *********************************************************************


:SetupSourcePath
Rem *********************************************************************
Rem * ⠭ 㦭 ⥩  ਡ⨢ ᫨   稪    *
Rem *     ஢  I386                                   *
Rem * %1 -   TxtSetup.si?                                          *
Rem *********************************************************************
Rem 1.  ᮢ, 室
If %BaseTargetDistrib%==%Distr% Goto :eof
Rem 2. 塞 ⭮⥫ 
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION 
  set LessPart=!Distr:%BaseTargetDistrib%=!\
  call :Read2FromSIF %1 "SetupSourcePath" ssp
  call :ReplStrInFile "%SSP%" "SetupSourcePath ="%LessPart%^"" "%~1" "%~1"
Endlocal
Goto :eof
Rem *********************************************************************

:Read2FromSIF 
Rem ********************************************
Rem * %1   䠩  ᪠               *
Rem * %2  ᪠                            *
Rem * %3   ६  १ *
Rem ********************************************
  Set FileToRead=%1
  Set Key=%~2
  For /F "usebackq delims=" %%i In (`Find /I "%Key%" %FileToRead%`) Do Set %3=%%i
  Set FileToRead=
  Set Key=
goto :eof
Rem *********************************************************************


:ReplStrInFile 
Rem *****************************************
Rem *  ப   䠩              *
Rem * %1  ᪠                         *
Rem * %2                         *
Rem * %3  䠩  ᪠               *
Rem * %4  䠩 㤠  १ *
Rem *****************************************
setlocal
set A=%~1
Set FLN=%4
Set SameFile=0
if %3==%4 (
  Set SameFile=1
  Set FLN="%temp%\%~nx4.tmp"
  if exist !FLN! Del !FLN! >nul
)

For /F "delims=" %%I in (%~3) do (
  if "%%I"=="%A%" (@Echo %~2 >>%FLN%) ELSE @Echo %%I >>%FLN%
)

if %SameFile%==1 (
  Copy /Y /b %FLN% "%~4" >nul
  Del %FLN% >nul
)
goto :eof
Rem *********************************************************************

:CopyMarkerFile 
Rem ***********************************************
Rem * ஢ થ 䠩  ७ ᪠ *
Rem * %1 -    ᪠                     *
Rem ***********************************************
If "%~1"=="%Distr%" Goto :eof
Copy /y /b "%Distr%\win51*.*" %1 >nul
Copy /y /b "%Distr%\BOOTFONT.BIN" %1 >nul
goto :eof
Rem *********************************************************************

