r26531@plastic: rob | 2007-05-19 23:49:19 +1000
[tangerine.git] / scripts / fetch.sh
blobfe1e481ab3ed27d7ad4de69fdfb00e58abbdb08d
1 #!/usr/bin/env bash
3 # Copyright © 2004, The AROS Development Team. All rights reserved.
4 # $Id$
7 usage()
9 error "Usage: $1 -a archive [-as archive_suffixes] [-ao archive_origins...] [-d destination] [-po patches_origins...] [-p patch[:subdir][:patch options]...]"
12 sf_mirrors="aleron voxel heanet avh umn unc puzzle mesh"
14 fetch_sf()
16 local origin="$1" file="$2" destination="$3"
17 local full_path
19 for i in 1; do
20 echo "Downloading from SourceForge... Try n. $i."
21 fetch "http://downloads.sourceforge.net/$origin" "${file}" "$destination" && break;
22 done
25 fetch()
27 local origin="$1" file="$2" destination="$3"
29 local protocol
31 if echo $origin | grep ":" >/dev/null; then
32 protocol=`echo $origin | cut -d':' -f1`
35 local ret=true
37 trap 'rm -f "$destination/$file".tmp; exit' SIGINT SIGKILL SIGTERM
39 case $protocol in
40 http | ftp)
41 if ! wget -t 5 -T 5 -c "$origin/$file" -O "$destination/$file".tmp; then
42 ret=false
43 else
44 mv "$destination/$file".tmp "$destination/$file"
46 rm -f "$destination/$file".tmp
48 sf | sourceforge)
49 ! fetch_sf "${origin:${#protocol}+3}" "$file" "$destination" && ret=false
51 "")
52 if test "$origin" = "$destination"; then
53 ! test -f "$origin/$file" && ret=false
54 else
55 if ! cp "$origin/$file" "$destination/$file".tmp; then
56 ret=false
57 else
58 mv "$destination/$file".tmp "$destination/$file"
63 echo "Unknown protocol type \`$protocol'"
64 ret=false;;
65 esac
67 trap SIGINT SIGKILL SIGTERM
69 $ret
72 fetch_multiple()
74 local origins="$1" file="$2" destination="$3"
75 for origin in $origins; do
76 echo "Trying $origin/$file..."
77 fetch "$origin" "$file" "$destination" && return 0
78 done
80 return 1
83 fetch_cached()
85 local origins="$1" file="$2" suffixes="$3" destination="$4" foundvar="$5"
87 local __dummy__
88 foundvar=${foundvar:-__dummy__}
90 export $foundvar=
92 test -e "$destination" -a ! -d "$destination" && \
93 echo "\`$destination' is not a diretory." && return 1
95 if ! test -e "$destination"; then
96 echo "\`$destination' does not exist. Making it."
97 ! mkdir -p "$destination" && return 1
100 if test "x$suffixes" != "x"; then
101 for sfx in $suffixes; do
102 fetch_multiple "$destination" "$file".$sfx "$destination" && \
103 export $foundvar="$file".$sfx && return 0
104 done
106 for sfx in $suffixes; do
107 fetch_multiple "$origins" "$file".$sfx "$destination" && \
108 export $foundvar="$file".$sfx && return 0
109 done
110 else
111 fetch_multiple "$destination $origins" "$file" "$destination" && \
112 export $foundvar="$file" && return 0
115 return 1
118 error()
120 echo $1
121 exit 1
125 unpack()
127 local location="$1" archive="$2";
129 local old_PWD="$PWD"
130 cd $location
132 echo "Unpacking \`$archive'..."
134 local ret=true
135 case "$archive" in
136 *.tar.bz2)
137 if ! tar xfj "$archive"; then ret=false; fi
139 *.tar.gz | *.tgz)
140 if ! tar xfz "$archive"; then ret=false; fi
143 echo "Unknown archive format for \`$archive'."
144 ret=false
146 esac
148 cd "$old_PWD"
150 $ret
153 unpack_cached()
155 local location="$1" archive="$2";
157 if ! test -f ${location}/.${archive}.unpacked; then
158 if unpack "$location" "$archive"; then
159 echo yes > ${location}/.${archive}.unpacked
160 true
161 else
162 false
167 do_patch()
169 local location="$1" patch_spec="$2";
171 local old_PWD="$PWD"
172 cd $location
173 local abs_location="$PWD"
175 local patch=`echo "$patch_spec": | cut -d: -f1`
176 local subdir=`echo "$patch_spec": | cut -d: -f2`
177 local patch_opt=`echo "$patch_spec": | cut -d: -f3`
179 cd ${subdir:-.}
181 local ret=true
183 if ! patch $patch_opt < $abs_location/$patch; then
184 ret=false
187 cd "$old_PWD"
189 $ret
192 patch_cached()
194 local location="$1" patch="$2";
196 local patchname=`echo $patch | cut -d: -f1`
198 if test "x$patchname" != "x"; then
199 if ! test -f ${location}/.${patchname}.applied; then
200 if do_patch "$location" "$patch"; then
201 echo yes > ${location}/.${patchname}.applied
202 true
203 else
204 false
211 while test "x$1" != "x"; do
212 if test "x${1:0:1}" == "x-" -a "x${2:0:1}" == "x-"; then
213 usage "$0"
216 case "$1" in
217 -ao) archive_origins="$2";;
218 -a) archive="$2";;
219 -as) archive_suffixes="$2";;
220 -d) destination="$2";;
221 -po) patches_origins="$2";;
222 -p) patches="$2";;
223 *) echo "Unknown option \`$1'."; usage "$0";;
224 esac
226 shift
227 shift
228 done
230 test -z "$archive" && usage "$0"
232 archive_origins=${archive_origins:-.}
233 destination=${destination:-.}
234 patches_origins=${patches_origins:-.}
236 fetch_cached "$archive_origins" "$archive" "$archive_suffixes" "$destination" archive2
237 test -z "$archive2" && error "Error while fetching the archive \`$archive'."
238 archive="$archive2"
240 for patch in $patches; do
241 patch=`echo $patch | cut -d: -f1`
242 if test "x$patch" != "x"; then
243 if ! fetch_cached "$patches_origins" "$patch" "" "$destination"; then
244 error "Error while fetching the patch \`$patch'."
247 done
249 if ! unpack_cached "$destination" "$archive"; then
250 error "Error while unpacking \`$archive'."
253 for patch in $patches; do
254 if ! patch_cached "$destination" "$patch"; then
255 error "Error while applying the patch \`$patch'."
257 done