Added wccpd a Web Cache Coordination Protocol daemon
[openwrt.git] / openwrt / scripts / download.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 my $target = shift @ARGV;
6 my $filename = shift @ARGV;
7 my $md5sum = shift @ARGV;
8 my @mirrors;
9
10 my $ok;
11
12 @ARGV > 0 or die "Syntax: $0 <target dir> <filename> <md5sum> <mirror> [<mirror> ...]\n";
13
14 sub download
15 {
16 my $mirror = shift;
17
18 open WGET, "wget -t1 --timeout=20 -O- \"$mirror/$filename\" |" or die "Cannot launch wget.\n";
19 open MD5SUM, "| md5sum > \"$target/$filename.md5sum\"" or die "Cannot launch md5sum.\n";
20 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
21 my $buffer;
22 while (read WGET, $buffer, 1048576) {
23 print MD5SUM $buffer;
24 print OUTPUT $buffer;
25 }
26 close MD5SUM;
27 close WGET;
28 close OUTPUT;
29
30 if (($? >> 8) != 0 ) {
31 print STDERR "Download failed.\n";
32 cleanup();
33 return;
34 }
35
36 my $sum = `cat "$target/$filename.md5sum"`;
37 $sum =~ /^(\w+)\s+/ or die "Could not generate md5sum\n";
38 $sum = $1;
39
40 if (($md5sum =~ /\w{32}/) and ($sum ne $md5sum)) {
41 print STDERR "MD5 sum of the downloaded file does not match - deleting download.\n";
42 cleanup();
43 return;
44 }
45
46 unlink "$target/$filename";
47 system("mv \"$target/$filename.dl\" \"$target/$filename\"");
48 cleanup();
49 }
50
51 sub cleanup
52 {
53 unlink "$target/$filename.dl";
54 unlink "$target/$filename.md5sum";
55 }
56
57 foreach my $mirror (@ARGV) {
58 if ($mirror =~ /^\@SF\/(.+)$/) {
59 my $sfpath = $1;
60 open SF, "wget -t1 -q -O- 'http://prdownloads.sourceforge.net/$sfpath/$filename' |";
61 while (<SF>) {
62 /type="radio" name="use_default" value="(\w+)" onclick="form\.submit\(\)"\/>/ and do {
63 push @mirrors, "http://$1.dl.sourceforge.net/sourceforge/$sfpath";
64 };
65 }
66 push @mirrors, "http://dl.sourceforge.net/sourceforge/$sfpath";
67 close SF;
68 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
69 my $gnupath = $1;
70 push @mirrors, "ftp://ftp.gnu.org/gnu/$gnupath";
71 push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$gnupath";
72 push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$gnupath";
73 push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$gnupath";
74 push @mirrors, "ftp://ftp.uu.net/archive/systems/gnu/$gnupath";
75 push @mirrors, "ftp://ftp.eu.uu.net/pub/gnu/$gnupath";
76 push @mirrors, "ftp://ftp.leo.org/pub/comp/os/unix/gnu/$gnupath";
77 push @mirrors, "ftp://ftp.digex.net/pub/gnu/$gnupath";
78 } else {
79 push @mirrors, $mirror;
80 }
81 }
82
83 #push @mirrors, 'http://mirror1.openwrt.org/';
84 #push @mirrors, 'http://mirror2.openwrt.org/';
85 push @mirrors, 'http://downloads.openwrt.org/sources/';
86
87 while (!$ok) {
88 my $mirror = shift @mirrors;
89 $mirror or die "No more mirrors to try - giving up.\n";
90
91 download($mirror);
92 -f "$target/$filename" and $ok = 1;
93 }
94
95 $SIG{INT} = \&cleanup;
96
This page took 0.059337 seconds and 5 git commands to generate.