1 :: Make sure the extensions are enabled
3 @setlocal EnableExtensions EnableDelayedExpansion
5 @call :print_usage "Failed to enable extensions"
9 ::Change the code page to unicode
10 @chcp 65001 1>nul 2>nul
12 @call :print_usage "Failed to change the code page to unicode"
16 :: Set up some global variables
17 @set "script_name=%~nx0"
18 @set "script_folder=%~dp0"
19 @set "script_folder=%script_folder:~0,-1%"
20 @set "dependency_path=%TEMP%\mingw-build-dependencies"
22 :: Check the command line parameters
25 @if [%1] == [] goto :options_parsed
30 @if /i [%arg%] == [/?] (
31 @call :print_usage "Downloads a specific version of MinGW"
34 @if /i [%arg%] == [/q] set quiet=true
35 @if /i [%two%] == [/v] @if /i not [%three%] == [/ve] @call :verbosity "!arg!"
36 @if /i [%arg%] == [/version] set "version=%~2" & shift
37 @if /i [%arg%] == [/arch] set "arch=%~2" & shift
38 @if /i [%arg%] == [/exceptions] set "exceptions=%~2" & shift
39 @if /i [%arg%] == [/threading] set "threading=%~2" & shift
40 @if /i [%arg%] == [/revision] set "revision=%~2" & shift
41 @if /i not [!one!] == [/] (
42 if not defined output_path (
45 @call :print_usage "Too many output locations: !output_path! !arg!" ^
46 "There should only be one output location"
53 @if defined quiet set logging_level=0
54 @if not defined output_path set "output_path=%script_folder%\mingw-builds"
55 @set "output_path=%output_path:/=\%"
58 @set "log_folder=%output_path%\logs"
59 @call :iso8601 timestamp
60 @set "log_path=%log_folder%\%timestamp%.log"
63 :: Get default architecture
64 @if not defined arch @call :architecture arch
66 :: Only keep a certain amount of logs
67 @set /a "log_keep=log_keep-1"
68 @if not exist %log_folder% @mkdir %log_folder%
69 @for /f "skip=%log_keep%" %%f in ('dir /b /o-D /tc %log_folder%') do @(
70 @call :log 4 "Removing old log file %log_folder%\%%f"
74 :: Set up some more global variables
75 @call :windows_version win_ver win_ver_major win_ver_minor win_ver_rev
76 @call :script_source script_source
77 @if [%script_source%] == [explorer] (
78 set /a "logging_level=logging_level+1"
81 :: Execute the main function
82 @call :main "%arch%" "%version%" "%threading%" "%exceptions%" "%revision%"
84 @call :log 0 "Failed to download MinGW"
85 @call :log 0 "View the log at %log_path%"
89 :: Stop the script if the user double clicked
90 @if [%script_source%] == [explorer] (
97 :: -------------------------- Functions start here ----------------------------
99 :main - Main function that performs the download
100 :: %1 - Target architecture
101 :: %2 - Version of MinGW to get [optional]
102 :: %3 - Threading model [optional]
103 :: %4 - Exception model [optional]
104 :: %5 - Package revision [optional]
107 @call :log 2 "Welcome to the MinGW download script"
108 @call :log 6 "------------------------------------"
110 @call :log 2 "This script downloads a specific version of MinGW"
112 @if "%arch%" == "" @exit /b 1
115 @set "exceptions=%~4"
117 @call :log 3 "arch = %arch%"
118 @call :log 3 "version = %version%"
119 @call :log 3 "exceptions = %exceptions%"
120 @call :log 3 "threading = %threading%"
121 @call :log 3 "revision = %revision%"
122 @call :repository repo
124 @call :log 0 "Failed to get the MinGW-builds repository information"
127 @call :resolve slug url "%repo%" "%arch%" "%version%" "%threading%" "%exceptions%" "%revision%"
129 @call :log 0 "Failed to resolve the correct URL of MinGW"
132 @call :unpack compiler_path "%url%" "%output_path%\mingw\%slug%"
134 @call :log 0 "Failed to unpack the MinGW archive"
137 @rmdir /s /q "%dependency_path%"
138 @echo.%compiler_path%
142 :repository - Gets the MinGW-builds repository
143 :: %1 - The return variable for the repository file path
145 @setlocal EnableDelayedExpansion
147 @call :log 0 "Failed to enable extensions"
151 @if "%var%" == "" @exit /b 1
153 @call :log 2 "Getting MinGW repository information"
154 @set "url=http://downloads.sourceforge.net/project/mingw-w64/Toolchains targetting Win32/Personal Builds/mingw-builds/installer/repository.txt"
156 @call :log 1 "Downloading MinGW repository"
157 @set "file_path=%dependency_path%\mingw-repository.txt"
158 @call :download "%url%" "%file_path%"
160 @call :log 0 "Failed to download the MinGW repository information"
163 @set "repository_path=%dependency_path%\repository.txt"
164 @del "%repository_path%" 2>nul
165 @for /f "delims=| tokens=1-6,*" %%a in (%file_path%) do @(
167 @set "version=!version: =!"
169 @set "arch=!arch: =!"
170 @set "threading=%%~c"
171 @set "threading=!threading: =!"
172 @set "exceptions=%%~d"
173 @set "exceptions=!exceptions: =!"
175 @set "revision=!revision: =!"
176 @set "revision=!revision:rev=!"
178 @set "url=!url:%%20= !"
179 @for /l %%a in (1,1,32) do @if "!url:~-1!" == " " set url=!url:~0,-1!
180 @echo !arch!^|!version!^|!threading!^|!exceptions!^|!revision!^|!url!>> "%repository_path%"
182 @del "%file_path%" 2>nul
183 @endlocal & set "%var%=%repository_path%"
186 :resolve - Gets the MinGW-builds repository
187 :: %1 - The return variable for the MinGW slug
188 :: %2 - The return variable for the MinGW URL
189 :: %3 - The repository information to use
190 :: %4 - Target architecture
191 :: %5 - Version of MinGW to get [optional]
192 :: %6 - Threading model [optional]
193 :: %7 - Exception model [optional]
194 :: %8 - Package revision [optional]
197 @if "%slug_var%" == "" @exit /b 1
199 @if "%url_var%" == "" @exit /b 1
200 @set "repository=%~3"
201 @if "%repository%" == "" @exit /b 1
203 @if "%arch%" == "" @exit /b 1
204 @call :resolve_version version "%repository%" "%arch%" "%~5"
205 @if errorlevel 1 @exit /b 1
206 @call :resolve_threading threading "%repository%" "%arch%" "%version%" "%~6"
207 @if errorlevel 1 @exit /b 1
208 @call :resolve_exceptions exceptions "%repository%" "%arch%" "%version%" "%threading%" "%~7"
209 @if errorlevel 1 @exit /b 1
210 @call :resolve_revision revision "%repository%" "%arch%" "%version%" "%threading%" "%exceptions%" "%~8"
211 @if errorlevel 1 @exit /b 1
212 @call :log 3 "Finding URL"
213 @for /f "delims=| tokens=1-6" %%a in (%repository%) do @(
214 @if "%arch%" == "%%a" (
215 @if "%version%" == "%%b" (
216 @if "%threading%" == "%%c" (
217 @if "%exceptions%" == "%%d" (
218 @if "%revision%" == "%%e" (
222 @call :log 0 "Failed to resolve URL"
225 @set slug=gcc-%version%-%arch%-%threading%-%exceptions%-rev%revision%
226 @call :log 2 "Resolved slug: %slug%"
227 @call :log 2 "Resolved url: %url%"
228 @endlocal & set "%slug_var%=%slug%" & set "%url_var%=%url%"
231 :unpack - Unpacks the MinGW archive
232 :: %1 - The return variable name for the compiler path
233 :: %2 - The filepath or URL of the archive
234 :: %3 - The folder to unpack to
236 @setlocal EnableDelayedExpansion
238 @call :log 0 "Failed to enable extensions"
242 @if "%var%" == "" @exit /b 1
243 @set "archive_path=%~2"
244 @if "%archive_path%" == "" @exit /b 1
245 @set "folder_path=%~3"
246 @if "%folder_path%" == "" @exit /b 1
247 @set "compiler_path=%folder_path%\bin"
248 @if exist "%compiler_path%" goto :unpack_done
250 @call :log 2 "Unpacking MinGW archive"
251 @set "http=%archive_path:~0,4%"
252 @if "%http%" == "http" (
253 @set "url=%archive_path%"
254 @for /f %%a in ("!url: =-!") do @set "file_name=%%~na"
255 @for /f %%a in ("!url: =-!") do @set "file_ext=%%~xa"
256 @set "archive_path=%dependency_path%\!file_name!!file_ext!"
257 @if not exist "!archive_path!" (
259 @call :log 1 "Downloading MinGW archive"
260 @call :download "!url!" "!archive_path!"
262 @del "!archive_path!" 2>nul
263 @call :log 0 "Failed to download: !file_name!!file_ext!"
268 @if not exist "%archive_path%" (
269 @call :log 0 "The archive did not exist to unpack: %archive_path%"
272 @for /f %%a in ("%archive_path: =-%") do @set "file_name=%%~na"
273 @for /f %%a in ("%archive_path: =-%") do @set "file_ext=%%~xa"
275 @call :log 1 "Unpacking MinGW %file_name%%file_ext%"
276 @call :find_sevenzip sevenzip_executable
278 @call :log 0 "Need 7zip to unpack the MinGW archive"
281 @call :iso8601 iso8601
282 @for /f %%a in ("%folder_path%") do @set "tmp_path=%%~dpatmp-%iso8601%"
283 @"%sevenzip_executable%" x -y "-o%tmp_path%" "%archive_path%" > nul
285 @rmdir /s /q "%folder_path%"
286 @call :log 0 "Failed to unpack the MinGW archive"
289 @set "expected_path=%tmp_path%\mingw64"
290 @if not exist "%expected_path%" (
291 @set "expected_path=%tmp_path%\mingw32"
293 @move /y "%expected_path%" "%folder_path%" > nul
295 @rmdir /s /q "%tmp_path%" 2>nul
296 @call :log 0 "Failed to move MinGW folder"
297 @call :log 0 "%expected_path%"
298 @call :log 0 "%folder_path%"
301 @rmdir /s /q %tmp_path%
302 @set "compiler_path=%folder_path%\bin"
304 @if not exist "%compiler_path%\gcc.exe" (
305 @call :log 0 "Failed to find gcc: %compiler_path%"
308 @endlocal & set "%var%=%compiler_path%"
311 :find_sevenzip - Finds (or downloads) the 7zip executable
312 :: %1 - The return variable for the 7zip executable path
315 @if "%var%" == "" @exit /b 1
316 @call :log 2 "Finding 7zip"
317 @call :find_in_path sevenzip_executable 7z.exe
318 @if not errorlevel 1 goto :find_sevenzip_done
319 @call :find_in_path sevenzip_executable 7za.exe
320 @if not errorlevel 1 goto :find_sevenzip_done
321 @set checksum=2FAC454A90AE96021F4FFC607D4C00F8
322 @set "url=http://7-zip.org/a/7za920.zip"
323 @for /f %%a in ("%url: =-%") do @set "file_name=%%~na"
324 @for /f %%a in ("%url: =-%") do @set "file_ext=%%~xa"
325 @set "archive_path=%dependency_path%\%file_name%%file_ext%"
326 @if not exist "%archive_path%" (
328 @call :log 1 "Downloading 7zip archive"
329 @call :download "%url%" "%archive_path%" %checksum%
331 @del "%archive_path%" 2>nul
332 @call :log 0 "Failed to download: %file_name%%file_ext%"
336 @set "sevenzip_path=%dependency_path%\sevenzip"
337 @if not exist "%sevenzip_path%" (
338 @call :unzip "%archive_path%" "%sevenzip_path%"
340 @call :log 0 "Failed to unzip the7zip archive"
344 @set "sevenzip_executable=%sevenzip_path%\7za.exe"
345 @if not exist "%sevenzip_executable%" (
346 @call :log 0 "Failed to find unpacked 7zip: %sevenzip_executable%"
350 @call :log 2 "Found 7zip: %sevenzip_executable%"
351 @endlocal & set "%var%=%sevenzip_executable%"
354 :unzip - Unzips a .zip archive
355 :: %1 - The archive to unzip
356 :: %2 - The location to unzip to
358 @set "archive_path=%~1"
359 @if "%archive_path%" == "" @exit /b 1
360 @set "folder_path=%~2"
361 @if "%folder_path%" == "" @exit /b 1
362 @for /f %%a in ("%archive_path: =-%") do @set "file_name=%%~na"
363 @for /f %%a in ("%archive_path: =-%") do @set "file_ext=%%~xa"
364 @call :log 2 "Unzipping: %file_name%%file_ext%"
365 @call :iso8601 iso8601
366 @set "log_path=%temp%\unzip-%iso8601%-%file_name%.log"
368 Add-Type -assembly "system.io.compression.filesystem"; ^
369 [io.compression.zipfile]::ExtractToDirectory(^
370 '%archive_path%', '%folder_path%') 2>"%log_path%"
372 @call :log 0 "Failed to unzip: %file_name%%file_ext%"
373 @call :log_append "%log_path%"
379 :resolve_version - Gets the version of the MinGW compiler
380 :: %1 - The return variable for the version
381 :: %2 - The repository information to use
382 :: %3 - The architecture of the compiler
383 :: %4 - Version of MinGW to get [optional]
385 @setlocal EnableDelayedExpansion
387 @call :log 0 "Failed to enable extensions"
391 @if "%var%" == "" @exit /b 1
392 @set "repository=%~2"
393 @if "%repository%" == "" @exit /b 1
395 @if "%arch%" == "" @exit /b 1
397 @if not "%version%" == "" goto :resolve_version_done
398 :: Find the latest version
399 @call :log 3 "Finding latest version"
401 @for /f "delims=| tokens=1-6" %%a in (%repository%) do @(
402 @if "%arch%" == "%%a" (
403 @call :version_compare result "%version%" "%%b"
405 @call :log 0 "Failed to compare versions: %version% %%a"
408 @if !result! lss 0 set version=%%b
411 :resolve_version_done
412 @if "%version%" == "" (
413 @call :log 0 "Failed to resolve latest version number"
416 @call :log 2 "Resolved version: %version%"
417 @endlocal & set "%var%=%version%"
420 :resolve_threading - Gets the threading model of the MinGW compiler
421 :: %1 - The return variable for the threading model
422 :: %2 - The repository information to use
423 :: %3 - The architecture of the compiler
424 :: %4 - The version of the compiler
425 :: %5 - threading model of MinGW to use [optional]
427 @setlocal EnableDelayedExpansion
429 @call :log 0 "Failed to enable extensions"
433 @if "%var%" == "" @exit /b 1
434 @set "repository=%~2"
435 @if "%repository%" == "" @exit /b 1
437 @if "%arch%" == "" @exit /b 1
439 @if "%version%" == "" @exit /b 1
441 @if not "%threading%" == "" goto :resolve_threading_done
442 @call :log 3 "Finding best threading model"
443 @for /f "delims=| tokens=1-6" %%a in (%repository%) do @(
444 @if "%arch%" == "%%a" (
445 @if "%version%" == "%%b" (
446 @if not defined threading (
449 @if "%%c" == "posix" (
452 :resolve_threading_done
453 @if "%threading%" == "" (
454 @call :log 0 "Failed to resolve the best threading model"
457 @call :log 2 "Resolved threading model: %threading%"
458 @endlocal & set "%var%=%threading%"
461 :resolve_exceptions - Gets the exception model of the MinGW compiler
462 :: %1 - The return variable for the exception model
463 :: %2 - The repository information to use
464 :: %3 - The architecture of the compiler
465 :: %4 - The version of the compiler
466 :: %4 - The threading model of the compiler
467 :: %5 - exception model of MinGW to use [optional]
469 @setlocal EnableDelayedExpansion
471 @call :log 0 "Failed to enable extensions"
475 @if "%var%" == "" @exit /b 1
476 @set "repository=%~2"
477 @if "%repository%" == "" @exit /b 1
479 @if "%arch%" == "" @exit /b 1
481 @if "%version%" == "" @exit /b 1
483 @if "%threading%" == "" @exit /b 1
484 @set "exceptions=%~6"
485 @if not "%exceptions%" == "" goto :resolve_exceptions_done
486 @call :log 3 "Finding best exception model"
487 @for /f "delims=| tokens=1-6" %%a in (%repository%) do @(
488 @if "%arch%" == "%%a" (
489 @if "%version%" == "%%b" (
490 @if "%threading%" == "%%c" (
491 @if not defined exceptions (
492 @set "exceptions=%%d"
494 @if "%%d" == "dwarf" (
495 @set "exceptions=%%d"
498 @set "exceptions=%%d"
500 :resolve_exceptions_done
501 @if "%exceptions%" == "" (
502 @call :log 0 "Failed to resolve the best exception model"
505 @call :log 2 "Resolved exception model: %exceptions%"
506 @endlocal & set "%var%=%exceptions%"
509 :resolve_revision - Gets the revision of the MinGW compiler
510 :: %1 - The return variable for the revision
511 :: %2 - The repository information to use
512 :: %3 - The architecture of the compiler
513 :: %4 - The version of the compiler
514 :: %4 - The threading model of the compiler
515 :: %4 - The exception model of the compiler
516 :: %5 - revision of the MinGW package to use [optional]
518 @setlocal EnableDelayedExpansion
520 @call :log 0 "Failed to enable extensions"
524 @if "%var%" == "" @exit /b 1
525 @set "repository=%~2"
526 @if "%repository%" == "" @exit /b 1
528 @if "%arch%" == "" @exit /b 1
530 @if "%version%" == "" @exit /b 1
532 @if "%threading%" == "" @exit /b 1
533 @set "exceptions=%~6"
534 @if "%exceptions%" == "" @exit /b 1
536 @if not "%revision%" == "" goto :resolve_revision_done
537 @call :log 3 "Finding latest revision"
538 @for /f "delims=| tokens=1-6" %%a in (%repository%) do @(
539 @if "%arch%" == "%%a" (
540 @if "%version%" == "%%b" (
541 @if "%threading%" == "%%c" (
542 @if "%exceptions%" == "%%d" (
543 @if "%%e" gtr "%revision%" (
546 :resolve_revision_done
547 @if "%revision%" == "" (
548 @call :log 0 "Failed to resolve latest revision"
551 @call :log 2 "Resolved revision: %revision%"
552 @endlocal & set "%var%=%revision%"
555 :version_compare - Compares two semantic version numbers
556 :: %1 - The return variable:
557 :: - < 0 : if %2 < %3
559 :: - > 0 : if %2 > %3
560 :: %2 - The first version to compare
561 :: %3 - The second version to compare
564 @if "%var%" == "" @exit /b 1
566 @if "%lhs%" == "" @exit /b 1
568 @if "%lhs%" == "" @exit /b 1
570 @for /f "delims=. tokens=1-6" %%a in ("%lhs%.%rhs%") do @(
573 goto :version_compare_done
577 goto :version_compare_done
581 goto :version_compare_done
585 goto :version_compare_done
589 goto :version_compare_done
593 goto :version_compare_done
601 :version_compare_done
602 @endlocal & set "%var%=%result%"
605 :print_usage - Prints the usage of the script
606 :: %* - message to print, each argument on it's own line
608 @for %%a in (%*) do @echo.%%~a
610 @echo.build [/?][/v[v...]^|/q][/version][/arch a][/threading t]
611 @echo. [/exceptions e][/revision r] location
613 @echo. /version v The version of MinGW to download
614 @echo. /arch a The target architecture [i686^|x86_64]
616 @echo. Threading model to use [posix^|win32]
618 @echo. Exception model to use [sjlj^|seh^|dwarf]
619 @echo. /revision e Revision of the release to use
620 @echo. /v Sets the output to be more verbose
621 @echo. /v[v...] Extra verbosity, /vv, /vvv, etc
622 @echo. /q Quiets the output
623 @echo. /? Shows this usage message
628 :script_source - Determines if the script was ran from the cli or explorer
629 :: %1 - The return variable [cli|explorer]
631 @setlocal EnableDelayedExpansion
633 @call :log 0 "Failed to enable extensions"
636 @call :log 3 "Attempting to detect the script source"
637 @echo "The invocation command was: '%cmdcmdline%'" >> %log_path%
638 @for /f "tokens=1-3,*" %%a in ("%cmdcmdline%") do @(
645 @if "!arg2:~0,1!" equ "!quote!" (
646 if "!arg2:~-1!" neq "!quote!" (
650 @call :log 4 "cmd = %cmd%"
651 @call :log 4 "arg1 = %arg1%"
652 @call :log 4 "arg2 = %arg2%"
653 @call :log 4 "rest = %rest%"
654 @call :log 4 "src = %~f0"
655 @if /i "%arg2%" == "call" (
656 set script_source=cli
658 @if /i "%arg1%" == "/c" (
659 set script_source=explorer
661 set script_source=cli
664 @call :log 3 "The script was invoked from %script_source%"
665 @endlocal & set "%~1=%script_source%"
668 :architecture - Finds the system architecture
669 :: %1 - The return variable [i686|x86_64]
671 @call :log 3 "Determining the processor architecture"
672 @set "key=HKLM\System\CurrentControlSet\Control\Session Manager\Environment"
673 @set "var=PROCESSOR_ARCHITECTURE"
674 @for /f "skip=2 tokens=2,*" %%a in ('reg query "%key%" /v "%var%"') do @set "arch=%%b"
675 @if "%arch%" == "AMD64" set arch=x86_64
676 @if "%arch%" == "x64" set arch=i686
677 @call :log 4 "arch = %arch%"
678 @endlocal & set "%~1=%arch%"
681 :md5 - Gets the MD5 checksum for a file
683 :: %2 - The file path
687 @if "%var%" == "" @exit /b 1
688 @if "%file_path%" == "" @exit /b 1
689 @if not exist "%file_path%" @exit /b 1
690 @for /f "skip=3 tokens=1,*" %%a in ('powershell Get-FileHash -Algorithm MD5 "'%file_path%'"') do @set hash=%%b
691 @if not defined hash (
693 @call :log 0 "Failed to get MD5 hash for %file_path%"
696 @endlocal & set "%var%=%hash: =%"
699 :windows_version - Checks the windows version
700 :: %1 - The windows version
701 :: %2 - The major version number return variable
702 :: %3 - The minor version number return variable
703 :: %4 - The revision version number return variable
705 @call :log 3 "Retrieving the Windows version"
706 @for /f "tokens=2 delims=[]" %%x in ('ver') do @set win_ver=%%x
707 @set win_ver=%win_ver:Version =%
708 @set win_ver_major=%win_ver:~0,1%
709 @set win_ver_minor=%win_ver:~2,1%
710 @set win_ver_rev=%win_ver:~4%
711 @call :log 4 "win_ver = %win_ver%"
712 @endlocal & set "%~1=%win_ver%" ^
713 & set "%~2=%win_ver_major%" ^
714 & set "%~3=%win_ver_minor%" ^
715 & set "%~4=%win_ver_rev%"
718 :find_in_path - Finds a program of file in the PATH
719 :: %1 - return variable of the file path
722 @if "%var%" == "" @exit /b 1
724 @if "%file%" == "" @exit /b 1
725 @call :log 3 "Searching PATH for %file%"
726 @for %%x in ("%file%") do @set "file_path=%%~f$PATH:x"
727 @if not defined file_path @exit /b 1
728 @endlocal & set "%var%=%file_path%"
731 :log_append - Appends another file into the current logging file
732 :: %1 - the file_path to the file to concatenate
735 @if "%file_path%" == "" @exit /b 1
736 @call :log 3 "Appending to log: %file_path%"
737 @call :iso8601 iso8601
738 @set temp_log=%temp%\append-%iso8601%.log
739 @call :log 4 "Using temp file %temp_log%"
740 @type "%log_path%" "%file_path%" > "%temp_log%" 2>nul
741 @move /y "%temp_log%" "%log_path%" 1>nul
742 @del "%file_path% 2>nul
743 @del "%temp_log% 2>nul
747 :iso8601 - Returns the current time in ISO8601 format
748 :: %1 - the return variable
749 :: %2 - format [extended|basic*]
750 :: iso8601 - contains the resulting timestamp
752 @wmic Alias /? >NUL 2>&1 || @exit /b 1
754 @if "%var%" == "" @exit /b 1
756 @if "%format%" == "" set format=basic
757 @for /F "skip=1 tokens=1-6" %%g IN ('wmic Path Win32_UTCTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') do @(
758 @if "%%~l"=="" goto :iso8601_done
770 @set minute=%minute:~-2%
771 @set seconds=%seconds:~-2%
772 @if /i [%format%] == [extended] (
773 set iso8601=%yyyy%-%mm%-%dd%T%hour%:%minute%:%seconds%Z
775 if /i [%format%] == [basic] (
776 set iso8601=%yyyy%%mm%%dd%T%hour%%minute%%seconds%Z
781 @set iso8601=%iso8601: =0%
782 @endlocal & set %var%=%iso8601%
785 :verbosity - Processes the verbosity parameter '/v[v...]
786 :: %1 - verbosity given on the command line
787 :: logging_level - set to the number of v's
792 @set verbosity=%verbosity:~1%
793 @if not [%verbosity%] == [] @(
794 set /a "logging_level=logging_level+1"
797 @endlocal & set logging_level=%logging_level%
800 :log - Logs a message, depending on verbosity
802 :: [0-4] for CLI logging
803 :: [5-9] for GUI logging
804 :: %2 - message to print
808 @if "%log_folder%" == "" (
809 echo Logging was used to early in the script, log_folder isn't set yet
812 @if "%log_path%" == "" (
813 echo Logging was used to early in the script, log_path isn't set yet
816 @if not exist "%log_folder%" mkdir "%log_folder%"
817 @if not exist "%log_path%" echo. 1>nul 2>"%log_path%"
818 @echo.%msg% >> "%log_path%"
820 @if [%script_source%] == [explorer] (
821 set /a "level=level-5"
826 @if "%logging_level%" == "" (
827 echo Logging was used to early in the script, logging_level isn't set yet
830 @if %logging_level% geq %level% echo.%msg% 1>&2
834 :download - Downloads a file from the internet
835 :: %1 - the url of the file to download
836 :: %2 - the file to download to
837 :: %3 - the MD5 checksum of the file (optional)
838 @setlocal EnableDelayedExpansion
840 @call :print_usage "Failed to enable extensions"
846 @for %%a in (%file_path%) do @set dir_path=%%~dpa
847 @for %%a in (%file_path%) do @set file_name=%%~nxa
848 @if "%url%" == "" @exit /b 1
849 @if "%file_path%" == "" @exit /b 1
850 @if "%dir_path%" == "" @exit /b 1
851 @if "%file_name%" == "" @exit /b 1
852 @if not exist "%dir_path%" mkdir "%dir_path%"
853 @call :log 2 "Downloading %url%"
854 @call :iso8601 iso8601
855 @set "temp_path=%temp%\download-%iso8601%-%file_name%"
856 @set "log_path=%temp%\download-%iso8601%-log-%file_name%"
857 @call :log 4 "Using temp file %temp_path%"
858 @powershell Invoke-WebRequest "'%url%'" ^
859 -OutFile "'%temp_path%'" ^
860 -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::IE ^
863 @call :log 0 "Failed to download %url%"
864 @call :log_append "%log_path%"
867 @if [%checksum%] neq [] (
868 @call :log 4 "Checking %checksum% against %temp_path%"
869 @call :md5 hash "%temp_path%"
870 if "!hash!" neq "%checksum%" (
871 @call :log 0 "Failed to match checksum: %temp_path%"
872 @call :log 0 "Hash : !hash!"
873 @call :log 0 "Checksum: %checksum%"
876 @call :log 3 "Checksum matched: %temp_path%"
877 @call :log 3 "Hash : !hash!"
878 @call :log 3 "Checksum: %checksum%"
881 @call :log 4 "Renaming %temp_path% to %file_path%"
882 @move /y "%temp_path%" "%file_path%" 1>nul