huperlayers.library now uses autoopening of libraries
[AROS.git] / scripts / fetch.sh
blobf273286a28a29e2daaa892efedf4747a422f7adb
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 mirror in $sf_mirrors; do
20 echo "Checking SourceForge mirror \`$mirror'..."
21 fetch "http://prdownloads.sourceforge.net/$origin" "${file}?use_mirror=${mirror}" "$destination" \
22 2>/dev/null && \
23 full_path=`awk '/<META[ \t\n]+HTTP-EQUIV.+/ { match($4, /=.+"/); print substr($4, RSTART+1, RLENGTH-2) }' "$destination/${file}?use_mirror=${mirror}"` && \
24 rm "$destination/${file}?use_mirror=${mirror}" && \
25 test "x$full_path" != "x" && \
26 fetch "`dirname $full_path`" "$file" "$destination" && \
27 break
28 done
31 fetch()
33 local origin="$1" file="$2" destination="$3"
35 local protocol
37 if echo $origin | grep ":" >/dev/null; then
38 protocol=`echo $origin | cut -d':' -f1`
41 local ret=true
43 trap 'rm -f "$destination/$file".tmp; exit' SIGINT SIGKILL SIGTERM
45 case $protocol in
46 http | ftp)
47 if ! wget -c "$origin/$file" -O "$destination/$file".tmp; then
48 ret=false
49 else
50 mv "$destination/$file".tmp "$destination/$file"
52 rm -f "$destination/$file".tmp
54 sf | sourceforge)
55 ! fetch_sf "${origin:${#protocol}+3}" "$file" "$destination" && ret=false
57 "")
58 if test "$origin" = "$destination"; then
59 ! test -f "$origin/$file" && ret=false
60 else
61 if ! cp "$origin/$file" "$destination/$file".tmp; then
62 ret=false
63 else
64 mv "$destination/$file".tmp "$destination/$file"
69 echo "Unknown protocol type \`$protocol'"
70 ret=false;;
71 esac
73 trap SIGINT SIGKILL SIGTERM
75 $ret
78 fetch_multiple()
80 local origins="$1" file="$2" destination="$3"
81 for origin in $origins; do
82 echo "Trying $origin/$file..."
83 fetch "$origin" "$file" "$destination" && return 0
84 done
86 return 1
89 fetch_cached()
91 local origins="$1" file="$2" suffixes="$3" destination="$4" foundvar="$5"
93 local __dummy__
94 foundvar=${foundvar:-__dummy__}
96 export $foundvar=
98 test -e "$destination" -a ! -d "$destination" && \
99 echo "\`$destination' is not a diretory." && return 1
101 if ! test -e "$destination"; then
102 echo "\`$destination' does not exist. Making it."
103 ! mkdir -p "$destination" && return 1
106 if test "x$suffixes" != "x"; then
107 for sfx in $suffixes; do
108 fetch_multiple "$destination" "$file".$sfx "$destination" && \
109 export $foundvar="$file".$sfx && return 0
110 done
112 for sfx in $suffixes; do
113 fetch_multiple "$origins" "$file".$sfx "$destination" && \
114 export $foundvar="$file".$sfx && return 0
115 done
116 else
117 fetch_multiple "$destination $origins" "$file" "$destination" && \
118 export $foundvar="$file" && return 0
121 return 1
124 error()
126 echo $1
127 exit 1
131 unpack()
133 local location="$1" archive="$2";
135 local old_PWD="$PWD"
136 cd $location
138 echo "Unpacking \`$archive'..."
140 local ret=true
141 case "$archive" in
142 *.tar.bz2)
143 if ! tar xfj "$archive"; then ret=false; fi
145 *.tar.gz | *.tgz)
146 if ! tar xfz "$archive"; then ret=false; fi
149 echo "Unknown archive format for \`$archive'."
150 ret=false
152 esac
154 cd "$old_PWD"
156 $ret
159 unpack_cached()
161 local location="$1" archive="$2";
163 if ! test -f ${location}/.${archive}.unpacked; then
164 if unpack "$location" "$archive"; then
165 echo yes > ${location}/.${archive}.unpacked
166 true
167 else
168 false
173 do_patch()
175 local location="$1" patch_spec="$2";
177 local old_PWD="$PWD"
178 cd $location
179 local abs_location="$PWD"
181 local patch=`echo "$patch_spec": | cut -d: -f1`
182 local subdir=`echo "$patch_spec": | cut -d: -f2`
183 local patch_opt=`echo "$patch_spec": | cut -d: -f3`
185 cd ${subdir:-.}
187 local ret=true
189 if ! patch $patch_opt < $abs_location/$patch; then
190 ret=false
193 cd "$old_PWD"
195 $ret
198 patch_cached()
200 local location="$1" patch="$2";
202 local patchname=`echo $patch | cut -d: -f1`
204 if test "x$patchname" != "x"; then
205 if ! test -f ${location}/.${patchname}.applied; then
206 if do_patch "$location" "$patch"; then
207 echo yes > ${location}/.${patchname}.applied
208 true
209 else
210 false
217 while test "x$1" != "x"; do
218 if test "x${1:0:1}" == "x-" -a "x${2:0:1}" == "x-"; then
219 usage "$0"
222 case "$1" in
223 -ao) archive_origins="$2";;
224 -a) archive="$2";;
225 -as) archive_suffixes="$2";;
226 -d) destination="$2";;
227 -po) patches_origins="$2";;
228 -p) patches="$2";;
229 *) echo "Unknown option \`$1'."; usage "$0";;
230 esac
232 shift
233 shift
234 done
236 test -z "$archive" && usage "$0"
238 archive_origins=${archive_origins:-.}
239 destination=${destination:-.}
240 patches_origins=${patches_origins:-.}
242 fetch_cached "$archive_origins" "$archive" "$archive_suffixes" "$destination" archive2
243 test -z "$archive2" && error "Error while fetching the archive \`$archive'."
244 archive="$archive2"
246 for patch in $patches; do
247 patch=`echo $patch | cut -d: -f1`
248 if test "x$patch" != "x"; then
249 if ! fetch_cached "$patches_origins" "$patch" "" "$destination"; then
250 error "Error while fetching the patch \`$patch'."
253 done
255 if ! unpack_cached "$destination" "$archive"; then
256 error "Error while unpacking \`$archive'."
259 for patch in $patches; do
260 if ! patch_cached "$destination" "$patch"; then
261 error "Error while applying the patch \`$patch'."
263 done