better logic
This commit is contained in:
@ -20,6 +20,7 @@ Optional arguments:
|
|||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ARCHIVE=""
|
ARCHIVE=""
|
||||||
BINARY=""
|
BINARY=""
|
||||||
POSITIONAL_ARGS=()
|
POSITIONAL_ARGS=()
|
||||||
@ -67,66 +68,75 @@ URL="${POSITIONAL_ARGS[0]}"
|
|||||||
#echo "binary: $BINARY"
|
#echo "binary: $BINARY"
|
||||||
|
|
||||||
|
|
||||||
function ensure_binary() {
|
function download() {
|
||||||
local url=$1
|
local url=$1
|
||||||
local archive=$2
|
local archive=$2
|
||||||
local binary=$3
|
local binary=$3
|
||||||
|
|
||||||
if [ -f "$binary" ]; then
|
if check_meta "$url" "$archive"; then
|
||||||
echo "File $binary exists already"
|
echo "Skipping download $url"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
update "$url" "$archive"
|
echo "Downloading $url ..."
|
||||||
|
rm "$archive" "$binary" || true
|
||||||
|
curl --location --silent --show-error --remote-time --output "$archive" "$url"
|
||||||
tar xzf "$archive"
|
tar xzf "$archive"
|
||||||
}
|
}
|
||||||
|
|
||||||
function update() {
|
|
||||||
local url=$1
|
|
||||||
local archive=$2
|
|
||||||
|
|
||||||
if ! check_file_sizes "$url" "$archive"; then
|
function check_meta() {
|
||||||
echo "Downloading $url (if remote is newer) ..."
|
|
||||||
curl --location --remote-time --time-cond "$archive" --output "$archive" "$url"
|
|
||||||
else
|
|
||||||
echo "Skipped download $url"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
function check_file_sizes() {
|
|
||||||
local url=$1
|
local url=$1
|
||||||
local fn=$2
|
local fn=$2
|
||||||
local local_size remote_size
|
|
||||||
|
local local_meta remote_meta
|
||||||
|
|
||||||
if [ ! -f "$fn" ]; then
|
if [ ! -f "$fn" ]; then
|
||||||
echo "File $fn not found"
|
echo "File $fn not found"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local_size=$(get_local_file_size "$fn")
|
local_meta=$(get_local_meta "$fn")
|
||||||
remote_size=$(get_remote_file_size "$url")
|
remote_meta=$(get_remote_meta "$url")
|
||||||
|
|
||||||
if [ "$remote_size" -ne "$local_size" ]; then
|
if [ "$remote_meta" != "$local_meta" ]; then
|
||||||
echo "Remote and local size of file $fn differs (remote: $remote_size, local: $local_size)"
|
echo "Remote and local meta data of file $fn differ (remote: $remote_meta, local: $local_meta)"
|
||||||
return 1
|
return 1
|
||||||
|
else
|
||||||
|
echo "Remote and local meta data of file $fn are identical ($remote_meta)"
|
||||||
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Remote and local size of file $fn is identical"
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_local_file_size() {
|
|
||||||
|
function get_local_meta() {
|
||||||
local fn=$1
|
local fn=$1
|
||||||
stat --format="%s" "$fn"
|
stat --format "s:%s|t:%Y" "$fn"
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_remote_file_size() {
|
function get_remote_meta() {
|
||||||
local url=$1
|
local url=$1
|
||||||
curl --location --head --silent "$url" | grep -i "content-length" | tail -n 1 | cut -d ' ' -f 2 | tr -d '\r'
|
header=$(get_header "$url")
|
||||||
|
size=$(extract_from "$header" "content-length")
|
||||||
|
time=$(extract_from "$header" "last-modified")
|
||||||
|
time=$(date --date "$time" +%s)
|
||||||
|
echo "s:$size|t:$time"
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_header() {
|
||||||
|
local url=$1
|
||||||
|
curl --head --location --silent --show-error "$url"
|
||||||
|
}
|
||||||
|
|
||||||
|
function extract_from() {
|
||||||
|
local header=$1
|
||||||
|
local entry=$2
|
||||||
|
echo "$header" | grep "^${entry}:" | tail -n 1 | cut -d ' ' -f 2- | tr -d "\r"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ensure_binary "$URL" "$ARCHIVE" "$BINARY"
|
|
||||||
|
download "$URL" "$ARCHIVE" "$BINARY"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user