better logic

This commit is contained in:
2025-04-16 10:42:07 +02:00
parent 7b176a4e3c
commit 14ecb383c2

View File

@ -20,6 +20,7 @@ Optional arguments:
EOF
}
ARCHIVE=""
BINARY=""
POSITIONAL_ARGS=()
@ -67,66 +68,75 @@ URL="${POSITIONAL_ARGS[0]}"
#echo "binary: $BINARY"
function ensure_binary() {
function download() {
local url=$1
local archive=$2
local binary=$3
if [ -f "$binary" ]; then
echo "File $binary exists already"
if check_meta "$url" "$archive"; then
echo "Skipping download $url"
return
fi
update "$url" "$archive"
echo "Downloading $url ..."
rm "$archive" "$binary" || true
curl --location --silent --show-error --remote-time --output "$archive" "$url"
tar xzf "$archive"
}
function update() {
local url=$1
local archive=$2
if ! check_file_sizes "$url" "$archive"; then
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() {
function check_meta() {
local url=$1
local fn=$2
local local_size remote_size
local local_meta remote_meta
if [ ! -f "$fn" ]; then
echo "File $fn not found"
return 1
fi
local_size=$(get_local_file_size "$fn")
remote_size=$(get_remote_file_size "$url")
local_meta=$(get_local_meta "$fn")
remote_meta=$(get_remote_meta "$url")
if [ "$remote_size" -ne "$local_size" ]; then
echo "Remote and local size of file $fn differs (remote: $remote_size, local: $local_size)"
if [ "$remote_meta" != "$local_meta" ]; then
echo "Remote and local meta data of file $fn differ (remote: $remote_meta, local: $local_meta)"
return 1
else
echo "Remote and local meta data of file $fn are identical ($remote_meta)"
return 0
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
stat --format="%s" "$fn"
stat --format "s:%s|t:%Y" "$fn"
}
function get_remote_file_size() {
function get_remote_meta() {
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"