@echo off

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

if not exist %WGET% (echo ERROR: cannot find the wget.exe & goto notfound)
if not exist %DLDIR% mkdir %DLDIR%

rem ==== operations ====

rem ==== downloading and checking (easy) ====
rem downloading, retry(-t)3, timeout(-T)30, new'n'differsonly(-N), noredirect(--max-redirect), dir2store(-P)
for /F "tokens=1" %%A in (%LIST%) do (%WGET% %%A -t 3 -T 30 -N --max-redirect 0 -P %DLDIR% || goto dlerror)
rem check file existence
for /F "tokens=2" %%A in (%LIST%) do if not exist %DLDIR%\%%A (echo cannot find %%A & goto dlerror)

echo INFO: download finished

goto end

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

:end
pause
exit /b %EXITCODE%
endlocal
