fix some warnings
[openwrt/crisos.git] / scripts / download.pl
blobe63595497f79591a1e0b76fda80424cc11e7c429
1 #!/usr/bin/env perl
2 #
3 # Copyright (C) 2006 OpenWrt.org
5 # This is free software, licensed under the GNU General Public License v2.
6 # See /LICENSE for more information.
9 use strict;
10 use warnings;
11 use File::Basename;
13 @ARGV > 2 or die "Syntax: $0 <target dir> <filename> <md5sum> [<mirror> ...]\n";
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;
22 sub localmirrors {
23 my @mlist;
24 open LM, "$scriptdir/localmirrors" and do {
25 while (<LM>) {
26 chomp $_;
27 push @mlist, $_;
29 close LM;
31 open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
32 while (<CONFIG>) {
33 /^CONFIG_LOCALMIRROR="(.+)"/ and do {
34 chomp;
35 push @mlist, $1;
38 close CONFIG;
41 return @mlist;
44 sub which($) {
45 my $prog = shift;
46 my $res = `which $prog`;
47 $res or return undef;
48 $res =~ /^no / and return undef;
49 $res =~ /not found/ and return undef;
50 return $res;
53 my $md5cmd = which("md5sum");
54 $md5cmd or $md5cmd = which("md5");
55 $md5cmd or die 'no md5 checksum program found, please install md5 or md5sum';
56 chomp $md5cmd;
58 sub download
60 my $mirror = shift;
61 my $options = $ENV{WGET_OPTIONS};
62 $options or $options = "";
64 $mirror =~ s/\/$//;
65 if( $mirror =~ /^file:\/\// ) {
66 my $cache = $mirror;
67 $cache =~ s/file:\/\///g;
68 print "Checking local cache: $cache\n";
69 system("mkdir -p $target/");
70 system("cp -f $cache/$filename $target/$filename.dl") == 0 or return;
71 system("$md5cmd $target/$filename.dl > \"$target/$filename.md5sum\" ") == 0 or return;
72 } else {
73 open WGET, "wget -t5 --timeout=20 $options -O- \"$mirror/$filename\" |" or die "Cannot launch wget.\n";
74 open MD5SUM, "| $md5cmd > \"$target/$filename.md5sum\"" or die "Cannot launch md5sum.\n";
75 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
76 my $buffer;
77 while (read WGET, $buffer, 1048576) {
78 print MD5SUM $buffer;
79 print OUTPUT $buffer;
81 close MD5SUM;
82 close WGET;
83 close OUTPUT;
85 if (($? >> 8) != 0 ) {
86 print STDERR "Download failed.\n";
87 cleanup();
88 return;
92 my $sum = `cat "$target/$filename.md5sum"`;
93 $sum =~ /^(\w+)\s*/ or die "Could not generate md5sum\n";
94 $sum = $1;
96 if (($md5sum =~ /\w{32}/) and ($sum ne $md5sum)) {
97 print STDERR "MD5 sum of the downloaded file does not match (file: $sum, requested: $md5sum) - deleting download.\n";
98 cleanup();
99 return;
102 unlink "$target/$filename";
103 system("mv \"$target/$filename.dl\" \"$target/$filename\"");
104 cleanup();
107 sub cleanup
109 unlink "$target/$filename.dl";
110 unlink "$target/$filename.md5sum";
113 @mirrors = localmirrors();
115 foreach my $mirror (@ARGV) {
116 if ($mirror =~ /^\@SF\/(.+)$/) {
117 # give sourceforge a few more tries, because it redirects to different mirrors
118 for (1 .. 5) {
119 push @mirrors, "http://downloads.sourceforge.net/$1";
121 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
122 push @mirrors, "ftp://ftp.gnu.org/gnu/$1";
123 push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$1";
124 push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$1";
125 push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$1";
126 push @mirrors, "ftp://ftp.uu.net/archive/systems/gnu/$1";
127 push @mirrors, "ftp://ftp.eu.uu.net/pub/gnu/$1";
128 push @mirrors, "ftp://ftp.leo.org/pub/comp/os/unix/gnu/$1";
129 push @mirrors, "ftp://ftp.digex.net/pub/gnu/$1";
130 } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
131 push @mirrors, "ftp://ftp.all.kernel.org/pub/$1";
132 push @mirrors, "http://ftp.all.kernel.org/pub/$1";
133 push @mirrors, "ftp://ftp.de.kernel.org/pub/$1";
134 push @mirrors, "http://ftp.de.kernel.org/pub/$1";
135 push @mirrors, "ftp://ftp.fr.kernel.org/pub/$1";
136 push @mirrors, "http://ftp.fr.kernel.org/pub/$1";
137 } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
138 push @mirrors, "http://ftp.gnome.org/pub/GNOME/sources/$1";
139 push @mirrors, "http://ftp.unina.it/pub/linux/GNOME/sources/$1";
140 push @mirrors, "http://fr2.rpmfind.net/linux/gnome.org/sources/$1";
141 push @mirrors, "ftp://ftp.dit.upm.es/pub/GNOME/sources/$1";
142 push @mirrors, "ftp://ftp.no.gnome.org/pub/GNOME/sources/$1";
143 push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
144 push @mirrors, "http://ftp.belnet.be/mirror/ftp.gnome.org/sources/$1";
145 push @mirrors, "http://linorg.usp.br/gnome/sources/$1";
146 push @mirrors, "http://mirror.aarnet.edu.au/pub/GNOME/sources/$1";
147 push @mirrors, "http://mirrors.ibiblio.org/pub/mirrors/gnome/sources/$1";
148 push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
149 push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
151 else {
152 push @mirrors, $mirror;
156 #push @mirrors, 'http://mirror1.openwrt.org';
157 push @mirrors, 'http://mirror2.openwrt.org/sources';
158 push @mirrors, 'http://downloads.openwrt.org/sources';
160 while (!$ok) {
161 my $mirror = shift @mirrors;
162 $mirror or die "No more mirrors to try - giving up.\n";
164 download($mirror);
165 -f "$target/$filename" and $ok = 1;
168 $SIG{INT} = \&cleanup;