Refactor downloading code into download.mk
[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 use File::Basename;
12
13 @ARGV > 2 or die "Syntax: $0 <target dir> <filename> <md5sum> [<mirror> ...]\n";
14
15 my $target = shift @ARGV;
16 my $filename = shift @ARGV;
17 my $md5sum = shift @ARGV;
18 my $scriptdir = dirname($0);
19 my @mirrors;
20 my $ok;
21
22 sub localmirrors {
23     my @mlist;
24     open LM, "$scriptdir/localmirrors" and do {
25             while (<LM>) {
26                         chomp $_;
27                         push @mlist, $_;
28                 }
29                 close LM;
30         };
31         open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
32                 while (<CONFIG>) {
33                         /^CONFIG_LOCALMIRROR="(.+)"/ and do {
34                                 chomp;
35                                 push @mlist, $1;
36                         };
37                 }
38                 close CONFIG;
39         };
40         
41
42     return @mlist;
43 }
44
45 sub which($) {
46         my $prog = shift;
47         my $res = `which $prog`;
48         $res or return undef;
49         $res =~ /^no / and return undef;
50         $res =~ /not found/ and return undef;
51         return $res;
52 }
53
54 my $md5cmd = which("md5sum");
55 $md5cmd or $md5cmd = which("md5");
56 $md5cmd or die 'no md5 checksum program found, please install md5 or md5sum';
57 chomp $md5cmd;
58
59 sub download
60 {
61         my $mirror = shift;
62         my $options = $ENV{WGET_OPTIONS};
63         $options or $options = "";
64         
65         $mirror =~ s/\/$//;
66         open WGET, "wget -t1 --timeout=20 $options -O- \"$mirror/$filename\" |" or die "Cannot launch wget.\n";
67         open MD5SUM, "| $md5cmd > \"$target/$filename.md5sum\"" or die "Cannot launch md5sum.\n";
68         open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
69         my $buffer;
70         while (read WGET, $buffer, 1048576) {
71                 print MD5SUM $buffer;
72                 print OUTPUT $buffer;
73         }
74         close MD5SUM;
75         close WGET;
76         close OUTPUT;
77         
78         if (($? >> 8) != 0 ) {
79                 print STDERR "Download failed.\n";
80                 cleanup();
81                 return;
82         }
83         
84         my $sum = `cat "$target/$filename.md5sum"`;
85         $sum =~ /^(\w+)\s*/ or die "Could not generate md5sum\n";
86         $sum = $1;
87         
88         if (($md5sum =~ /\w{32}/) and ($sum ne $md5sum)) {
89                 print STDERR "MD5 sum of the downloaded file does not match - deleting download.\n";
90                 cleanup();
91                 return;
92         }
93         
94         unlink "$target/$filename";
95         system("mv \"$target/$filename.dl\" \"$target/$filename\"");
96         cleanup();
97 }
98
99 sub cleanup
100 {
101         unlink "$target/$filename.dl";
102         unlink "$target/$filename.md5sum";
103 }
104
105 @mirrors = localmirrors();
106
107 foreach my $mirror (@ARGV) {
108         if ($mirror =~ /^\@SF\/(.+)$/) {
109                 # give sourceforge a few more tries, because it redirects to different mirrors
110                 for (1 .. 5) {
111                         push @mirrors, "http://downloads.sourceforge.net/$1";
112                 }
113         } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
114                 push @mirrors, "ftp://ftp.gnu.org/gnu/$1";
115                 push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$1";
116                 push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$1";
117                 push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$1";
118                 push @mirrors, "ftp://ftp.uu.net/archive/systems/gnu/$1";
119                 push @mirrors, "ftp://ftp.eu.uu.net/pub/gnu/$1";
120                 push @mirrors, "ftp://ftp.leo.org/pub/comp/os/unix/gnu/$1";
121                 push @mirrors, "ftp://ftp.digex.net/pub/gnu/$1";
122         } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
123                 push @mirrors, "ftp://ftp.us.kernel.org/pub/$1";
124                 push @mirrors, "http://ftp.us.kernel.org/pub/$1";
125                 push @mirrors, "ftp://ftp.kernel.org/pub/$1";
126                 push @mirrors, "http://ftp.kernel.org/pub/$1";
127                 push @mirrors, "ftp://ftp.de.kernel.org/pub/$1";
128                 push @mirrors, "http://ftp.de.kernel.org/pub/$1";
129                 push @mirrors, "ftp://ftp.fr.kernel.org/pub/$1";
130                 push @mirrors, "http://ftp.fr.kernel.org/pub/$1";
131         } else {
132                 push @mirrors, $mirror;
133         }
134 }
135
136 #push @mirrors, 'http://mirror1.openwrt.org';
137 push @mirrors, 'http://mirror2.openwrt.org/sources';
138 push @mirrors, 'http://downloads.openwrt.org/sources';
139
140 while (!$ok) {
141         my $mirror = shift @mirrors;
142         $mirror or die "No more mirrors to try - giving up.\n";
143         
144         download($mirror);
145         -f "$target/$filename" and $ok = 1;
146 }
147
148 $SIG{INT} = \&cleanup;
149