some further WIP polish localization.
[AROS.git] / scripts / fetch.sh
blob9d95a3539394ce041b483cd7b06281bc44f574d2
1 #!/usr/bin/env bash
3 # Copyright © 2004-2017, 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://mirror.netcologne.de/gnu http://ftp.gnu.org/pub/gnu ftp://ftp.heanet.ie/pub/gnu ftp://mirror.jre655.com/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 # Note: at time of writing, the main SF download server redirects to mirrors
45 # and corrects moved paths, so we retry this server multiple times (in one
46 # wget call).
47 sf_mirrors="http://downloads.sourceforge.net"
49 fetch_sf()
51 local origin="$1" file="$2" destination="$3"
52 local full_path
53 local ret=true
55 if ! fetch_mirrored "$origin" "${file}" "$destination" "SourceForge" "${sf_mirrors}"; then
56 ret=false
59 $ret
62 github_mirrors="https://github.com"
64 fetch_github()
66 local origin="$1" file="$2" destination="$3"
67 local full_path
68 local ret=true
70 if ! fetch_mirrored "$origin" "$file" "$destination" "Github" "${github_mirrors}"; then
71 ret=false
74 $ret
77 fetch()
79 local origin="$1" file="$2" destination="$3"
81 local protocol
83 if echo $origin | grep ":" >/dev/null; then
84 protocol=`echo $origin | cut -d':' -f1`
87 local ret=true
89 trap 'rm -f "$destination/$file".tmp; exit' SIGINT SIGKILL SIGTERM
91 case $protocol in
92 https| http | ftp)
93 if ! wget -t 3 --retry-connrefused -T 15 -c "$origin/$file" -O "$destination/$file".tmp; then
94 ret=false
95 else
96 mv "$destination/$file".tmp "$destination/$file"
98 rm -f "$destination/$file".tmp
100 gnu)
101 if ! fetch_gnu "${origin:${#protocol}+3}" "$file" "$destination"; then
102 ret=false
105 sf | sourceforge)
106 if ! fetch_sf "${origin:${#protocol}+3}" "$file" "$destination"; then
107 ret=false
110 github)
111 if ! fetch_github "${origin:${#protocol}+3}" "$file" "$destination"; then
112 ret=false
116 if test "$origin" = "$destination"; then
117 ! test -f "$origin/$file" && ret=false
118 else
119 if ! cp "$origin/$file" "$destination/$file".tmp; then
120 ret=false
121 else
122 mv "$destination/$file".tmp "$destination/$file"
126 esac
128 trap SIGINT SIGKILL SIGTERM
130 $ret
133 fetch_multiple()
135 local origins="$1" file="$2" destination="$3"
136 for origin in $origins; do
137 echo "Trying $origin/$file..."
138 fetch "$origin" "$file" "$destination" && return 0
139 done
141 return 1
144 fetch_cached()
146 local origins="$1" file="$2" suffixes="$3" destination="$4" foundvar="$5"
148 local __dummy__
149 foundvar=${foundvar:-__dummy__}
151 export $foundvar=
153 test -e "$destination" -a ! -d "$destination" && \
154 echo "fetch_cached: \`$destination' is not a diretory." && return 1
156 if ! test -e "$destination"; then
157 echo "fetch_cached: \`$destination' does not exist. Making it."
158 ! mkdir -p "$destination" && return 1
161 if test "x$suffixes" != "x"; then
162 for sfx in $suffixes; do
163 fetch_multiple "$destination" "$file".$sfx "$destination" && \
164 export $foundvar="$file".$sfx && return 0
165 done
167 for sfx in $suffixes; do
168 fetch_multiple "$origins" "$file".$sfx "$destination" && \
169 export $foundvar="$file".$sfx && return 0
170 done
171 else
172 fetch_multiple "$destination $origins" "$file" "$destination" && \
173 export $foundvar="$file" && return 0
176 return 1
179 error()
181 echo $1
182 exit 1
186 unpack()
188 local location="$1" archive="$2" archivepath="$3";
190 local old_PWD="$PWD"
191 cd $location
193 echo "Unpacking \`$archive'..."
195 local ret=true
196 case "$archive" in
197 *.tar.bz2)
198 if ! tar xfj "$archivepath/$archive"; then ret=false; fi
200 *.tar.gz | *.tgz)
201 if ! tar xfz "$archivepath/$archive"; then ret=false; fi
203 *.zip)
204 if ! unzip "$archivepath/$archive"; then ret=false; fi
206 *.tar.xz)
207 if ! tar xfJ "$archivepath/$archive"; then ret=false; fi
210 echo "Unknown archive format for \`$archive'."
211 ret=false
213 esac
215 cd "$old_PWD"
217 $ret
220 unpack_cached()
222 local location="$1" archive="$2" archivepath="$3";
224 if ! test -e "$location"; then
225 echo "unpack_cached: \`$location' does not exist. Making it."
226 ! mkdir -p "$location" && return 1
229 if ! test -f ${location}/.${archive}.unpacked; then
230 if unpack "$location" "$archive" "$archivepath"; then
231 echo yes > ${location}/.${archive}.unpacked
232 true
233 else
234 false
239 do_patch()
241 local location="$1" patch_spec="$2";
243 local old_PWD="$PWD"
244 cd $location
245 local abs_location="$PWD"
247 local patch=`echo "$patch_spec": | cut -d: -f1`
248 local subdir=`echo "$patch_spec": | cut -d: -f2`
249 local patch_opt=`echo "$patch_spec": | cut -d: -f3 | sed -e "s/,/ /g"`
251 cd ${subdir:-.}
253 local ret=true
255 if ! patch $patch_opt < $abs_location/$patch; then
256 ret=false
259 cd "$old_PWD"
261 $ret
264 patch_cached()
266 local location="$1" patch="$2";
268 local patchname=`echo $patch | cut -d: -f1`
270 if test "x$patchname" != "x"; then
271 if ! test -f ${location}/.${patchname}.applied; then
272 if do_patch "$location" "$patch"; then
273 echo yes > ${location}/.${patchname}.applied
274 true
275 else
276 false
282 location="./"
284 while test "x$1" != "x"; do
285 if test "x${1:0:1}" == "x-" -a "x${2:0:1}" == "x-"; then
286 usage "$0"
289 case "$1" in
290 -ao) archive_origins="$2";;
291 -a) archive="$2";;
292 -s) suffixes="$2";;
293 -d) destination="$2";;
294 -po) patches_origins="$2";;
295 -p) patches="$2";;
296 -l) location="$2";;
297 *) echo "fetch: Unknown option \`$1'."; usage "$0";;
298 esac
300 shift
301 shift
302 done
304 test -z "$archive" && usage "$0"
306 archive_origins=${archive_origins:-.}
307 destination=${destination:-.}
308 location=${location:-.}
309 patches_origins=${patches_origins:-.}
311 fetch_cached "$archive_origins" "$archive" "$suffixes" "$location" archive2
312 test -z "$archive2" && error "fetch: Error while fetching the archive \`$archive'."
313 archive="$archive2"
315 for patch in $patches; do
316 patch=`echo $patch | cut -d: -f1`
317 if test "x$patch" != "x"; then
318 if ! fetch_cached "$patches_origins" "$patch" "" "$destination"; then
319 fetch_cached "$patches_origins" "$patch" "tar.bz2 tar.gz zip" "$destination" patch2
320 test -z "$patch2" && error "fetch: Error while fetching the patch \`$patch'."
321 if ! unpack_cached "$destination" "$patch2" "$destination"; then
322 error "fetch: Error while unpacking \`$patch2'."
326 done
328 if test "x$suffixes" != "x"; then
329 if ! unpack_cached "$destination" "$archive" "$location"; then
330 error "fetch: Error while unpacking \`$archive'."
334 for patch in $patches; do
335 if ! patch_cached "$destination" "$patch"; then
336 error "fetch: Error while applying the patch \`$patch'."
338 done