aboutsummaryrefslogtreecommitdiff
path: root/scripts/download-archive
blob: c4aa965dafdbee7d08dccc6aa62e13c75f765401 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/sh

set -e
set -u

fatal () {
    echo "$@" >&2
    exit 1
}

archive=''
download=''
mirrors=''

while getopts m:a:d: opt; do
    case $opt in
        m) mirrors="$mirrors $OPTARG";;
        a) archive="$OPTARG";;
        d) download="$OPTARG";;
        *) fatal "Unknown option: $opt";;
    esac
done
shift `expr $OPTIND - 1`


wget=`type -p wget || true`
curl=`type -p curl || true`

download_wget () {
    (set -x; $wget -O "$1" "$2") || rm -f "$1"
}

download_curl () {
    (set -x $curl -L -v "$2" > "$1") || rm -f "$1"
}


download_tool=''
if [ -n "$wget" ]; then
    download_tool=download_wget
elif [ -n "$curl" ]; then
    download_tool=download_curl
else
    fatal "Don't know how to download"
fi

for m in $mirrors; do
    $download_tool "$archive" "$m/$archive"
    [ ! -e "$archive" ] || break;
done

[ -e "$archive" ] || $download_tool "$archive" "$download"
[ -e "$archive" ] || fatal "Failed to get \`$archive'"

exit 0