source: freewrt/scripts/download.pl@ 1954439

freewrt_2_0
Last change on this file since 1954439 was 1954439, checked in by mirabilos <m$(date +%Y)@…>, 4 months ago

prefer the beefier mirror over ex-flyingfish

  • Property mode set to 100755
File size: 2.2 KB
Line 
1#!/usr/bin/perl
2use strict;
3use warnings;
4
5my $target = shift @ARGV;
6my $filename = shift @ARGV;
7my $md5sum = shift @ARGV;
8my @mirrors;
9
10my $ok;
11
12@ARGV > 0 or die "Syntax: $0 <target dir> <filename> <md5sum> <mirror> [<mirror> ...]\n";
13
14sub download
15{
16 my $mirror = shift;
17
18 open WGET, "wget -t1 --timeout=20 -O- \"$mirror/$filename\" |" or die "Cannot launch wget.\n";
19 open MD5SUM, "| md5sum > \"$target/$filename.md5sum\"" or die "Cannot launch md5sum.\n";
20 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
21 my $buffer;
22 while (read WGET, $buffer, 1048576) {
23 print MD5SUM $buffer;
24 print OUTPUT $buffer;
25 }
26 close MD5SUM;
27 close WGET;
28 close OUTPUT;
29
30 if (($? >> 8) != 0 ) {
31 print STDERR "Download failed.\n";
32 cleanup();
33 return;
34 }
35
36 my $sum = `cat "$target/$filename.md5sum"`;
37 $sum =~ /^(\w+)\s+/ or die "Could not generate md5sum\n";
38 $sum = $1;
39
40 if ($sum ne $md5sum) {
41 print STDERR "MD5 sum of the downloaded file does not match - deleting download.\n";
42 cleanup();
43 return;
44 }
45
46 unlink "$target/$filename";
47 system("mv \"$target/$filename.dl\" \"$target/$filename\"");
48 cleanup();
49}
50
51sub cleanup
52{
53 unlink "$target/$filename.dl";
54 unlink "$target/$filename.md5sum";
55}
56
57foreach my $mirror (@ARGV) {
58 if ($mirror =~ /^\@SF\/(.+)$/) {
59 my $sfpath = $1;
60 open SF, "wget -t1 -q -O- 'https://prdownloads.sourceforge.net/$sfpath/$filename' |";
61 while (<SF>) {
62 /RADIO NAME=use_default VALUE=(\w+) OnClick="form\.submit\(\)">/ or
63 /type="radio" name="use_default" value="(\w+)" onclick="form\.submit\(\)"\/>/ and do {
64 push @mirrors, "https://$1.dl.sourceforge.net/sourceforge/$sfpath";
65 };
66 /<a href="\/.+\?use_mirror=(\w+)"><b>Download/ and do {
67 push @mirrors, "https://$1.dl.sourceforge.net/sourceforge/$sfpath";
68 };
69 }
70 push @mirrors, "https://dl.sourceforge.net/sourceforge/$sfpath";
71 close SF;
72 } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
73 my $gnupath = $1;
74 push @mirrors, "https://ftp.gnu.org/gnu/$gnupath";
75 } else {
76 push @mirrors, $mirror;
77 }
78}
79
80push @mirrors, 'http://mbsd.evolvis.org/.Archive/FreeWRT/';
81
82while (!$ok) {
83 my $mirror = shift @mirrors;
84 $mirror or die "No more mirrors to try - giving up.\n";
85
86 download($mirror);
87 -f "$target/$filename" and $ok = 1;
88}
89
90$SIG{INT} = \&cleanup;
91
Note: See TracBrowser for help on using the repository browser.