@echo off

rem ==== setup ====
setlocal
rem this contains trailing "\"
set SELFDIR=%~dp0
call %SELFDIR%xx_env.bat
set EXITCODE=0

set ESEWGET=cscript.exe %BIN%\wgetmodoki.wsf
rem override download list
set LIST=%LISTDIR%\prepare.txt

if not exist %DLDIR% mkdir %DLDIR%
if not exist %GNUWIN32DIR% mkdir %GNUWIN32DIR%

rem ==== operations ====

rem ==== downloading and checking (easy) ====
for /F "tokens=1" %%A in (%LIST%) do (%ESEWGET% %%A -t 3 -T 30 -N --max-redirect 0 -P %DLDIR% || goto dlerror)
for /F "tokens=2" %%A in (%LIST%) do if not exist %DLDIR%\%%A (echo cannot find %%A & goto dlerror)
for /F "tokens=2" %%A in (%LIST%) do (call %EXCOMMAND% %DLDIR%\%%A %GNUWIN32DIR% & if ERRORLEVEL 1 goto failed)

copy %GNUWIN32DIR%\bin\*.dll %BIN%
copy %GNUWIN32DIR%\bin\*.exe %BIN%

echo INFO: download and extraction finished
goto end

rem ==== errors ====
:dlerror
echo ERROR: download failed
:notfound
set EXITCODE=1
goto end

:failed
echo ERROR: extraction failed
set EXITCODE=1
goto end

:end
pause
exit /b %EXITCODE%
endlocal
