<& /htmlheader &>

% my @slist;
% foreach my $user (sort keys %usertbl) {
% push @slist, qq{<a href="#$user">$user</a>};
% }
<% join " | ", @slist, qq{<a href="index_list.html">old page</a>} %>

<table border=0 cellspacing=2 cellpadding=2 width="50%">
% foreach my $user (('mopspipe', grep { $_ ne 'mopspipe' } sort keys %usertbl)) {
<tr> 
    <td> 
        <font size="+2">
        <& usercard, user => $user, dbinfo_aref => $usertbl{$user} &>
        </font>
        <p>
    </td> 
</tr>
% }

</table>

<& /htmlfooter &>

<%args>
</%args>

<%init>
use File::Slurp;
my %dbinfo;
my @databases = grep /psmops/, `echo 'show databases' | /usr/bin/mysql -umops -pmops -h mops01`;
#my @databases = grep /psmops/, `echo 'show databases' | mysql -umops -pmops -h localhost`;
my $home;               # database's $MOPS_HOME
my @abbr = qw( JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC );

foreach my $dbname (@databases) {
    my $desc;
    my $descfile;
    my $verfile;
    my $logfile;

    $dbname =~ s/\W+//g;               # strip | and whitespace
    $home = '/home/mops/MOPS_DEVEL';
    $descfile = "$home/var/$dbname/config/description";
    $verfile = "$home/var/$dbname/config/version";
    $logfile = "$home/var/$dbname/log/mops.log";

    if (-e "$home/var/$dbname/config/version") {
        # Better version check here eventually.
        if (-e $descfile) {
            $desc = read_file($descfile);
        }
        if (-e $verfile) {
            my @st = stat($verfile);
            my $created = scalar(localtime($st[9]));
            my $owner = getpwuid($st[4]);

            @st = stat($logfile);
            my @t = localtime($st[9]);
#            my $modified = scalar(localtime($st[9]));
            my $modified = sprintf "%4d-%s-%d", $t[5] + 1900, $abbr[$t[4]], $t[3];

            my ($version) = grep !/^#/, read_file($verfile);
            chomp $version;

            if ($version >= 200606) {
                $dbinfo{$dbname} = {
                    name => $dbname,
#                    desc => ($desc or 'N/A') . " ($version)",
                    desc => ($desc or 'N/A'),
                    version => $version,
                    timestamp => (-M $logfile),
                    created => $created,
                    modified => $modified,
                    owner => ($owner || 'unknown'),
                };
            }
        }
    }
}


# Group the simulations by user, creating a single table for each user.
my %usertbl;
foreach my $k (keys %dbinfo) {
    push @{$usertbl{$dbinfo{$k}->{owner}}}, $dbinfo{$k};      # add to list 
}

# Sort each of the per-user lists by time.
foreach my $k (keys %usertbl) {
    @{$usertbl{$k}} = sort { $a->{timestamp} <=> $b->{timestamp} } @{$usertbl{$k}};
}

#my @sorted_instances;
#if ($sort_by eq 'a') {
#    @sorted_instances = sort { $a cmp $b } keys %dbinfo;
#}
#elsif ($sort_by eq 'u') {
#    @sorted_instances = sort { $dbinfo{$a}->{owner} cmp $dbinfo{$b}->{owner} } keys %dbinfo;
#}
#else {
#    @sorted_instances = sort { $dbinfo{$a}->{timestamp} <=> $dbinfo{$b}->{timestamp} } keys %dbinfo;
#}

</%init>
