-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.sh
executable file
·58 lines (49 loc) · 1012 Bytes
/
fetch.sh
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
57
58
#!/bin/sh
# print help
help() {
echo "Usage: $0 <url> <destination>"
}
# help option
if [ "-h" == "$1" ] || [ "--help" == "$1" ]; then
help
exit 0
fi
# check for sufficient arguments
if [[ $# < 2 ]]; then
echo "Error: not enough arguments."
help
exit 1
fi
# check if already downloaded
if [ -e "$2" ]; then
echo "Cached $2"
exit 0
fi
# autodetect download tool
if [ -z "$DLCMD" ]; then
DLCMD=$(which wget) ||
DLCMD=$(which curl) ||
(echo 'Could not detect wget or curl.'; exit 1)
fi
# detect whether downloader is wget or curl
DLSTYLE=dlcurl
case $(basename "$(echo $DLCMD | awk '{print $NF}')") in
wget)
DLSTYLE=dlwget
;;
curl)
DLSTYLE=dlcurl
;;
esac
# download with curl
dlcurl() {
curl $CURLFLAGS "$1" > "$2" ||
(echo 'Download failed.'; exit 2)
}
# download with wget
dlwget() {
wget $WGETFLAGS "$1" -O "$2" ||
(echo 'Download failed.'; exit 2)
}
# Download file.
$DLSTYLE "$1" "$2"