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> <md5sum> [<mirror> ...]\n";
16 my $target = shift @ARGV;
17 my $filename = shift @ARGV;
18 my $md5sum = shift @ARGV;
19 my $scriptdir = dirname($0);
25 open LM, "$scriptdir/localmirrors" and do {
28 push @mlist, $_ if $_;
32 open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
34 /^CONFIG_LOCALMIRROR="(.+)"/ and do {
36 my @local_mirrors = split(/;/, $1);
37 push @mlist, @local_mirrors;
48 my $res = `which $prog`;
50 $res =~ /^no / and return undef;
51 $res =~ /not found/ and return undef;
55 my $md5cmd = which("md5sum") || which("md5") || die 'no md5 checksum program found, please install md5 or md5sum';
61 my $options = $ENV{WGET_OPTIONS} || "";
65 if ($mirror =~ s!^file://!!) {
67 print STDERR "Wrong local cache directory -$mirror-.\n";
73 system("mkdir", "-p", "$target/");
76 if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
77 print("Failed to search for $filename in $mirror\n");
83 while (defined(my $line = readline TMPDLS)) {
84 chomp ($link = $line);
86 print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
94 print("No instances of $filename found in $mirror.\n");
98 print("Copying $filename from $link\n");
99 copy($link, "$target/$filename.dl");
101 if (system("$md5cmd '$target/$filename.dl' > '$target/$filename.md5sum'")) {
102 print("Failed to generate md5 sum for $filename\n");
106 open WGET, "wget -t5 --timeout=20 --no-check-certificate $options -O- '$mirror/$filename' |" or die "Cannot launch wget.\n";
107 open MD5SUM, "| $md5cmd > '$target/$filename.md5sum'" or die "Cannot launch md5sum.\n";
108 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
110 while (read WGET, $buffer, 1048576) {
111 print MD5SUM $buffer;
112 print OUTPUT $buffer;
119 print STDERR "Download failed.\n";
125 my $sum = `cat "$target/$filename.md5sum"`;
126 $sum =~ /^(\w+)\s*/ or die "Could not generate md5sum\n";
129 if (($md5sum =~ /\w{32}/) and ($sum ne $md5sum)) {
130 print STDERR "MD5 sum of the downloaded file does not match (file: $sum, requested: $md5sum) - deleting download.\n";
135 unlink "$target/$filename";
136 system("mv", "$target/$filename.dl", "$target/$filename");
142 unlink "$target/$filename.dl";
143 unlink "$target/$filename.md5sum";
146 @mirrors = localmirrors();
148 foreach my $mirror (@ARGV) {
149 if ($mirror =~ /^\@SF\/(.+)$/) {
150 # give sourceforge a few more tries, because it redirects to different mirrors
152 push @mirrors, "http://downloads.sourceforge.net/$1";
154 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
155 push @mirrors, "http://ftp.gnu.org/pub/gnu/$1";
156 push @mirrors, "http://ftpmirror.gnu.org/$1";
157 push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$1";
158 push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$1";
159 push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$1";
160 } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
162 if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
163 push @extra, "$extra[0]/testing";
164 } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
165 push @extra, "$extra[0]/longterm/v$1";
167 foreach my $dir (@extra) {
168 push @mirrors, "ftp://ftp.all.kernel.org/pub/$dir";
169 push @mirrors, "http://ftp.all.kernel.org/pub/$dir";
170 push @mirrors, "ftp://ftp.de.kernel.org/pub/$dir";
171 push @mirrors, "http://ftp.de.kernel.org/pub/$dir";
172 push @mirrors, "ftp://ftp.fr.kernel.org/pub/$dir";
173 push @mirrors, "http://ftp.fr.kernel.org/pub/$dir";
175 } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
176 push @mirrors, "http://ftp.gnome.org/pub/GNOME/sources/$1";
177 push @mirrors, "http://ftp.unina.it/pub/linux/GNOME/sources/$1";
178 push @mirrors, "http://fr2.rpmfind.net/linux/gnome.org/sources/$1";
179 push @mirrors, "ftp://ftp.dit.upm.es/pub/GNOME/sources/$1";
180 push @mirrors, "ftp://ftp.no.gnome.org/pub/GNOME/sources/$1";
181 push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
182 push @mirrors, "http://ftp.belnet.be/mirror/ftp.gnome.org/sources/$1";
183 push @mirrors, "http://linorg.usp.br/gnome/sources/$1";
184 push @mirrors, "http://mirror.aarnet.edu.au/pub/GNOME/sources/$1";
185 push @mirrors, "http://mirrors.ibiblio.org/pub/mirrors/gnome/sources/$1";
186 push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
187 push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
190 push @mirrors, $mirror;
194 #push @mirrors, 'http://mirror1.openwrt.org';
195 push @mirrors, 'http://mirror2.openwrt.org/sources';
196 push @mirrors, 'http://downloads.openwrt.org/sources';
199 my $mirror = shift @mirrors;
200 $mirror or die "No more mirrors to try - giving up.\n";
203 -f "$target/$filename" and $ok = 1;
206 $SIG{INT} = \&cleanup;