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