xref: /OK3568_Linux_fs/buildroot/support/download/wget (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1#!/usr/bin/env bash
2
3# We want to catch any unexpected failure, and exit immediately
4set -e
5
6# Download helper for wget, to be called from the download wrapper script
7#
8# Options:
9#   -q          Be quiet.
10#   -o FILE     Save into file FILE.
11#   -f FILENAME The filename of the tarball to get at URL
12#   -u URL      Download file at URL.
13#   -e ENCODE   Tell wget to urlencode the filename passed to it
14#
15# Environment:
16#   WGET     : the wget command to call
17
18quiet=
19while getopts "${BR_BACKEND_DL_GETOPTS}" OPT; do
20    case "${OPT}" in
21    q)  quiet=-q;;
22    o)  output="${OPTARG}";;
23    f)  filename="${OPTARG}";;
24    u)  url="${OPTARG}";;
25    e)  encode="-e";;
26    :)  printf "option '%s' expects a mandatory argument\n" "${OPTARG}"; exit 1;;
27    \?) printf "unknown option '%s'\n" "${OPTARG}" >&2; exit 1;;
28    esac
29done
30
31shift $((OPTIND-1)) # Get rid of our options
32
33# Caller needs to single-quote its arguments to prevent them from
34# being expanded a second time (in case there are spaces in them)
35_wget() {
36    if [ -z "${quiet}" ]; then
37        printf '%s ' ${WGET} "${@}"; printf '\n'
38    fi
39    _plain_wget "$@"
40}
41# Note: please keep command below aligned with what is printed above
42_plain_wget() {
43    eval ${WGET} "${@}"
44}
45
46# Replace every '?' with '%3F' in the filename; only for the PRIMARY and BACKUP
47# mirror
48[ -n "${encode}" ] && filename=${filename//\?/%3F}
49
50_wget ${quiet} "${@}" -O "'${output}'" "'${url}/${filename}'"
51