Fix for missed interrupts, try 2. Clear any outstanding interrupt before
[tangerine.git] / scripts / fetch.sh
blob98ac8f0957fab33e4a96bdfb11238e3e5a43abe7
1 #!/usr/bin/env bash
3 # Copyright © 2004-2008, 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 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://downloads.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
92 "")
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"
104 echo "Unknown protocol type \`$protocol'"
105 ret=false;;
106 esac
108 trap SIGINT SIGKILL SIGTERM
110 $ret
113 fetch_multiple()
115 local origins="$1" file="$2" destination="$3"
116 for origin in $origins; do
117 echo "Trying $origin/$file..."
118 fetch "$origin" "$file" "$destination" && return 0
119 done
121 return 1
124 fetch_cached()
126 local origins="$1" file="$2" suffixes="$3" destination="$4" foundvar="$5"
128 local __dummy__
129 foundvar=${foundvar:-__dummy__}
131 export $foundvar=
133 test -e "$destination" -a ! -d "$destination" && \
134 echo "fetch_cached: \`$destination' is not a diretory." && return 1
136 if ! test -e "$destination"; then
137 echo "fetch_cached: \`$destination' does not exist. Making it."
138 ! mkdir -p "$destination" && return 1
141 if test "x$suffixes" != "x"; then
142 for sfx in $suffixes; do
143 fetch_multiple "$destination" "$file".$sfx "$destination" && \
144 export $foundvar="$file".$sfx && return 0
145 done
147 for sfx in $suffixes; do
148 fetch_multiple "$origins" "$file".$sfx "$destination" && \
149 export $foundvar="$file".$sfx && return 0
150 done
151 else
152 fetch_multiple "$destination $origins" "$file" "$destination" && \
153 export $foundvar="$file" && return 0
156 return 1
159 error()
161 echo $1
162 exit 1
166 unpack()
168 local location="$1" archive="$2" archivepath="$3";
170 local old_PWD="$PWD"
171 cd $location
173 echo "Unpacking \`$archive'..."
175 local ret=true
176 case "$archive" in
177 *.tar.bz2)
178 if ! tar xfj "$archivepath/$archive"; then ret=false; fi
180 *.tar.gz | *.tgz)
181 if ! tar xfz "$archivepath/$archive"; then ret=false; fi
184 echo "Unknown archive format for \`$archive'."
185 ret=false
187 esac
189 cd "$old_PWD"
191 $ret
194 unpack_cached()
196 local location="$1" archive="$2" archivepath="$3";
198 if ! test -e "$location"; then
199 echo "unpack_cached: \`$location' does not exist. Making it."
200 ! mkdir -p "$location" && return 1
203 if ! test -f ${location}/.${archive}.unpacked; then
204 if unpack "$location" "$archive" "$archivepath"; then
205 echo yes > ${location}/.${archive}.unpacked
206 true
207 else
208 false
213 do_patch()
215 local location="$1" patch_spec="$2";
217 local old_PWD="$PWD"
218 cd $location
219 local abs_location="$PWD"
221 local patch=`echo "$patch_spec": | cut -d: -f1`
222 local subdir=`echo "$patch_spec": | cut -d: -f2`
223 local patch_opt=`echo "$patch_spec": | cut -d: -f3`
225 cd ${subdir:-.}
227 local ret=true
229 if ! patch $patch_opt < $abs_location/$patch; then
230 ret=false
233 cd "$old_PWD"
235 $ret
238 patch_cached()
240 local location="$1" patch="$2";
242 local patchname=`echo $patch | cut -d: -f1`
244 if test "x$patchname" != "x"; then
245 if ! test -f ${location}/.${patchname}.applied; then
246 if do_patch "$location" "$patch"; then
247 echo yes > ${location}/.${patchname}.applied
248 true
249 else
250 false
256 location="./"
258 while test "x$1" != "x"; do
259 if test "x${1:0:1}" == "x-" -a "x${2:0:1}" == "x-"; then
260 usage "$0"
263 case "$1" in
264 -ao) archive_origins="$2";;
265 -a) archive="$2";;
266 -s) suffixes="$2";;
267 -d) destination="$2";;
268 -po) patches_origins="$2";;
269 -p) patches="$2";;
270 -l) location="$2";;
271 *) echo "fetch: Unknown option \`$1'."; usage "$0";;
272 esac
274 shift
275 shift
276 done
278 test -z "$archive" && usage "$0"
280 archive_origins=${archive_origins:-.}
281 destination=${destination:-.}
282 location=${location:-.}
283 patches_origins=${patches_origins:-.}
285 fetch_cached "$archive_origins" "$archive" "$suffixes" "$location" archive2
286 test -z "$archive2" && error "fetch: Error while fetching the archive \`$archive'."
287 archive="$archive2"
289 for patch in $patches; do
290 patch=`echo $patch | cut -d: -f1`
291 if test "x$patch" != "x"; then
292 if ! fetch_cached "$patches_origins" "$patch" "" "$destination"; then
293 fetch_cached "$patches_origins" "$patch" "tar.bz2 tar.gz" "$destination" patch2
294 test -z "$patch2" && error "fetch: Error while fetching the patch \`$patch'."
295 if ! unpack_cached "$destination" "$patch2" "$destination"; then
296 error "fetch: Error while unpacking \`$patch2'."
300 done
302 if ! unpack_cached "$destination" "$archive" "$location"; then
303 error "fetch: Error while unpacking \`$archive'."
306 for patch in $patches; do
307 if ! patch_cached "$destination" "$patch"; then
308 error "fetch: Error while applying the patch \`$patch'."
310 done