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