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