@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=prepare.txt

pushd "%LISTDIR%"

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

rem ==== operations ====

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

rem copy "%GNUWIN32DIR%\bin\*.dll" "%BIN%"
rem 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
popd
pause
exit /b %EXITCODE%
endlocal
