#!/usr/local/bin/perl

use CGI qw(:standard);

my $rup = "/bin/rup -l";

my $machines = "pinchot leopold darwin capra";
my %ports = ("pinchot" => ":8080", 
             "leopold" => ":8080",
             "darwin" => ":8080",
             "capra" => ":8080",
            );
my $domain = "dnr.state.mn.us";

my $script = "/cgi-bin/mapserv";
my $query = '';

$cgi = new CGI;

if($cgi->self_url =~ /\?/) {
  $query = $cgi->self_url;
  $query =~ s/.*\?//;
}

# Load Balancing setup
#
@hosts = `$rup $machines`;
foreach $line (@hosts) {
  while ($line =~ s/^\s//) {};
  $line =~ s/\,//g;
  @array = split(/ /,$line);
  $mach = $array[0];
  pop(@array);
  pop(@array);
  $load_1 = pop(@array);
  $pool{$mach} = $load_1;
}

$cload = 100;
foreach $mach (keys(%pool)) {
  if ($pool{$mach} < $cload) {
     $cload = $pool{$mach};
     $preferred = $mach;
   }
}
  
print "Location: http://$preferred.$domain". $ports{$preferred} ."$script?$query\n\n";

exit;
