dnsmasq: full: disable ipset support by default
[openwrt/staging/mkresin.git] / scripts / download.pl
index 324cfdb3b3102924eee361da17c8e587ac100765..af13c0ae008e90688091274b419f76d31d3e8c54 100755 (executable)
@@ -54,10 +54,8 @@ sub localmirrors {
 
 sub which($) {
        my $prog = shift;
-       my $res = `which $prog`;
+       my $res = `command -v $prog`;
        $res or return undef;
-       $res =~ /^no / and return undef;
-       $res =~ /not found/ and return undef;
        return $res;
 }
 
@@ -65,8 +63,8 @@ sub hash_cmd() {
        my $len = length($file_hash);
        my $cmd;
 
-       $len == 64 and return "mkhash sha256";
-       $len == 32 and return "mkhash md5";
+       $len == 64 and return "$ENV{'MKHASH'} sha256";
+       $len == 32 and return "$ENV{'MKHASH'} md5";
        return undef;
 }
 
@@ -93,6 +91,7 @@ $hash_cmd or ($file_hash eq "skip") or die "Cannot find appropriate hash command
 sub download
 {
        my $mirror = shift;
+       my $download_filename = shift;
 
        $mirror =~ s!/$!!;
 
@@ -139,7 +138,7 @@ sub download
                        }
                };
        } else {
-               my @cmd = download_cmd("$mirror/$url_filename");
+               my @cmd = download_cmd("$mirror/$download_filename");
                print STDERR "+ ".join(" ",@cmd)."\n";
                open(FETCH_FD, '-|', @cmd) or die "Cannot launch curl or wget.\n";
                $hash_cmd and do {
@@ -193,10 +192,17 @@ foreach my $mirror (@ARGV) {
                for (1 .. 5) {
                        push @mirrors, "https://downloads.sourceforge.net/$1";
                }
+       } elsif ($mirror =~ /^\@OPENWRT$/) {
+               # use OpenWrt source server directly
+       } elsif ($mirror =~ /^\@DEBIAN\/(.+)$/) {
+               push @mirrors, "https://ftp.debian.org/debian/$1";
+               push @mirrors, "https://mirror.leaseweb.com/debian/$1";
+               push @mirrors, "https://mirror.netcologne.de/debian/$1";
        } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
                push @mirrors, "https://mirror.netcologne.de/apache.org/$1";
                push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
                push @mirrors, "https://mirror.csclub.uwaterloo.ca/apache/$1";
+               push @mirrors, "https://archive.apache.org/dist/$1";
                push @mirrors, "http://mirror.cogentco.com/pub/apache/$1";
                push @mirrors, "http://mirror.navercorp.com/apache/$1";
                push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
@@ -215,14 +221,14 @@ foreach my $mirror (@ARGV) {
                push @mirrors, "http://mirror.internode.on.net/pub/gnu/$1";
                push @mirrors, "http://mirror.navercorp.com/gnu/$1";
                push @mirrors, "ftp://mirrors.rit.edu/gnu/$1";
-               push @mirrors, "ftp://download.xs4all.nl/pub/gnu/";
+               push @mirrors, "ftp://download.xs4all.nl/pub/gnu/$1";
+               push @mirrors, "https://ftp.gnu.org/gnu/$1";
        } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
                push @mirrors, "https://mirror.netcologne.de/savannah/$1";
                push @mirrors, "https://mirror.csclub.uwaterloo.ca/nongnu/$1";
                push @mirrors, "http://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
                push @mirrors, "http://nongnu.uib.no/$1";
                push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
-               push @mirrors, "http://public.p-knowledge.co.jp/Savannah-nongnu-mirror/$1";
                push @mirrors, "ftp://cdimage.debian.org/mirror/gnu.org/savannah/$1";
                push @mirrors, "ftp://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
        } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
@@ -231,18 +237,18 @@ foreach my $mirror (@ARGV) {
                        push @extra, "$extra[0]/testing";
                } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
                        push @extra, "$extra[0]/longterm/v$1";
-               }               
+               }
                foreach my $dir (@extra) {
                        push @mirrors, "https://cdn.kernel.org/pub/$dir";
-                       push @mirrors, "https://mirror.rackspace.com/kernel.org/$dir";
-                       push @mirrors, "http://download.xs4all.nl/ftp.kernel.org/pub/$dir";
-                       push @mirrors, "http://mirrors.mit.edu/kernel/$dir";
+                       push @mirrors, "https://download.xs4all.nl/ftp.kernel.org/pub/$dir";
+                       push @mirrors, "https://mirrors.mit.edu/kernel/$dir";
                        push @mirrors, "http://ftp.nara.wide.ad.jp/pub/kernel.org/$dir";
                        push @mirrors, "http://www.ring.gr.jp/archives/linux/kernel.org/$dir";
                        push @mirrors, "ftp://ftp.riken.jp/Linux/kernel.org/$dir";
                        push @mirrors, "ftp://www.mirrorservice.org/sites/ftp.kernel.org/pub/$dir";
                }
-    } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
+       } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
+               push @mirrors, "https://download.gnome.org/sources/$1";
                push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnome/sources/$1";
                push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
                push @mirrors, "http://ftp.kaist.ac.kr/gnome/sources/$1";
@@ -251,23 +257,41 @@ foreach my $mirror (@ARGV) {
                push @mirrors, "http://ftp.belnet.be/ftp.gnome.org/sources/$1";
                push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
                push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
-    }
-    else {
+       } else {
                push @mirrors, $mirror;
        }
 }
 
-#push @mirrors, 'http://mirror1.openwrt.org';
-push @mirrors, 'http://sources.lede-project.org';
-push @mirrors, 'http://mirror2.openwrt.org/sources';
-push @mirrors, 'http://downloads.openwrt.org/sources';
+push @mirrors, 'https://sources.cdn.openwrt.org';
+push @mirrors, 'https://sources.openwrt.org';
+push @mirrors, 'https://mirror2.openwrt.org/sources';
+
+if (-f "$target/$filename") {
+       $hash_cmd and do {
+               if (system("cat '$target/$filename' | $hash_cmd > '$target/$filename.hash'")) {
+                       die "Failed to generate hash for $filename\n";
+               }
+
+               my $sum = `cat "$target/$filename.hash"`;
+               $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
+               $sum = $1;
+
+               cleanup();
+               exit 0 if $sum eq $file_hash;
+
+               die "Hash of the local file $filename does not match (file: $sum, requested: $file_hash) - deleting download.\n";
+               unlink "$target/$filename";
+       };
+}
 
 while (!-f "$target/$filename") {
        my $mirror = shift @mirrors;
        $mirror or die "No more mirrors to try - giving up.\n";
 
-       download($mirror);
+       download($mirror, $url_filename);
+       if (!-f "$target/$filename" && $url_filename ne $filename) {
+               download($mirror, $filename);
+       }
 }
 
 $SIG{INT} = \&cleanup;
-