@echo off
rem *** Author: T. Wittrock, RZ Uni Kiel ***

verify other 2>nul
setlocal enableextensions
if errorlevel 1 goto NoExtensions

%~d0
cd "%~p0"

for %%i in (all enu deu nld esn fra ptg ptb ita rus plk ell csy dan nor sve fin) do (if /i "%1"=="%%i" goto Valid1)
for %%i in (w2k wxp w2k3 o2k oxp o2k3) do (
  if /i "%1"=="%%i" (
    for %%j in (enu deu nld esn fra ptg ptb ita rus plk ell csy dan nor sve fin) do (if /i "%2"=="%%j" goto Valid2)
    goto Valid1
  )
)
for %%i in (w60 o2k7) do (
  if /i "%1"=="%%i" (
    if /i "%2"=="glb" shift /2
    goto Valid1
  )
)
goto InvalidParams

:Valid1
set CREATE_DVD=1
if %2~==~ (goto InvalidParams) else (set OUTPUT_PATH=%~fs2)
for %%i in (all w2k wxp w2k3 w60 o2k oxp o2k3 o2k7) do (if /i "%1"=="%%i" goto V1CopyFilter)
rem *** Generate filter ***
echo Generating filter for %1...
copy /Y ..\exclude\ExcludeListUSB-all.txt "%TEMP%\ExcludeListUSB-%1.txt" > nul
for %%i in (enu deu nld esn fra ptg ptb ita rus plk ell csy dan nor sve fin) do (
  if /i "%1" NEQ "%%i" (
    echo %%i\>> "%TEMP%\ExcludeListUSB-%1.txt"
  )
)
if /i "%1" NEQ "enu" dir /B ..\client\o2k3\glb\*2003SP*.*>> "%TEMP%\ExcludeListUSB-%1-%2.txt"
goto NoMoreParams
:V1CopyFilter
rem *** Copy filter ***
echo Copying filter for %1...
if exist ..\exclude\ExcludeListUSB-%1.txt (
  copy /Y ..\exclude\ExcludeListUSB-%1.txt "%TEMP%\ExcludeListUSB-%1.txt" > nul
) else (
  copy /Y ..\exclude\ExcludeListUSB-%1-x86.txt "%TEMP%\ExcludeListUSB-%1.txt" > nul
)
goto NoMoreParams

:Valid2
if %3~==~ (goto InvalidParams) else (set OUTPUT_PATH=%~fs3)
rem *** Generate filter ***
echo Generating filter for %1 %2...
if exist ..\exclude\ExcludeListUSB-%1.txt (
  copy /Y ..\exclude\ExcludeListUSB-%1.txt "%TEMP%\ExcludeListUSB-%1-%2.txt" > nul
) else (
  copy /Y ..\exclude\ExcludeListUSB-%1-x86.txt "%TEMP%\ExcludeListUSB-%1-%2.txt" > nul
)
for %%i in (enu deu nld esn fra ptg ptb ita rus plk ell csy dan nor sve fin) do (
  if /i "%2" NEQ "%%i" (
    echo %%i\>> "%TEMP%\ExcludeListUSB-%1-%2.txt"
  )
)
if /i "%1"=="o2k3" (
  if /i "%2" NEQ "enu" (
    dir /B ..\client\o2k3\glb\*2003SP*.*>> "%TEMP%\ExcludeListUSB-%1-%2.txt"
  )
)
goto NoMoreParams

:NoMoreParams
if "%CREATE_DVD%"=="1" goto DVD

rem *** Copy client tree ***
title Copying client tree for %1 %2...
echo Copying client tree for %1 %2...
if not exist %OUTPUT_PATH%\nul goto InvalidTarget
pushd ..\client
xcopy *.* %OUTPUT_PATH% /E /Q /Y /EXCLUDE:%TEMP%\ExcludeListUSB-%1-%2.txt
if errorlevel 1 goto XCopyError
popd
del "%TEMP%\ExcludeListUSB-%1-%2.txt"
goto EoF

:DVD
rem *** Copy client tree ***
title Copying client tree for %1...
echo Copying client tree for %1...
if not exist %OUTPUT_PATH%\nul goto InvalidTarget
pushd ..\client
xcopy *.* %OUTPUT_PATH% /E /Q /Y /EXCLUDE:%TEMP%\ExcludeListUSB-%1.txt
if errorlevel 1 goto XCopyError
popd
del "%TEMP%\ExcludeListUSB-%1.txt"
goto EoF

:NoExtensions
echo.
echo ERROR: No command extensions available.
echo.
goto Error

:InvalidParams
echo.
echo ERROR: Invalid parameter: %1 %2 %3 %4
echo Usage1: %~n0 {w2k ^| wxp ^| w2k3 ^| w60 ^| o2k ^| oxp ^| o2k3 ^| o2k7} {enu ^| deu ^| nld ^| esn ^| fra ^| ptg ^| ptb ^| ita ^| rus ^| plk ^| ell ^| csy ^| dan ^| nor ^| sve ^| fin} TargetPath
echo Usage2: %~n0 {all ^| w2k ^| wxp ^| w2k3 ^| w60 ^| o2k ^| oxp ^| o2k3 ^| o2k7 ^| enu ^| deu ^| nld ^| esn ^| fra ^| ptg ^| ptb ^| ita ^| rus ^| plk ^| ell ^| csy ^| dan ^| nor ^| sve ^| fin} TargetPath
echo.
goto Error

:InvalidTarget
echo.
echo ERROR: Directory %OUTPUT_PATH% not found.
echo.
goto Error

:XCopyError
echo.
echo ERROR: Copying failed.
echo.
goto Error

:Error
endlocal
exit /b 1

:EoF
title %ComSpec%
endlocal
