3 # Copyright (C) 2006 OpenWrt.org
5 # This is free software, licensed under the GNU General Public License v2.
6 # See /LICENSE for more information.
14 @ARGV > 2 or die "Syntax: $0 <target dir> <filename> <hash> <url filename> [<mirror> ...]\n";
17 my $target = shift @ARGV;
18 my $filename = shift @ARGV;
19 my $file_hash = shift @ARGV;
20 $url_filename = shift @ARGV unless $ARGV[0] =~ /:\/\//;
21 my $scriptdir = dirname($0);
25 $url_filename or $url_filename = $filename;
29 open LM, "$scriptdir/localmirrors" and do {
32 push @mlist, $_ if $_;
36 open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
38 /^CONFIG_LOCALMIRROR="(.+)"/ and do {
40 my @local_mirrors = split(/;/, $1);
41 push @mlist, @local_mirrors;
47 my $mirror = $ENV{'DOWNLOAD_MIRROR'};
48 $mirror and push @mlist, split(/;/, $mirror);
55 my $res = `which $prog`;
57 $res =~ /^no / and return undef;
58 $res =~ /not found/ and return undef;
63 my $len = length($file_hash);
66 $len == 64 and return "openssl dgst -sha256 | sed -e 's,.*= ,,'";
68 my $cmd = which("md5sum") || which("md5") || die 'no md5 checksum program found, please install md5 or md5sum';
75 my $hash_cmd = hash_cmd();
80 my $options = $ENV{WGET_OPTIONS} || "";
84 if ($mirror =~ s!^file://!!) {
86 print STDERR "Wrong local cache directory -$mirror-.\n";
92 system("mkdir", "-p", "$target/");
95 if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
96 print("Failed to search for $filename in $mirror\n");
102 while (defined(my $line = readline TMPDLS)) {
103 chomp ($link = $line);
105 print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
113 print("No instances of $filename found in $mirror.\n");
117 print("Copying $filename from $link\n");
118 copy($link, "$target/$filename.dl");
121 if (system("$hash_cmd '$target/$filename.dl' > '$target/$filename.hash'")) {
122 print("Failed to generate hash for $filename\n");
127 open WGET, "wget -t5 --timeout=20 --no-check-certificate $options -O- '$mirror/$url_filename' |" or die "Cannot launch wget.\n";
129 open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
131 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
133 while (read WGET, $buffer, 1048576) {
134 $hash_cmd and print MD5SUM $buffer;
135 print OUTPUT $buffer;
137 $hash_cmd and close MD5SUM;
142 print STDERR "Download failed.\n";
149 my $sum = `cat "$target/$filename.hash"`;
150 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
153 if ($sum ne $file_hash) {
154 print STDERR "MD5 sum of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
160 unlink "$target/$filename";
161 system("mv", "$target/$filename.dl", "$target/$filename");
167 unlink "$target/$filename.dl";
168 unlink "$target/$filename.hash";
171 @mirrors = localmirrors();
173 foreach my $mirror (@ARGV) {
174 if ($mirror =~ /^\@SF\/(.+)$/) {
175 # give sourceforge a few more tries, because it redirects to different mirrors
177 push @mirrors, "http://downloads.sourceforge.net/$1";
179 } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
180 push @mirrors, "http://ftp.tudelft.nl/apache/$1";
181 push @mirrors, "http://apache.openmirror.de/$1";
182 push @mirrors, "http://mirrors.ocf.berkeley.edu/apache/$1";
183 push @mirrors, "http://mirror.cc.columbia.edu/pub/software/apache/$1";
184 push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
185 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
186 push @mirrors, "http://ftpmirror.gnu.org/$1";
187 push @mirrors, "http://ftp.gnu.org/pub/gnu/$1";
188 push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$1";
189 push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$1";
190 push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$1";
191 } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
192 push @mirrors, "http://download.savannah.gnu.org/releases/$1";
193 push @mirrors, "http://nongnu.uib.no/$1";
194 push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
195 push @mirrors, "http://download-mirror.savannah.gnu.org/releases/$1";
196 } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
198 if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
199 push @extra, "$extra[0]/testing";
200 } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
201 push @extra, "$extra[0]/longterm/v$1";
203 foreach my $dir (@extra) {
204 push @mirrors, "https://kernel.org/pub/$dir";
205 push @mirrors, "ftp://kernel.org/pub/$dir";
207 } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
208 push @mirrors, "http://ftp.gnome.org/pub/GNOME/sources/$1";
209 push @mirrors, "http://www.mirrorservice.org/sites/ftp.gnome.org/pub/GNOME/sources/$1";
210 push @mirrors, "http://fr2.rpmfind.net/linux/gnome.org/sources/$1";
211 push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
212 push @mirrors, "http://ftp.belnet.be/ftp.gnome.org/sources/$1";
213 push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
214 push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
217 push @mirrors, $mirror;
221 #push @mirrors, 'http://mirror1.openwrt.org';
222 push @mirrors, 'http://mirror2.openwrt.org/sources';
223 push @mirrors, 'http://downloads.openwrt.org/sources';
226 my $mirror = shift @mirrors;
227 $mirror or die "No more mirrors to try - giving up.\n";
230 -f "$target/$filename" and $ok = 1;
233 $SIG{INT} = \&cleanup;