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