open(HOST,"<$ARGV[0]");

if (!-e 'secure_args') {
    die("Unable to find security arguments for grabbing lon-status");
}

my $secure_args = `cat secure_args`;
$secure_args =~ s/^\s*//x;
$secure_args =~ s/\s*$//x;
my $i++;
my %vers;
my %distvers;
my %arch;
my %done;
my %combo1;
my %combo2;
while(my $line=<HOST>) {
    if ($line=~/^\#/) { next; }
    my $host=(split(':',$line))[3];
    chomp($host);
    if (!$host || exists($done{$host})) { next; }
    $done{$host}=1;


    my $roles=`curl --max-time 15 --connect-timeout 5 -s http://$host/adm/login  `;
    if ($roles eq '') {
        $roles=`curl --insecure --max-time 15 --connect-timeout 5 -s https://$host/adm/login `;
    }

#-- lon-capa version
    print($host." -> ");
    my $ver=`curl --max-time 15 --connect-timeout 5 -s http://$host/adm/login | grep -- -200 | tr ">" "\n" | tail -1 `;
    $ver=~s/&nbsp;//g;
    $ver=~s/ //g;
    if ($ver eq "") {
	print(" https -> ");
        $ver=`curl --insecure --max-time 15 --connect-timeout 5 -s https://$host/adm/login | grep -- -200 | tr ">" "\n" | tail -1 `;
    }
    $ver=~s/&nbsp;//g;
    $ver=~s/ //g;
    if ($ver eq "") {
	if ($roles eq '') { $ver="dead\n"; } else { $ver = "old\n" }
    }
    print($ver);
    $ver=~s/\s//g;
    push(@{$vers{$ver}},$host);

    print(' 'x(length($host)).' -> ');
    my $lonstatus=`curl $secure_args --max-time 15 --connect-timeout 5 -s http://$host/lon-status/ `;
    if ($lonstatus !~ /LON Status Report/) {
	print(" https -> ");
        $lonstatus=`curl --insecure $secure_args --max-time 15 --connect-timeout 5 -s https://$host/lon-status/  `;
    }
    if ($lonstatus =~ /LON Status Report/) {
	print(" lon-status \n");
    }

#-- OS 
    my $dist = $lonstatus;
    print(' 'x(length($host)).' -> ');
    my $re = '.*distprobe.*?<pre>(.*?)</pre>.*';
#    my $re = '.*?<h1>.*?</h1>.*?<h2>(.*?)</h2>.*';
    if ($dist =~ m{$re}sx) {
	$dist=~s{$re}{$1}sx;
	print($dist);
    } else {
	undef($dist);
    }

    print("\n");
    push(@{$distvers{$dist}},$host);

#-- arch
    my $arch = $lonstatus;
    print(' 'x(length($host)).' -> ');
    my $re = '.*?Detected\s+64bit\s+platform\s*\((\d*)\).*';
    if ($arch =~ m{$re}sx) {
	$arch=~s{$re}{$1}sx;
	print($arch);
    } else {
	undef($arch);
    }
    print("\n");
    push(@{$arch{$arch}},$host);

    $combo1{"$ver,$dist,$arch"}++;#.=$host.', ';
    $combo2{"$dist,$arch"}++;#.=$host.', ';

#    if ($i++==14) { last; }
}
print("LON-CAPA Versions\n");
foreach my $key (sort(keys(%vers))) {
    print($key." -> ".scalar(@{$vers{$key}})." machines\n");
    print("     ".join("\n     ",sort(@{$vers{$key}}))."\n");
}
print("OS Versions\n");
foreach my $key (sort(keys(%distvers))) {
    print($key." -> ".scalar(@{$distvers{$key}})." machines\n");
    print("     ".join("\n     ",sort(@{$distvers{$key}}))."\n");
}
print("64bit?\n");
foreach my $key (sort(keys(%arch))) {
    print($key." -> ".scalar(@{$arch{$key}})." machines\n");
    print("     ".join("\n     ",sort(@{$arch{$key}}))."\n");
}
print("Combo 1?\n");
foreach my $key (sort(keys(%combo1))) {
    print($key." -> ".$combo1{$key}." machines\n");
}
print("Combo 2?\n");
foreach my $key (sort(keys(%combo2))) {
    print($key." -> ".$combo2{$key}." machines\n");
}
