#!/usr/bin/perl
# SPDX-License: LGPL-2.1-or-later
# Copyright 2019 Kopano B.V.
# Produce the PlotData string for MediaWiki EasyTimeline from a textfile.
#
use strict;
use warnings;
our($series, $last_date, $last_cat, $last_ver, $srow);

while (<>) {
	next if (substr($_, 0, 1) eq "#");
	@_ = split(/\s+/, $_);
	if ($_[0] ne ".") {
		($srow, $series, $last_date, $last_cat, $last_ver) = (0, $_[0]);
		my $x = $series;
		$x =~ s/[^a-zA-Z0-9]/_/g;
		print "\n  bar:series$x width:18\n";
		next if (!defined($_[2]));
	}
	++$srow;
	my $date = $_[2];
	my($cat, $ver) = split(/:/, $_[1], 2);
	if (defined($last_date)) {
		print "    color:s$last_cat from:".&fdate($last_date).
		      " till:".&fdate($date);
		if ($srow == 2) {
			print " fontsize:M text:$series";
		}
		print "\n";
	}
	if (defined($ver)) {
		print "    at:".&fdate($date)." mark:(line,p$cat) # $ver\n";
	}
	($last_cat, $last_ver, $last_date) = ($cat, $ver, $date);
}
print "# PlotData auto-generated by rm2mwtl.pl from $ARGV.\n";

sub fdate
{
	if ($_[0] eq "unspecified") {
		@_ = localtime();
		return sprintf "%02u/%02u/%04u", $_[3], $_[4]+1, $_[5]+1900;
	}
	my($y, $m, $d) = split(/-/, $_[0]);
	return "$d/$m/$y";
}
