summaryrefslogtreecommitdiff
path: root/jpl/get_jdk/get_jdk.pl
blob: d6d399d66910ee15e84b87e43b499ff4109e5439 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/perl -w

# Based on an ftp client found in the LWP Cookbook and
# revised by Nathan V. Patwardhan <nvp@ora.com>.

# Copyright 1997 O'Reilly and Associates
# This package may be copied under the same terms as Perl itself.
#
# Code appears in the Unix version of the Perl Resource Kit

use LWP::UserAgent;
use URI::URL;

my $ua = new LWP::UserAgent;

# check to see if a JDK port exists for the OS.  i'd say
# that we should use solaris by default, but a 9meg tarfile
# is a hard pill to swallow if it won't work for somebody.  :-)
my $os_type = $^O; my $URL = lookup_jdk_port($os_type);
die("No JDK port found.  Contact your vendor for details.  Exiting.\n")
    if $URL eq '';

print "A JDK port for your OS has been found.\nContacting: ".$URL."\n";

# Now, parse the URL using URI::URL
my($jdk_file) = (url($URL)->crack)[5]; 
$jdk_file =~ /(.+)\/(.+)/; $jdk_file = $2;

print "Attempting to download: $jdk_file\n";

my $expected_length;
my $bytes_received = 0;

open(OUT, ">".$jdk_file) or die("Can't open $jdk_file: $!");
$ua->request(HTTP::Request->new('GET', $URL),
	     sub {
		 my($chunk, $res) = @_;

		 $bytes_received += length($chunk);
		 unless (defined $expected_length) {
		     $expected_length = $res->content_length || 0;
		 }
		 if ($expected_length) {
		     printf STDERR "%d%% - ",
		     100 * $bytes_received / $expected_length;
		 }
		 print STDERR "$bytes_received bytes received\n";

		 print OUT $chunk;
	     }
);
close(OUT);

sub lookup_jdk_port {
    my($port_os) = @_;
    my $jdk_hosts = 'jdk_hosts';
    my %HOSTS = ();

    open(CFG, $jdk_hosts) or die("hosts error: $!");
    while(<CFG>) {
	chop;
	($os, $host) = split(/\s*=>\s*/, $_);
	next unless $os eq $port_os;
	push(@HOSTS, $host);
    }
    close(CFG);

    return "" unless @HOSTS;
    return $HOSTS[rand @HOSTS];		# Pick one at random.
}