| 1 | #!/usr/bin/perl
|
|---|
| 2 | # $FreeWRT$
|
|---|
| 3 | #-
|
|---|
| 4 | # This file is part of the FreeWRT project. FreeWRT is copyrighted
|
|---|
| 5 | # material, please see the LICENCE file in the top-level directory
|
|---|
| 6 | # or at http://www.freewrt.org/licence for details.
|
|---|
| 7 |
|
|---|
| 8 | use strict;
|
|---|
| 9 | use warnings;
|
|---|
| 10 |
|
|---|
| 11 | my $target = shift @ARGV;
|
|---|
| 12 | my $filename = shift @ARGV;
|
|---|
| 13 | my $md5sum = shift @ARGV;
|
|---|
| 14 | my @mirrors;
|
|---|
| 15 |
|
|---|
| 16 | my $ok;
|
|---|
| 17 |
|
|---|
| 18 | @ARGV > 0 or die "Syntax: $0 <target dir> <filename> <md5sum> <mirror> [<mirror> ...]\n";
|
|---|
| 19 |
|
|---|
| 20 | sub download
|
|---|
| 21 | {
|
|---|
| 22 | my $mirror = shift;
|
|---|
| 23 |
|
|---|
| 24 | open WGET, "wget -t1 --timeout=20 -O- \"$mirror/$filename\" |" or die "Cannot launch wget.\n";
|
|---|
| 25 | open MD5SUM, "| md5sum > \"$target/$filename.md5sum\"" or die "Cannot launch md5sum.\n";
|
|---|
| 26 | open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
|
|---|
| 27 | my $buffer;
|
|---|
| 28 | while (read WGET, $buffer, 1048576) {
|
|---|
| 29 | print MD5SUM $buffer;
|
|---|
| 30 | print OUTPUT $buffer;
|
|---|
| 31 | }
|
|---|
| 32 | close MD5SUM;
|
|---|
| 33 | close WGET;
|
|---|
| 34 | close OUTPUT;
|
|---|
| 35 |
|
|---|
| 36 | if (($? >> 8) != 0 ) {
|
|---|
| 37 | print STDERR "Download failed.\n";
|
|---|
| 38 | cleanup();
|
|---|
| 39 | return;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | my $sum = `cat "$target/$filename.md5sum"`;
|
|---|
| 43 | $sum =~ /^(\w+)\s+/ or die "Could not generate md5sum\n";
|
|---|
| 44 | $sum = $1;
|
|---|
| 45 |
|
|---|
| 46 | if ($md5sum ne 'NO') {
|
|---|
| 47 | if ($sum ne $md5sum) {
|
|---|
| 48 | print STDERR "MD5 sum of the downloaded file does not match - deleting download.\n";
|
|---|
| 49 | cleanup();
|
|---|
| 50 | return;
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | unlink "$target/$filename";
|
|---|
| 55 | system("mv \"$target/$filename.dl\" \"$target/$filename\"");
|
|---|
| 56 | cleanup();
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | sub cleanup
|
|---|
| 60 | {
|
|---|
| 61 | unlink "$target/$filename.dl";
|
|---|
| 62 | unlink "$target/$filename.md5sum";
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | foreach my $mirror (@ARGV) {
|
|---|
| 66 | if ($mirror =~ /^\@SF\/(.+)$/) {
|
|---|
| 67 | my $sfpath = $1;
|
|---|
| 68 | push @mirrors, "http://easynews.dl.sourceforge.net/sourceforge/$sfpath";
|
|---|
| 69 | push @mirrors, "http://puzzle.dl.sourceforge.net/sourceforge/$sfpath";
|
|---|
| 70 | push @mirrors, "http://optusnet.dl.sourceforge.net/sourceforge/$sfpath";
|
|---|
| 71 | push @mirrors, "http://heanet.dl.sourceforge.net/sourceforge/$sfpath";
|
|---|
| 72 | push @mirrors, "http://jaist.dl.sourceforge.net/sourceforge/$sfpath";
|
|---|
| 73 | push @mirrors, "http://nchc.dl.sourceforge.net/sourceforge/$sfpath";
|
|---|
| 74 | push @mirrors, "http://switch.dl.sourceforge.net/sourceforge/$sfpath";
|
|---|
| 75 | push @mirrors, "http://kent.dl.sourceforge.net/sourceforge/$sfpath";
|
|---|
| 76 | push @mirrors, "http://internap.dl.sourceforge.net/sourceforge/$sfpath";
|
|---|
| 77 | push @mirrors, "http://mesh.dl.sourceforge.net/sourceforge/$sfpath";
|
|---|
| 78 | push @mirrors, "http://ovh.dl.sourceforge.net/sourceforge/$sfpath";
|
|---|
| 79 | push @mirrors, "http://surfnet.dl.sourceforge.net/sourceforge/$sfpath";
|
|---|
| 80 | push @mirrors, "http://ufpr.dl.sourceforge.net/sourceforge/$sfpath";
|
|---|
| 81 | push @mirrors, "http://dl.sourceforge.net/sourceforge/$sfpath";
|
|---|
| 82 | } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
|
|---|
| 83 | my $gnupath = $1;
|
|---|
| 84 | push @mirrors, "ftp://ftp.gnu.org/gnu/$gnupath";
|
|---|
| 85 | push @mirrors, "ftp://ftp.belnet.be/mirror/ftp.gnu.org/gnu/$gnupath";
|
|---|
| 86 | push @mirrors, "ftp://ftp.mirror.nl/pub/mirror/gnu/$gnupath";
|
|---|
| 87 | push @mirrors, "http://mirror.switch.ch/ftp/mirror/gnu/$gnupath";
|
|---|
| 88 | push @mirrors, "ftp://ftp.uu.net/archive/systems/gnu/$gnupath";
|
|---|
| 89 | push @mirrors, "ftp://ftp.eu.uu.net/pub/gnu/$gnupath";
|
|---|
| 90 | push @mirrors, "ftp://ftp.leo.org/pub/comp/os/unix/gnu/$gnupath";
|
|---|
| 91 | push @mirrors, "ftp://ftp.digex.net/pub/gnu/$gnupath";
|
|---|
| 92 | } else {
|
|---|
| 93 | push @mirrors, $mirror;
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | push @mirrors, 'http://www.freewrt.org/distfiles/';
|
|---|
| 98 |
|
|---|
| 99 | while (!$ok) {
|
|---|
| 100 | my $mirror = shift @mirrors;
|
|---|
| 101 | $mirror or die "No more mirrors to try - giving up.\n";
|
|---|
| 102 |
|
|---|
| 103 | download($mirror);
|
|---|
| 104 | -f "$target/$filename" and $ok = 1;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | $SIG{INT} = \&cleanup;
|
|---|
| 108 |
|
|---|