Files
graphene/Scripts/download
Jörg Thalheim 82caa0683d [Scripts] Make bash shebangs portable
Unlike /usr/bin/env which is a Posix standard, there is no guarentee
that /bin/bash exists. This is the case for operating systems such
as FreeBSD, NixOS and Guix. By using /usr/bin/env we also give
the user the option to provide their own bash in a different path
by setting the $PATH environemnt variable.
Distributions usually provide their own packaging wrappers
to fixup shebangs upon installation, however those are not convienent
to use when developing in the source tree.

See also other upstream discussions about the topic:

- https://github.com/systemd/systemd/pull/5816
2020-06-24 19:51:55 +02:00

79 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -eu -o pipefail
declare -a urls
usage() {
echo "Usage: download --url https://example.org/test --url https://mirror.example/blah --output test.tar --sha256 1234..abc"
exit "${1:-0}"
}
while [ $# -gt 0 ]; do
case "$1" in
--url)
urls+=("$2")
shift
;;
--output)
output="$2"
shift
;;
--sha256)
sha256="$2"
shift
;;
--help|-h)
usage
;;
*)
usage 1
;;
esac
shift
done
if [ -z "${output:-}" ] || [ -z "${sha256:-}" ] || [ -z "${urls[*]}" ]; then
usage 1
fi
if [ -n "${DL_CACHE:-}" ]; then
if [ -f "$DL_CACHE/$sha256" ]; then
echo "download: Found '$output' (${sha256:0:8}...) in cache."
cp "$DL_CACHE/$sha256" "$output"
exit 0
fi
fi
if [ "${DL_OFFLINE:-}" == "true" ]; then
echo "download: ERROR: File '$output' (${sha256:0:8}...) not found in cache and offline mode is active!"
exit 1
fi
tmpd="$(mktemp -d -p "${DL_CACHE:-.}")"
cleanup() {
rm -rf "$tmpd"
}
trap cleanup EXIT
for url in "${urls[@]}"; do
echo "download: Trying to fetch $url"
wget --timeout=10 -O "$tmpd/unverified" "$url" || true
sha256_received="$(sha256sum "$tmpd/unverified" | cut -d ' ' -f 1)"
if [ "$sha256" != "$sha256_received" ]; then
echo "download: WARNING: Hash mismatch: Expected $sha256 but received $sha256_received"
continue
fi
echo "download: Fetched '$output' (${sha256:0:8}...) successfully."
if [ -n "${DL_CACHE:-}" ]; then
mv "$tmpd/unverified" "$DL_CACHE/$sha256"
cp "$DL_CACHE/$sha256" "$output"
exit 0
fi
mv "$tmpd/unverified" "$output"
exit 0
done
echo "download: ERROR: Failed to download '$output' (${sha256:0:8}...)! No URLs left to try."
exit 1