allow arbitary folder layout when using localmirrors
[openwrt/staging/dedeckeh.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 return @mlist;
44 }
45
46 sub which($) {
47 my $prog = shift;
48 my $res = `which $prog`;
49 $res or return undef;
50 $res =~ /^no / and return undef;
51 $res =~ /not found/ and return undef;
52 return $res;
53 }
54
55 my $md5cmd = which("md5sum");
56 $md5cmd or $md5cmd = which("md5");
57 $md5cmd or die 'no md5 checksum program found, please install md5 or md5sum';
58 chomp $md5cmd;
59
60 sub download
61 {
62 my $mirror = shift;
63 my $options = $ENV{WGET_OPTIONS};
64 $options or $options = "";
65
66 $mirror =~ s/\/$//;
67 if( $mirror =~ /^file:\/\// ) {
68 my $cache = $mirror;
69 $cache =~ s/file:\/\///g;
70 if(system("test -d $cache")) {
71 print STDERR "Wrong local cache directory -$cache-.\n";
72 cleanup();
73 return;
74 }
75 if(! -d $target) {
76 system("mkdir -p $target/");
77 }
78 if (open TMPDLS, "find $cache -follow -name $filename 2>/dev/null |"){
79 my $i = 0;
80 my $link = "";
81 while (defined($link = readline TMPDLS)) {
82 chomp $link;
83 $i++;
84 if ($i > 1) {
85 print("$i or more instances of $filename in $cache found . Only one instance allowed.\n");
86 return;
87 }
88 }
89 close TMPDLS;
90 if ($i < 1) {
91 print("No instances of $filename found in $cache.\n");
92 return;
93 } elsif ($i == 1){
94 print("Copying $filename from $link\n");
95 copy($link, "$target/$filename.dl");
96 }
97 } else {
98 print("Failed to search for $filename in $cache\n");
99 return;
100 }
101 system("$md5cmd $target/$filename.dl > \"$target/$filename.md5sum\" ") == 0 or return;
102 } else {
103 open WGET, "wget -t5 --timeout=20 --no-check-certificate $options -O- \"$mirror/$filename\" |" or die "Cannot launch wget.\n";
104 open MD5SUM, "| $md5cmd > \"$target/$filename.md5sum\"" or die "Cannot launch md5sum.\n";
105 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
106 my $buffer;
107 while (read WGET, $buffer, 1048576) {
108 print MD5SUM $buffer;
109 print OUTPUT $buffer;
110 }
111 close MD5SUM;
112 close WGET;
113 close OUTPUT;
114
115 if (($? >> 8) != 0 ) {
116 print STDERR "Download failed.\n";
117 cleanup();
118 return;
119 }
120 }
121
122 my $sum = `cat "$target/$filename.md5sum"`;
123 $sum =~ /^(\w+)\s*/ or die "Could not generate md5sum\n";
124 $sum = $1;
125
126 if (($md5sum =~ /\w{32}/) and ($sum ne $md5sum)) {
127 print STDERR "MD5 sum of the downloaded file does not match (file: $sum, requested: $md5sum) - deleting download.\n";
128 cleanup();
129 return;
130 }
131
132 unlink "$target/$filename";
133 system("mv \"$target/$filename.dl\" \"$target/$filename\"");
134 cleanup();
135 }
136
137 sub cleanup
138 {
139 unlink "$target/$filename.dl";
140 unlink "$target/$filename.md5sum";
141 }
142
143 @mirrors = localmirrors();
144
145 foreach my $mirror (@ARGV) {
146 if ($mirror =~ /^\@SF\/(.+)$/) {
147 # give sourceforge a few more tries, because it redirects to different mirrors
148 for (1 .. 5) {
149 push @mirrors, "http://downloads.sourceforge.net/$1";
150 }
151 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
152 push @mirrors, "ftp://ftp.gnu.org/gnu/$1";
153 push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$1";
154 push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$1";
155 push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$1";
156 push @mirrors, "ftp://ftp.uu.net/archive/systems/gnu/$1";
157 push @mirrors, "ftp://ftp.eu.uu.net/pub/gnu/$1";
158 push @mirrors, "ftp://ftp.leo.org/pub/comp/os/unix/gnu/$1";
159 push @mirrors, "ftp://ftp.digex.net/pub/gnu/$1";
160 } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
161 my @extra = ( $1 );
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";
166 }
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";
174 }
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";
188 }
189 else {
190 push @mirrors, $mirror;
191 }
192 }
193
194 #push @mirrors, 'http://mirror1.openwrt.org';
195 push @mirrors, 'http://mirror2.openwrt.org/sources';
196 push @mirrors, 'http://downloads.openwrt.org/sources';
197
198 while (!$ok) {
199 my $mirror = shift @mirrors;
200 $mirror or die "No more mirrors to try - giving up.\n";
201
202 download($mirror);
203 -f "$target/$filename" and $ok = 1;
204 }
205
206 $SIG{INT} = \&cleanup;
207