#!/usr/bin/perl

use strict;
use warnings;

use LWP::Simple;
use POSIX;

my $title;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$mon += 1;
my $week = POSIX::strftime( "%V", localtime);

my $output = "[[!meta author=\"Richard 'RichiH' Hartmann\"]]\n";
$output .= "[[!tag tags/tech/floss/debian/rc-stats/8.0-jessie tags/random/hidden]]\n";
$output .= sprintf "[[!meta date=\"%04d-%02d-%02d %02d:%02d:%02d\"]]\n", $year, $mon, $mday, $hour, $min, $sec;

foreach (split(/\n/,get('http://udd.debian.org/cgi-bin/rcblog.cgi'))) {
	if (/<h1>(.*)<\/h1>/) {
		$title = $1;
		$output .= '[[!meta title="' . $title . '"]]' . "\n\n";
		$title =~ s/\s/_/g;
		$title = sprintf "%02d-Debian_$title", $mday;
		next;
	}
	$output .= "$_\n" unless (/<\/body><\/html>/ || /<html><body>/);
}

my $image = sprintf "rc_bugs_report_en_%04d-%02d.png", $year, $week;
$output .= "<p>Graphical overview of bug stats thanks to azhag:<br />\n";
$output .= "<img src=\"../../../../img/$image\"></p>\n";


open(FH, "> $title.mdwn") or die "$0: $!";
print FH $output;
close FH;

getstore("http://dug.net.pl/gnuplot/rcbugs/$image", "../../../../img/$image") or die "$0: $!";
