#!/usr/bin/perl -w

use strict;

use SiteSummary;
use Getopt::Std;

my %profiles;
my %versions;
my %phostmap;
my %vhostmap;
my %opts;

sub usage {
    my $retval = shift;
    print <<EOF;
Usage: $0 [-l]
   -l   list hosts with the debian-edu version
EOF
    exit $retval;
}

getopt("l", \%opts) || usage(1);

for_all_hosts(\&handle_host);

print_summary();

exit 0;

sub handle_host {
    my $hostid = shift;
    my $profile = get_debian_edu_profile($hostid);
    my $version = get_debian_edu_ver($hostid);
    if ($profile) {
        $profiles{$profile}++ if (defined $profile);
        $phostmap{$profile} = [] unless exists $phostmap{$profile};
        push @{$phostmap{$profile}}, $hostid ;
    }
    if ($version) {
        $versions{$version}++ if (defined $version);
        $vhostmap{$version} = [] unless exists $vhostmap{$version};
        push @{$vhostmap{$version}}, $hostid ;
    }
}

sub print_summary {
    printf("  %-30s %5s\n", "debian-edu-profile", "count");
    foreach my $profile ( keys %profiles ) {
        printf(" %30s %5s\n", $profile || "n/a", $profiles{$profile});
        if (exists $opts{l}) {
            for my $hostid (sort @{$phostmap{$profile}}) {
                my $hostname = get_hostname($hostid);
                printf "    %s %s %s\n", $hostname, $profile, $hostid;
            }
        }
    }
    printf("  %-30s %5s\n", "debian-edu-version", "count");
    foreach my $version ( keys %versions ) {
        printf(" %30s %5s\n", $version || "n/a", $versions{$version});
        if (exists $opts{l}) {
            for my $hostid (sort @{$vhostmap{$version}}) {
                my $hostname = get_hostname($hostid);
                printf "    %s %s %s\n", $hostname, $version, $hostid;
            }
        }
    }
}
