2019-02-26 21:29:01 +01:00
|
|
|
termux_download() {
|
|
|
|
if [ $# != 3 ]; then
|
|
|
|
termux_error_exit "termux_download(): Invalid arguments - expected \$URL \$DESTINATION \$CHECKSUM"
|
|
|
|
fi
|
|
|
|
local URL="$1"
|
|
|
|
local DESTINATION="$2"
|
|
|
|
local CHECKSUM="$3"
|
|
|
|
|
|
|
|
if [ -f "$DESTINATION" ] && [ "$CHECKSUM" != "SKIP_CHECKSUM" ]; then
|
|
|
|
# Keep existing file if checksum matches.
|
|
|
|
local EXISTING_CHECKSUM
|
|
|
|
EXISTING_CHECKSUM=$(sha256sum "$DESTINATION" | cut -f 1 -d ' ')
|
|
|
|
if [ "$EXISTING_CHECKSUM" = "$CHECKSUM" ]; then return; fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
local TMPFILE
|
2020-10-22 21:45:33 +02:00
|
|
|
TMPFILE=$(mktemp "$TERMUX_PKG_TMPDIR/download.${TERMUX_PKG_NAME-unnamed}.XXXXXXXXX")
|
2019-02-26 21:29:01 +01:00
|
|
|
echo "Downloading ${URL}"
|
2020-07-27 23:20:33 +02:00
|
|
|
if curl --fail --retry 20 --retry-connrefused --retry-delay 30 --location --output "$TMPFILE" "$URL"; then
|
2020-07-24 14:14:50 +02:00
|
|
|
local ACTUAL_CHECKSUM
|
|
|
|
ACTUAL_CHECKSUM=$(sha256sum "$TMPFILE" | cut -f 1 -d ' ')
|
|
|
|
if [ "$CHECKSUM" != "SKIP_CHECKSUM" ]; then
|
|
|
|
if [ "$CHECKSUM" != "$ACTUAL_CHECKSUM" ]; then
|
|
|
|
>&2 printf "Wrong checksum for %s:\nExpected: %s\nActual: %s\n" \
|
|
|
|
"$URL" "$CHECKSUM" "$ACTUAL_CHECKSUM"
|
2020-08-21 14:39:06 +02:00
|
|
|
return 1
|
2019-02-26 21:29:01 +01:00
|
|
|
fi
|
|
|
|
else
|
2020-07-24 14:14:50 +02:00
|
|
|
printf "WARNING: No checksum check for %s:\nActual: %s\n" \
|
|
|
|
"$URL" "$ACTUAL_CHECKSUM"
|
2019-02-26 21:29:01 +01:00
|
|
|
fi
|
2020-07-24 14:14:50 +02:00
|
|
|
mv "$TMPFILE" "$DESTINATION"
|
2020-08-21 14:39:06 +02:00
|
|
|
return 0
|
2020-07-24 14:14:50 +02:00
|
|
|
fi
|
2019-02-26 21:29:01 +01:00
|
|
|
|
2020-08-21 14:39:06 +02:00
|
|
|
echo "Failed to download $URL" >&2
|
|
|
|
return 1
|
2019-02-26 21:29:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Make script standalone executable as well as sourceable
|
|
|
|
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
|
|
|
termux_download "$@"
|
|
|
|
fi
|