start service tasks separately in-case platforms need to perform additional set-up...
[AROS.git] / scripts / fetch.sh
blob9c5003efdd779d0908eb3928fa4499d3876de8bb
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 github_mirrors="https://github.com"
61 fetch_github()
63 local origin="$1" file="$2" destination="$3"
64 local full_path
65 local ret=true
67 if ! fetch_mirrored "$origin" "$file" "$destination" "Github" "${github_mirrors}"; then
68 ret=false
71 $ret
74 fetch()
76 local origin="$1" file="$2" destination="$3"
78 local protocol
80 if echo $origin | grep ":" >/dev/null; then
81 protocol=`echo $origin | cut -d':' -f1`
84 local ret=true
86 trap 'rm -f "$destination/$file".tmp; exit' SIGINT SIGKILL SIGTERM
88 case $protocol in
89 https| http | ftp)
90 if ! wget -t 5 -T 15 -c "$origin/$file" -O "$destination/$file".tmp; then
91 ret=false
92 else
93 mv "$destination/$file".tmp "$destination/$file"
95 rm -f "$destination/$file".tmp
97 gnu)
98 if ! fetch_gnu "${origin:${#protocol}+3}" "$file" "$destination"; then
99 ret=false
102 sf | sourceforge)
103 if ! fetch_sf "${origin:${#protocol}+3}" "$file" "$destination"; then
104 ret=false
107 github)
108 if ! fetch_github "${origin:${#protocol}+3}" "$file" "$destination"; then
109 ret=false
113 if test "$origin" = "$destination"; then
114 ! test -f "$origin/$file" && ret=false
115 else
116 if ! cp "$origin/$file" "$destination/$file".tmp; then
117 ret=false
118 else
119 mv "$destination/$file".tmp "$destination/$file"
123 esac
125 trap SIGINT SIGKILL SIGTERM
127 $ret
130 fetch_multiple()
132 local origins="$1" file="$2" destination="$3"
133 for origin in $origins; do
134 echo "Trying $origin/$file..."
135 fetch "$origin" "$file" "$destination" && return 0
136 done
138 return 1
141 fetch_cached()
143 local origins="$1" file="$2" suffixes="$3" destination="$4" foundvar="$5"
145 local __dummy__
146 foundvar=${foundvar:-__dummy__}
148 export $foundvar=
150 test -e "$destination" -a ! -d "$destination" && \
151 echo "fetch_cached: \`$destination' is not a diretory." && return 1
153 if ! test -e "$destination"; then
154 echo "fetch_cached: \`$destination' does not exist. Making it."
155 ! mkdir -p "$destination" && return 1
158 if test "x$suffixes" != "x"; then
159 for sfx in $suffixes; do
160 fetch_multiple "$destination" "$file".$sfx "$destination" && \
161 export $foundvar="$file".$sfx && return 0
162 done
164 for sfx in $suffixes; do
165 fetch_multiple "$origins" "$file".$sfx "$destination" && \
166 export $foundvar="$file".$sfx && return 0
167 done
168 else
169 fetch_multiple "$destination $origins" "$file" "$destination" && \
170 export $foundvar="$file" && return 0
173 return 1
176 error()
178 echo $1
179 exit 1
183 unpack()
185 local location="$1" archive="$2" archivepath="$3";
187 local old_PWD="$PWD"
188 cd $location
190 echo "Unpacking \`$archive'..."
192 local ret=true
193 case "$archive" in
194 *.tar.bz2)
195 if ! tar xfj "$archivepath/$archive"; then ret=false; fi
197 *.tar.gz | *.tgz)
198 if ! tar xfz "$archivepath/$archive"; then ret=false; fi
200 *.zip)
201 if ! unzip "$archivepath/$archive"; then ret=false; fi
203 *.tar.xz)
204 if ! tar xfJ "$archivepath/$archive"; then ret=false; fi
207 echo "Unknown archive format for \`$archive'."
208 ret=false
210 esac
212 cd "$old_PWD"
214 $ret
217 unpack_cached()
219 local location="$1" archive="$2" archivepath="$3";
221 if ! test -e "$location"; then
222 echo "unpack_cached: \`$location' does not exist. Making it."
223 ! mkdir -p "$location" && return 1
226 if ! test -f ${location}/.${archive}.unpacked; then
227 if unpack "$location" "$archive" "$archivepath"; then
228 echo yes > ${location}/.${archive}.unpacked
229 true
230 else
231 false
236 do_patch()
238 local location="$1" patch_spec="$2";
240 local old_PWD="$PWD"
241 cd $location
242 local abs_location="$PWD"
244 local patch=`echo "$patch_spec": | cut -d: -f1`
245 local subdir=`echo "$patch_spec": | cut -d: -f2`
246 local patch_opt=`echo "$patch_spec": | cut -d: -f3 | sed -e "s/,/ /g"`
248 cd ${subdir:-.}
250 local ret=true
252 if ! patch $patch_opt < $abs_location/$patch; then
253 ret=false
256 cd "$old_PWD"
258 $ret
261 patch_cached()
263 local location="$1" patch="$2";
265 local patchname=`echo $patch | cut -d: -f1`
267 if test "x$patchname" != "x"; then
268 if ! test -f ${location}/.${patchname}.applied; then
269 if do_patch "$location" "$patch"; then
270 echo yes > ${location}/.${patchname}.applied
271 true
272 else
273 false
279 location="./"
281 while test "x$1" != "x"; do
282 if test "x${1:0:1}" == "x-" -a "x${2:0:1}" == "x-"; then
283 usage "$0"
286 case "$1" in
287 -ao) archive_origins="$2";;
288 -a) archive="$2";;
289 -s) suffixes="$2";;
290 -d) destination="$2";;
291 -po) patches_origins="$2";;
292 -p) patches="$2";;
293 -l) location="$2";;
294 *) echo "fetch: Unknown option \`$1'."; usage "$0";;
295 esac
297 shift
298 shift
299 done
301 test -z "$archive" && usage "$0"
303 archive_origins=${archive_origins:-.}
304 destination=${destination:-.}
305 location=${location:-.}
306 patches_origins=${patches_origins:-.}
308 fetch_cached "$archive_origins" "$archive" "$suffixes" "$location" archive2
309 test -z "$archive2" && error "fetch: Error while fetching the archive \`$archive'."
310 archive="$archive2"
312 for patch in $patches; do
313 patch=`echo $patch | cut -d: -f1`
314 if test "x$patch" != "x"; then
315 if ! fetch_cached "$patches_origins" "$patch" "" "$destination"; then
316 fetch_cached "$patches_origins" "$patch" "tar.bz2 tar.gz zip" "$destination" patch2
317 test -z "$patch2" && error "fetch: Error while fetching the patch \`$patch'."
318 if ! unpack_cached "$destination" "$patch2" "$destination"; then
319 error "fetch: Error while unpacking \`$patch2'."
323 done
325 if test "x$suffixes" != "x"; then
326 if ! unpack_cached "$destination" "$archive" "$location"; then
327 error "fetch: Error while unpacking \`$archive'."
331 for patch in $patches; do
332 if ! patch_cached "$destination" "$patch"; then
333 error "fetch: Error while applying the patch \`$patch'."
335 done