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