#!/usr/bin/perl
#
# Prints out a list of guides and the software versions they're using.

use strict;
use warnings;
use XML::Simple;
use LWP::UserAgent;

my $out = "/srv/www/dev.openguides.org/html/static/versions.html";

my %guides = ( Birmingham => 'http://birmingham.openguides.org/',
               Cambridge => 'http://cambridge.openguides.org/',
               Cotswolds => 'http://cotswolds.openguides.org/',
               Glasgow => 'http://glasgow.openguides.org/',
               Lancaster => 'http://lancaster.openguides.org/',
               London => 'http://london.openguides.org/',
               Manchester => 'http://manchester.openguides.org/',
               'Randomness (London)' => 'http://london.randomness.org.uk/',
               'Milton Keynes' => 'http://miltonkeynes.openguides.org/',
               Norwich => 'http://norwich.openguides.org/',
               Nottingham => 'http://nottingham.openguides.org/',
               Oxford => 'http://oxford.openguides.org/',
               'Vegan Oxford' => 'http://the.earth.li/~kake/cgi-bin/openguides/vegan-oxford.cgi',
               'Reading' => 'http://reading.openguides.org/',
               Southampton => 'http://southampton.openguides.org/',
               Vienna => 'http://vienna.openguides.org/',
               Brussels => 'http://www.justbiit.com/cgi-bin/openguides/',
               'Montreal, QC' => 'http://montreal.openguides.org/',
               'Victoria, BC' => 'http://victoria.openguides.org/',
               'Boston, MA' => 'http://boston.openguides.org/',
               'Saint Paul, MN' => 'http://saintpaul.openguides.org/',
               'New York' => 'http://newyork.openguides.org/myguide/wiki.cgi',
               'The Tourist Engineer' => 'http://engineer.openguides.org/',
             );

my $ua = LWP::UserAgent->new(agent => "OpenGuides get-guide-versions");

open OUT, ">$out" or die $!;

print OUT "<html><head><title>Versions of OpenGuides in use</title></head>\n";
print OUT "<body>\n";
print OUT "Last updated: " . scalar(localtime(time)) . "\n";
print OUT "<table>\n";

foreach my $guide (sort keys %guides) {
    print OUT "<tr><td><a href=\"$guides{$guide}\">$guide</td><td>";

    # Try DOAP
    my $success = 0;
    my $response = $ua->get("$guides{$guide}?action=about;format=rdf");
    if ($response->is_success) {
        my $doap = eval { XMLin($response->content)} ;
        if ($@) {
            # fall through
        } else {
            if (my $version = eval { $doap->{Project}->{release}->{Version}->{revision} }) {
                print OUT "$version (DOAP)";
                $success++;
            }
        }
    }
    if ($success == 0) {
        $response = $ua->get($guides{$guide});
        if ($response->is_success) {
            my $content = $response->content;
            if ($content =~ /version.(0\.\d+(-\w+)?)/s) {
                print OUT $1;
            } else {
                print OUT "Not found";
            }
            print OUT " (HTML)";
        } else {
            print OUT "No methods worked";
        }
        print OUT "</td></tr>\n";
    }
}

print OUT "</table></body></html>\n";

close OUT;
