build: add support for choosing a different url filename part than the output file
[openwrt/staging/yousong.git] / scripts / download.pl
1 #!/usr/bin/env perl
2 #
3 # Copyright (C) 2006 OpenWrt.org
4 #
5 # This is free software, licensed under the GNU General Public License v2.
6 # See /LICENSE for more information.
7 #
8
9 use strict;
10 use warnings;
11 use File::Basename;
12 use File::Copy;
13
14 @ARGV > 2 or die "Syntax: $0 <target dir> <filename> <md5sum> <url filename> [<mirror> ...]\n";
15
16 my $url_filename;
17 my $target = shift @ARGV;
18 my $filename = shift @ARGV;
19 my $md5sum = shift @ARGV;
20 $url_filename = shift @ARGV unless $ARGV[0] =~ /:\/\//;
21 my $scriptdir = dirname($0);
22 my @mirrors;
23 my $ok;
24
25 $url_filename or $url_filename = $filename;
26
27 sub localmirrors {
28 my @mlist;
29 open LM, "$scriptdir/localmirrors" and do {
30 while (<LM>) {
31 chomp $_;
32 push @mlist, $_ if $_;
33 }
34 close LM;
35 };
36 open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
37 while (<CONFIG>) {
38 /^CONFIG_LOCALMIRROR="(.+)"/ and do {
39 chomp;
40 my @local_mirrors = split(/;/, $1);
41 push @mlist, @local_mirrors;
42 };
43 }
44 close CONFIG;
45 };
46
47 my $mirror = $ENV{'DOWNLOAD_MIRROR'};
48 $mirror and push @mlist, split(/;/, $mirror);
49
50 return @mlist;
51 }
52
53 sub which($) {
54 my $prog = shift;
55 my $res = `which $prog`;
56 $res or return undef;
57 $res =~ /^no / and return undef;
58 $res =~ /not found/ and return undef;
59 return $res;
60 }
61
62 my $md5cmd = which("md5sum") || which("md5") || die 'no md5 checksum program found, please install md5 or md5sum';
63 chomp $md5cmd;
64
65 sub download
66 {
67 my $mirror = shift;
68 my $options = $ENV{WGET_OPTIONS} || "";
69
70 $mirror =~ s!/$!!;
71
72 if ($mirror =~ s!^file://!!) {
73 if (! -d "$mirror") {
74 print STDERR "Wrong local cache directory -$mirror-.\n";
75 cleanup();
76 return;
77 }
78
79 if (! -d "$target") {
80 system("mkdir", "-p", "$target/");
81 }
82
83 if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
84 print("Failed to search for $filename in $mirror\n");
85 return;
86 }
87
88 my $link;
89
90 while (defined(my $line = readline TMPDLS)) {
91 chomp ($link = $line);
92 if ($. > 1) {
93 print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
94 return;
95 }
96 }
97
98 close TMPDLS;
99
100 if (! $link) {
101 print("No instances of $filename found in $mirror.\n");
102 return;
103 }
104
105 print("Copying $filename from $link\n");
106 copy($link, "$target/$filename.dl");
107
108 if (system("$md5cmd '$target/$filename.dl' > '$target/$filename.md5sum'")) {
109 print("Failed to generate md5 sum for $filename\n");
110 return;
111 }
112 } else {
113 open WGET, "wget -t5 --timeout=20 --no-check-certificate $options -O- '$mirror/$url_filename' |" or die "Cannot launch wget.\n";
114 open MD5SUM, "| $md5cmd > '$target/$filename.md5sum'" or die "Cannot launch md5sum.\n";
115 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
116 my $buffer;
117 while (read WGET, $buffer, 1048576) {
118 print MD5SUM $buffer;
119 print OUTPUT $buffer;
120 }
121 close MD5SUM;
122 close WGET;
123 close OUTPUT;
124
125 if ($? >> 8) {
126 print STDERR "Download failed.\n";
127 cleanup();
128 return;
129 }
130 }
131
132 my $sum = `cat "$target/$filename.md5sum"`;
133 $sum =~ /^(\w+)\s*/ or die "Could not generate md5sum\n";
134 $sum = $1;
135
136 if (($md5sum =~ /\w{32}/) and ($sum ne $md5sum)) {
137 print STDERR "MD5 sum of the downloaded file does not match (file: $sum, requested: $md5sum) - deleting download.\n";
138 cleanup();
139 return;
140 }
141
142 unlink "$target/$filename";
143 system("mv", "$target/$filename.dl", "$target/$filename");
144 cleanup();
145 }
146
147 sub cleanup
148 {
149 unlink "$target/$filename.dl";
150 unlink "$target/$filename.md5sum";
151 }
152
153 @mirrors = localmirrors();
154
155 foreach my $mirror (@ARGV) {
156 if ($mirror =~ /^\@SF\/(.+)$/) {
157 # give sourceforge a few more tries, because it redirects to different mirrors
158 for (1 .. 5) {
159 push @mirrors, "http://downloads.sourceforge.net/$1";
160 }
161 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
162 push @mirrors, "http://ftpmirror.gnu.org/$1";
163 push @mirrors, "http://ftp.gnu.org/pub/gnu/$1";
164 push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$1";
165 push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$1";
166 push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$1";
167 } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
168 push @mirrors, "http://download.savannah.gnu.org/releases/$1";
169 push @mirrors, "http://nongnu.uib.no/$1";
170 push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
171 push @mirrors, "http://download-mirror.savannah.gnu.org/releases/$1";
172 } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
173 my @extra = ( $1 );
174 if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
175 push @extra, "$extra[0]/testing";
176 } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
177 push @extra, "$extra[0]/longterm/v$1";
178 }
179 foreach my $dir (@extra) {
180 push @mirrors, "https://kernel.org/pub/$dir";
181 push @mirrors, "ftp://kernel.org/pub/$dir";
182 }
183 } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
184 push @mirrors, "http://ftp.gnome.org/pub/GNOME/sources/$1";
185 push @mirrors, "http://ftp.unina.it/pub/linux/GNOME/sources/$1";
186 push @mirrors, "http://fr2.rpmfind.net/linux/gnome.org/sources/$1";
187 push @mirrors, "ftp://ftp.dit.upm.es/pub/GNOME/sources/$1";
188 push @mirrors, "ftp://ftp.no.gnome.org/pub/GNOME/sources/$1";
189 push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
190 push @mirrors, "http://ftp.belnet.be/mirror/ftp.gnome.org/sources/$1";
191 push @mirrors, "http://linorg.usp.br/gnome/sources/$1";
192 push @mirrors, "http://mirror.aarnet.edu.au/pub/GNOME/sources/$1";
193 push @mirrors, "http://mirrors.ibiblio.org/pub/mirrors/gnome/sources/$1";
194 push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
195 push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
196 }
197 else {
198 push @mirrors, $mirror;
199 }
200 }
201
202 #push @mirrors, 'http://mirror1.openwrt.org';
203 push @mirrors, 'http://mirror2.openwrt.org/sources';
204 push @mirrors, 'http://downloads.openwrt.org/sources';
205
206 while (!$ok) {
207 my $mirror = shift @mirrors;
208 $mirror or die "No more mirrors to try - giving up.\n";
209
210 download($mirror);
211 -f "$target/$filename" and $ok = 1;
212 }
213
214 $SIG{INT} = \&cleanup;
215