11#! /bin/bash
2- VERSION=4.5 .0
2+ VERSION=4.6 .0
33printDownloaderHelp () {
44 cat << EOF
55
@@ -25,27 +25,24 @@ printDownloaderHelp() {
2525
2626EOF
2727}
28-
28+ VERBOSE=0
2929validate_url () {
3030 if [[ $1 =~ ^https? ://.+ ]]; then
3131 return 0
3232 else
3333 return 1
3434 fi
3535}
36-
3736convert_bytes_to_mb () {
3837 bytes=$1
3938 mb=$(( bytes / 1024 / 1024 ))
4039 echo " ${mb} MB"
4140}
42-
4341CHECK_RESULT=0
4442MAX_REDIRECTS=20
4543RETRY_MAX=20
4644RETRY_DELAY_S=1
4745EXTRA_ARGS=" "
48-
4946# ansi console escape codes
5047CON_DEFAULT=" \033[0m"
5148CON_WHITE=" \033[37m"
@@ -55,7 +52,6 @@ CON_RED="\033[31m"
5552CON_YELLOW=" \033[33m"
5653CON_YELLOW_GREEN=" \033[33m"
5754CON_GREEN=" \033[32m"
58-
5955echoError () {
6056 echo -e " $CON_BOLD$CON_RED $1 $CON_DEFAULT "
6157}
@@ -70,15 +66,21 @@ check_remote_vs_local() {
7066 LOCAL_FILE=" $1 "
7167 REMOTE_URL=" $2 "
7268 USE_WGET2=" $3 "
73- echo " [downloader] check if local == remote: [$REMOTE_URL ]"
69+ if [[ $VERBOSE == 1 ]]; then
70+ echo " [downloader] check if local == remote: [$REMOTE_URL ]"
71+ fi
7472 if [ ! -f " $LOCAL_FILE " ]; then
75- echo " [downloader] No download cache"
76- echo " [downloader] Proceeding with download. "
73+ if [[ $VERBOSE == 1 ]]; then
74+ echo " [downloader] No download cache"
75+ fi
76+ echo " [downloader] Downloading ..."
7777 CHECK_RESULT=0
7878 return
7979 else
80- echo " [downloader] Found download cache."
81- echo " [cache] [$LOCAL_FILE ]"
80+ if [[ $VERBOSE == 1 ]]; then
81+ echo " [downloader] Found download cache."
82+ echo " [cache] [$LOCAL_FILE ]"
83+ fi
8284 fi
8385 REMOTE_URL=$( echo " $REMOTE_URL " | sed ' s/[[:space:]]*$//' )
8486 LocalSize=$( wc -c < " $LOCAL_FILE " | tr -d ' [:space:]' )
@@ -90,7 +92,9 @@ check_remote_vs_local() {
9092 LocalSizeMB=$( convert_bytes_to_mb $LocalSize )
9193 RemoteSizeMB=$( convert_bytes_to_mb $RemoteSize )
9294
93- echo " [downloader] Remote size:[${RemoteSizeMB} ] | Local size:[${LocalSizeMB} ]"
95+ if [[ $VERBOSE == 1 ]]; then
96+ echo " [downloader] Remote size:[${RemoteSizeMB} ] | Local size:[${LocalSizeMB} ]"
97+ fi
9498 if [ -z " $modified " ]; then
9599 echo " [downloader] failed to retrieve last-modified header from remote [" $REMOTE_URL " ] ... Proceeding with download"
96100 CHECK_RESULT=0
@@ -104,7 +108,7 @@ check_remote_vs_local() {
104108 fi
105109 if [ $? -ne 0 ]; then
106110 echo " [downloader] Error in converting Remote modification time [report this openFrameworks devs]."
107- echo " [downloader] ... Proceeding with download "
111+ echo " [downloader] Downloading ... [ $REMOTE_URL ] "
108112 CHECK_RESULT=0
109113 rm -f $LOCAL_FILE
110114 return
@@ -119,28 +123,30 @@ check_remote_vs_local() {
119123 fi
120124 if [ $? -ne 0 ]; then
121125 echo " [downloader] Error in converting Local modification time [report this openFrameworks devs]."
122- echo " [downloader] ... Proceeding with download "
126+ echo " [downloader] Downloading: [ $REMOTE_URL ] "
123127 CHECK_RESULT=0
124128 rm -f $LOCAL_FILE
125129 return
126130 fi
127131 if [ " $LocalSize " != " $RemoteSize " ]; then
128- echo " [downloader] Remote size bits:[${RemoteSize} ] | Local size bits:[${LocalSize} ]"
129- echo " [downloader] File sizes differ between remote and local file."
130- echo " [downloader] ... Proceeding with download"
132+ if [[ $VERBOSE == 1 ]]; then
133+ echo " [downloader] Remote size bits:[${RemoteSize} ] | Local size bits:[${LocalSize} ]"
134+ fi
135+ echo " [downloader] File sizes differ between remote and local file. Downloading: [$REMOTE_URL ]"
131136 CHECK_RESULT=0
132137 rm -f $LOCAL_FILE
133138 return
134139 fi
135140 if [ " $local_ctime " -lt " $remote_ctime " ]; then
136- echo " [downloader] Remote modification Time:[${remote_ctime} ] | Local modification Time:[${local_ctime} ]"
137- echo " [downloader] Remote file is newer."
138- echo " [downloader] ... Proceeding with download"
141+ if [[ $VERBOSE == 1 ]]; then
142+ echo " [downloader] Remote modification Time:[${remote_ctime} ] | Local modification Time:[${local_ctime} ]"
143+ fi
144+ echo " [downloader] Remote file is newer. Downloading: [$REMOTE_URL ]"
139145 CHECK_RESULT=0
140146 rm -f $LOCAL_FILE
141147 return
142148 fi
143- echo " [downloader] No need to download again. Every bit matters. "
149+ echo " [downloader] Cached. No need to download again: [ $REMOTE_URL ] "
144150 CHECK_RESULT=1
145151 return
146152}
@@ -311,7 +317,9 @@ downloader() {
311317 fi
312318 # [options]
313319 if [[ " $COMPRESSION " == " 1" ]]; then
314- echo " [downloader] enabled brotli/zlib losslesss compression response"
320+ if [[ $VERBOSE == 1 ]]; then
321+ echo " [downloader] enabled brotli/zlib losslesss compression response"
322+ fi
315323 elif [[ " $COMPRESSION " == " 0" ]] && [[ $CURL == 1 && $CURL_INSTALLED == 1 ]] && [[ $WGET2 == 0 || $WGET2_INSTALLED == 0 ]]; then
316324 EXTRA_ARGS+=" -Z "
317325 fi
@@ -389,7 +397,9 @@ downloader() {
389397 URLS_TO_DOWNLOAD=$( echo " $URLS_TO_DOWNLOAD " | sed ' s/[[:space:]]*$//' )
390398 echo
391399 if [ -z " $URLS_TO_DOWNLOAD " ]; then
392- echo " [downloader] No URLS to download, continue..."
400+ if [[ $VERBOSE == 1 ]]; then
401+ echo " [downloader] No URLS to download, continue..."
402+ fi
393403 else
394404 if [[ " ${SILENT} " == 1 ]]; then
395405 if [[ $WGET2 == 1 ]] && [[ $WGET2_INSTALLED == 1 ]]; then
@@ -407,16 +417,22 @@ downloader() {
407417 fi
408418 else
409419 if [[ $WGET2 == 1 ]] && [[ $WGET2_INSTALLED == 1 ]]; then
410- echo " [downloader] [wget2] urls:[$URLS_TO_DOWNLOAD ] args:[$EXTRA_ARGS $FINAL_EXTRA_ARGS "
420+ if [[ $VERBOSE == 1 ]]; then
421+ echo " [downloader] [wget2] urls:[$URLS_TO_DOWNLOAD ] args:[$EXTRA_ARGS $FINAL_EXTRA_ARGS "
422+ fi
411423 echo " "
412424 wget2 -N -c --no-tcp-fastopen --progress=bar --force-progress --tries=${RETRY_MAX} --max-redirect=${MAX_REDIRECTS} --retry-connrefused --waitretry=${RETRY_DELAY_S} --timeout=1500 ${EXTRA_ARGS} ${FINAL_EXTRA_ARGS} ${URLS_TO_DOWNLOAD}
413425 elif [[ $CURL == 1 ]] && [[ $CURL_INSTALLED == 1 ]]; then
414- echo " [downloader] [cURL] urls:[$URLS_TO_DOWNLOAD ] args:[$EXTRA_ARGS $FINAL_EXTRA_ARGS ${CONNECTION_EXTRA_ARGS[@]} ]"
426+ if [[ $VERBOSE == 1 ]]; then
427+ echo " [downloader] [cURL] urls:[$URLS_TO_DOWNLOAD ] args:[$EXTRA_ARGS $FINAL_EXTRA_ARGS ${CONNECTION_EXTRA_ARGS[@]} ]"
428+ fi
415429 echo
416430 curl -Z -L --retry ${RETRY_MAX} --retry-delay ${RETRY_DELAY_S} --max-redirs ${MAX_REDIRECTS} --progress-bar --header " Connection: close" ${EXTRA_ARGS} ${FINAL_EXTRA_ARGS} ${URLS_TO_DOWNLOAD}
417431
418432 elif [[ $WGET == 1 ]] && [[ $WGET_INSTALLED == 1 ]]; then
419- echo " [downloader] [wget] [$FILENAME ] urls:[$URLS_TO_DOWNLOAD ] args:[$EXTRA_ARGS $FINAL_EXTRA_ARGS ]"
433+ if [[ $VERBOSE == 1 ]]; then
434+ echo " [downloader] [wget] [$FILENAME ] urls:[$URLS_TO_DOWNLOAD ] args:[$EXTRA_ARGS $FINAL_EXTRA_ARGS ]"
435+ fi
420436 echo
421437 wget -nv --progress=bar -N --tries=${RETRY_MAX} --retry-connrefused --waitretry=${RETRY_DELAY_S} ${EXTRA_ARGS} ${FINAL_EXTRA_ARGS} ${URLS_TO_DOWNLOAD}
422438 else
@@ -427,11 +443,15 @@ downloader() {
427443 if [[ " $CLOSE_CONNECTION " == " 1" ]]; then
428444 if [[ $CURL == 1 ]] && [[ $CURL_INSTALLED == 1 ]] || [[ $CURL == 1 && $WGET2 == 1 ]]; then
429445 if [[ ${# FORWARDED_URLS[@]} -eq 0 ]]; then
430- echo " [downloader] No active connections to close"
446+ if [[ $VERBOSE == 1 ]]; then
447+ echo " [downloader] No active connections to close"
448+ fi
431449 else
432450 FIRST_URL=" ${FORWARDED_URLS[0]} "
433451 FIRST_URL=$( echo " $FIRST_URL " | sed ' s/[[:space:]]*$//' )
434- echo " [downloader] Closing the ports yarr url:[$FIRST_URL ]"
452+ if [[ $VERBOSE == 1 ]]; then
453+ echo " [downloader] Closing the ports yarr url:[$FIRST_URL ]"
454+ fi
435455 curl -I -L --retry-connrefused --insecure --silent --head --max-time 1 --retry ${RETRY_MAX} ${CLOSE_EXTRA_ARGS} --no-keepalive --header " Connection: close" --retry-delay ${RETRY_DELAY_S} --max-redirs ${MAX_REDIRECTS} ${FIRST_URL}
436456 fi
437457 fi
0 commit comments