Added a new GNU mirror, as ftp.cise.ufl.edu is currently unavailable.
[AROS.git] / scripts / fetch.sh
blob4f3acbc7d4f850ecbc8238c963c8f62ad1cc0b46
1 #!/usr/bin/env bash
3 # Copyright © 2004-2014, The AROS Development Team. All rights reserved.
4 # $Id$
7 usage()
9 error "Usage: $1 -a archive [-s suffixes] [-ao archive_origins...] [-l location to download to] [-d dir to unpack to] [-po patches_origins...] [-p patch[:subdir][:patch options]...]"
12 fetch_mirrored()
14 local origin="$1" file="$2" destination="$3" mirrosgroup="$4" mirrors="$5"
15 local full_path
16 local ret=false
18 for mirror in $mirrors; do
19 echo "Downloading from ${mirrosgroup}... "
20 if fetch "${mirror}/$origin" "${file}" "$destination"; then
21 ret=true
22 break;
24 done
26 $ret
29 gnu_mirrors="http://ftp.gnu.org/pub/gnu http://mirror.mcs.anl.gov/pub/gnu ftp://ftp.cise.ufl.edu/pub/mirrors/GNU/gnu"
31 fetch_gnu()
33 local origin="$1" file="$2" destination="$3"
34 local full_path
35 local ret=true
37 if ! fetch_mirrored "$origin" "${file}" "$destination" "GNU" "${gnu_mirrors}"; then
38 ret=false
41 $ret
44 sf_mirrors="http://download.sourceforge.net http://aleron.dl.sourceforge.net http://voxel.dl.sourceforge.net http://heanet.dl.sourceforge.net http://avh.dl.sourceforge.net http://umn.dl.sourceforge.net http://unc.dl.sourceforge.net http://puzzle.dl.sourceforge.net http://mesh.dl.sourceforge.net"
46 fetch_sf()
48 local origin="$1" file="$2" destination="$3"
49 local full_path
50 local ret=true
52 if ! fetch_mirrored "$origin" "${file}" "$destination" "SourceForge" "${sf_mirrors}"; then
53 ret=false
56 $ret
59 fetch()
61 local origin="$1" file="$2" destination="$3"
63 local protocol
65 if echo $origin | grep ":" >/dev/null; then
66 protocol=`echo $origin | cut -d':' -f1`
69 local ret=true
71 trap 'rm -f "$destination/$file".tmp; exit' SIGINT SIGKILL SIGTERM
73 case $protocol in
74 http | ftp)
75 if ! wget -t 5 -T 15 -c "$origin/$file" -O "$destination/$file".tmp; then
76 ret=false
77 else
78 mv "$destination/$file".tmp "$destination/$file"
80 rm -f "$destination/$file".tmp
82 gnu)
83 if ! fetch_gnu "${origin:${#protocol}+3}" "$file" "$destination"; then
84 ret=false
87 sf | sourceforge)
88 if ! fetch_sf "${origin:${#protocol}+3}" "$file" "$destination"; then
89 ret=false
93 if test "$origin" = "$destination"; then
94 ! test -f "$origin/$file" && ret=false
95 else
96 if ! cp "$origin/$file" "$destination/$file".tmp; then
97 ret=false
98 else
99 mv "$destination/$file".tmp "$destination/$file"
103 esac
105 trap SIGINT SIGKILL SIGTERM
107 $ret
110 fetch_multiple()
112 local origins="$1" file="$2" destination="$3"
113 for origin in $origins; do
114 echo "Trying $origin/$file..."
115 fetch "$origin" "$file" "$destination" && return 0
116 done
118 return 1
121 fetch_cached()
123 local origins="$1" file="$2" suffixes="$3" destination="$4" foundvar="$5"
125 local __dummy__
126 foundvar=${foundvar:-__dummy__}
128 export $foundvar=
130 test -e "$destination" -a ! -d "$destination" && \
131 echo "fetch_cached: \`$destination' is not a diretory." && return 1
133 if ! test -e "$destination"; then
134 echo "fetch_cached: \`$destination' does not exist. Making it."
135 ! mkdir -p "$destination" && return 1
138 if test "x$suffixes" != "x"; then
139 for sfx in $suffixes; do
140 fetch_multiple "$destination" "$file".$sfx "$destination" && \
141 export $foundvar="$file".$sfx && return 0
142 done
144 for sfx in $suffixes; do
145 fetch_multiple "$origins" "$file".$sfx "$destination" && \
146 export $foundvar="$file".$sfx && return 0
147 done
148 else
149 fetch_multiple "$destination $origins" "$file" "$destination" && \
150 export $foundvar="$file" && return 0
153 return 1
156 error()
158 echo $1
159 exit 1
163 unpack()
165 local location="$1" archive="$2" archivepath="$3";
167 local old_PWD="$PWD"
168 cd $location
170 echo "Unpacking \`$archive'..."
172 local ret=true
173 case "$archive" in
174 *.tar.bz2)
175 if ! tar xfj "$archivepath/$archive"; then ret=false; fi
177 *.tar.gz | *.tgz)
178 if ! tar xfz "$archivepath/$archive"; then ret=false; fi
180 *.zip)
181 if ! unzip "$archivepath/$archive"; then ret=false; fi
183 *.tar.xz)
184 if ! tar xfJ "$archivepath/$archive"; then ret=false; fi
187 echo "Unknown archive format for \`$archive'."
188 ret=false
190 esac
192 cd "$old_PWD"
194 $ret
197 unpack_cached()
199 local location="$1" archive="$2" archivepath="$3";
201 if ! test -e "$location"; then
202 echo "unpack_cached: \`$location' does not exist. Making it."
203 ! mkdir -p "$location" && return 1
206 if ! test -f ${location}/.${archive}.unpacked; then
207 if unpack "$location" "$archive" "$archivepath"; then
208 echo yes > ${location}/.${archive}.unpacked
209 true
210 else
211 false
216 do_patch()
218 local location="$1" patch_spec="$2";
220 local old_PWD="$PWD"
221 cd $location
222 local abs_location="$PWD"
224 local patch=`echo "$patch_spec": | cut -d: -f1`
225 local subdir=`echo "$patch_spec": | cut -d: -f2`
226 local patch_opt=`echo "$patch_spec": | cut -d: -f3 | sed -e "s/,/ /g"`
228 cd ${subdir:-.}
230 local ret=true
232 if ! patch $patch_opt < $abs_location/$patch; then
233 ret=false
236 cd "$old_PWD"
238 $ret
241 patch_cached()
243 local location="$1" patch="$2";
245 local patchname=`echo $patch | cut -d: -f1`
247 if test "x$patchname" != "x"; then
248 if ! test -f ${location}/.${patchname}.applied; then
249 if do_patch "$location" "$patch"; then
250 echo yes > ${location}/.${patchname}.applied
251 true
252 else
253 false
259 location="./"
261 while test "x$1" != "x"; do
262 if test "x${1:0:1}" == "x-" -a "x${2:0:1}" == "x-"; then
263 usage "$0"
266 case "$1" in
267 -ao) archive_origins="$2";;
268 -a) archive="$2";;
269 -s) suffixes="$2";;
270 -d) destination="$2";;
271 -po) patches_origins="$2";;
272 -p) patches="$2";;
273 -l) location="$2";;
274 *) echo "fetch: Unknown option \`$1'."; usage "$0";;
275 esac
277 shift
278 shift
279 done
281 test -z "$archive" && usage "$0"
283 archive_origins=${archive_origins:-.}
284 destination=${destination:-.}
285 location=${location:-.}
286 patches_origins=${patches_origins:-.}
288 fetch_cached "$archive_origins" "$archive" "$suffixes" "$location" archive2
289 test -z "$archive2" && error "fetch: Error while fetching the archive \`$archive'."
290 archive="$archive2"
292 for patch in $patches; do
293 patch=`echo $patch | cut -d: -f1`
294 if test "x$patch" != "x"; then
295 if ! fetch_cached "$patches_origins" "$patch" "" "$destination"; then
296 fetch_cached "$patches_origins" "$patch" "tar.bz2 tar.gz zip" "$destination" patch2
297 test -z "$patch2" && error "fetch: Error while fetching the patch \`$patch'."
298 if ! unpack_cached "$destination" "$patch2" "$destination"; then
299 error "fetch: Error while unpacking \`$patch2'."
303 done
305 if test "x$suffixes" != "x"; then
306 if ! unpack_cached "$destination" "$archive" "$location"; then
307 error "fetch: Error while unpacking \`$archive'."
311 for patch in $patches; do
312 if ! patch_cached "$destination" "$patch"; then
313 error "fetch: Error while applying the patch \`$patch'."
315 done