fix pcmcia-cs install (remove previous PKG_INSTALL_DIR)
[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 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 my $options = $ENV{WGET_OPTIONS};
39 $options or $options = "";
40
41 $mirror =~ s/\/$//;
42 open WGET, "wget -t1 --timeout=20 $options -O- \"$mirror/$filename\" |" or die "Cannot launch wget.\n";
43 open MD5SUM, "| $md5cmd > \"$target/$filename.md5sum\"" or die "Cannot launch md5sum.\n";
44 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
45 my $buffer;
46 while (read WGET, $buffer, 1048576) {
47 print MD5SUM $buffer;
48 print OUTPUT $buffer;
49 }
50 close MD5SUM;
51 close WGET;
52 close OUTPUT;
53
54 if (($? >> 8) != 0 ) {
55 print STDERR "Download failed.\n";
56 cleanup();
57 return;
58 }
59
60 my $sum = `cat "$target/$filename.md5sum"`;
61 $sum =~ /^(\w+)\s*/ or die "Could not generate md5sum\n";
62 $sum = $1;
63
64 if (($md5sum =~ /\w{32}/) and ($sum ne $md5sum)) {
65 print STDERR "MD5 sum of the downloaded file does not match - deleting download.\n";
66 cleanup();
67 return;
68 }
69
70 unlink "$target/$filename";
71 system("mv \"$target/$filename.dl\" \"$target/$filename\"");
72 cleanup();
73 }
74
75 sub cleanup
76 {
77 unlink "$target/$filename.dl";
78 unlink "$target/$filename.md5sum";
79 }
80
81 foreach my $mirror (@ARGV) {
82 if ($mirror =~ /^\@SF\/(.+)$/) {
83 # give sourceforge a few more tries, because it redirects to different mirrors
84 for (1 .. 5) {
85 push @mirrors, "http://downloads.sourceforge.net/$1";
86 }
87 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
88 my $gnupath = $1;
89 push @mirrors, "ftp://ftp.gnu.org/gnu/$gnupath";
90 push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$gnupath";
91 push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$gnupath";
92 push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$gnupath";
93 push @mirrors, "ftp://ftp.uu.net/archive/systems/gnu/$gnupath";
94 push @mirrors, "ftp://ftp.eu.uu.net/pub/gnu/$gnupath";
95 push @mirrors, "ftp://ftp.leo.org/pub/comp/os/unix/gnu/$gnupath";
96 push @mirrors, "ftp://ftp.digex.net/pub/gnu/$gnupath";
97 } else {
98 push @mirrors, $mirror;
99 }
100 }
101
102 #push @mirrors, 'http://mirror1.openwrt.org';
103 push @mirrors, 'http://mirror2.openwrt.org/sources';
104 push @mirrors, 'http://downloads.openwrt.org/sources';
105
106 while (!$ok) {
107 my $mirror = shift @mirrors;
108 $mirror or die "No more mirrors to try - giving up.\n";
109
110 download($mirror);
111 -f "$target/$filename" and $ok = 1;
112 }
113
114 $SIG{INT} = \&cleanup;
115
This page took 0.052043 seconds and 5 git commands to generate.