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