#!/usr/bin/perl

# Copyright (c) 2004 Asgeir.Nilsen@telenor.com
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use 5.6.0;
use strict;
use warnings;

use Confluence;
use IO::File;
use MIME::Lite;

######## CONFIGURATION SECTION ###########
my $BASE = 'http://confluence.atlassian.com';
my $USER = 'nobody';
my $PASS = 'changeme';
my $DATA = '/tmp';
my $MAIL = 'confluence-user@lists.atlassian.com';
######## CONFIGURATION SECTION ###########

my $con = Confluence->new("$BASE/rpc/xmlrpc", $USER, $PASS);

# SPACEKEY from command line
my $space = shift;

my $result = $con->getBlogEntries($space);

die unless defined $result;

my @data = sort { $b->{publishSort} cmp $a->{publishSort} } 
map { my $t = `date -u -d '$_->{publishDate}' +%Y%m%d%H%M%S`;
      chomp($t);
      $_->{publishSort} = $t;
      $_ }
@$result;

# Get the newest article we've displayed before
my $sep = IO::File->new("$DATA/$space.dat")->getline;

my $count = 0;
my $subject = undef;

my $output = "<html><head><title>News from $space</title><base href=\"$BASE/\"/><link rel=\"stylesheet\" href=\"$BASE/styles/main-action.css?spaceKey=$space\" type=\"text/css\" /></head><body>";

my %users = ();

foreach my $item (@data) {
    # Skip articles displayed earlier
    last if $item->{publishSort} le $sep;
    # Store a new date for the most recent one displayed
    IO::File->new(">$DATA/$space.dat")->print($item->{publishSort}) unless $count++;
    $subject ||= $item->{title};

    # Retrieve and cache user data
    my $uid = $item->{author};
    $users{$uid} ||= $con->getUser($uid);

    $output .= "<h1><a href=\"" . $item->{url} . "\">" . $item->{title} . "</a></h1>";
    $output .= '<div class="pagesubheading"><a href="mailto:' . $users{$uid}{email} . '">' .
	$users{$uid}{fullname} . '</a>&nbsp;&mdash;&nbsp;' .
	$item->{publishDate} . '</div>';

    # Render blog post as HTML
    $result = $con->renderContent($item->{space}, $item->{id}, ""); 

    # Clean up the rendered HTML, removing header and trailer
    $result =~ s/^.+<body>//s;
    $result =~ s:</body>.+$::s;
    $output .= $result;
}

$output .= "</body></html>";

if ($count) {
    # Send off the HTML to the mailing list, if any new news.
    my $mime = new MIME::Lite
	To      => sprintf($MAIL, $space),
	Subject => $subject,
	Type    => 'text/html',
	Data    => $output;
    
    $mime->send;
}
