-
Notifications
You must be signed in to change notification settings - Fork 254
/
fetchurl
executable file
·125 lines (103 loc) · 2.18 KB
/
fetchurl
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/sh
#
# Small utility to fetch and unpack archives on the web (with cache)
#
# Depends on : curl, tar
#
set -e
set +u
# ENV vars, inherited from external
CACHE=${CACHE:-1}
UNPACK=${UNPACK:-1}
VERBOSE=${VERBOSE:-0}
EXTRACT_DIR=${EXTRACT_DIR:-`pwd`}
if [ -n "$HOME" ]; then
CACHE_DIR=${CACHE_DIR:-$HOME/.cache/fetchurl}
else
CACHE_DIR=${CACHE_DIR:-}
fi
TMP_DIR=${TMP_DIR:-/tmp}
URL=$1
set -u
stderr () {
echo $@ 1>&2
}
sh () {
echo $ $@
if [ "$VERBOSE" -ne 0 ]; then
$@
else
$@ >/dev/null 2>&1
fi
}
expand_path() {
here=`pwd`
cd $1
echo `pwd -P`
cd "$here"
}
usage() {
echo "Usage: fetchurl url"
echo "CACHE=${CACHE}"
echo "UNPACK=${UNPACK}"
echo "REPLACE=${REPLACE}"
echo "VERBOSE=${VERBOSE}"
echo "EXTRACT_DIR=${EXTRACT_DIR}"
echo "CACHE_DIR=${CACHE_DIR}"
echo "TMP_DIR=${TMP_DIR}"
echo "URL=${URL}"
exit 1
}
if [ -z "$URL" ]; then
stderr "ERROR: missing url"
usage
fi
if [ -z "$CACHE_DIR" ] && [ "$CACHE" -ne 0 ]; then
stderr "ERROR: missing cache dir"
usage
fi
filename=`basename "$URL" | sed 's/\?.*//'`
tmp_file="$TMP_DIR/$filename"
cache_file="$CACHE_DIR/$filename"
mkdir -p "$CACHE_DIR"
# Fetch
if [ "$CACHE" -eq 0 ] || [ ! -f "$cache_file" ]; then
rm -rf "$tmp_file"
sh curl -L -o "$tmp_file" "$URL"
sh mv "$tmp_file" "$cache_file"
fi
# TODO: checksums
# Unpack
if [ "$UNPACK" -ne 0 ]; then
if [ "$filename" != "${filename%.tar.gz}" ]; then
extname=.tar.gz
elif [ "$filename" != "${filename%.tgz}" ]; then
extname=.tgz
elif [ "$filename" != "${filename%.tar.bz2}" ]; then
extname=.tar.bz2
elif [ "$filename" != "${filename%.tar.xz}" ]; then
extname=.tar.xz
else
stderr extension of $filename is not supported
exit 1
fi
target_dir=`expand_path "$EXTRACT_DIR"`
mkdir -p "$target_dir"
sh cd "$target_dir"
[ "$REPLACE" -ne 1 ] && [ `uname` = "Linux" ] && tarargs="--skip-old-files" || tarargs=""
case "$extname" in
.tar.gz|.tgz)
sh tar "$tarargs" -xzvf "$cache_file"
;;
.tar.bz2)
sh tar "$tarargs" -xjvf "$cache_file"
;;
.tar.xz)
sh tar "$tarargs" -xJvf "$cache_file"
;;
*)
stderr BUG, this should not happen
exit 1
;;
esac
fi