#!/usr/local/bin/perl

use warnings;
use strict;

my $svgheader = qq(<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">);
my $startline = "<polyline fill=\"none\" stroke=\"black\" stroke-width=\"0.5\"\npoints=\"";
my $svg;
my @maplines;
my $currentline = 0;

# Avoiding the "Use of unitialized value" warning
$maplines[$currentline] = "";

if (!($ARGV[0])) { print "usage: perl mapper.pl logfile.txt\n"; exit; }

my $logfile = $ARGV[0];
 
open(LOGFILE, "<$logfile") || die "can't open $logfile!!\n";

print $svgheader;

while (<LOGFILE>) {
    if (m/start new map line/) {
        # Don't start a new line if current line is empty
        if ($maplines[$currentline] ne "") {
            $currentline++;
            # Avoiding the "Use of unitialized value" warning
            $maplines[$currentline] = "";
        }
    } elsif (m/ (-?\d+[,\.]\d{2}), (-?\d+[,\.]\d{2}), (-?\d+[,\.]\d{2})[,\.] /) {

        my $x = $1;
        my $y = $3;

        $x =~ s/,/\./g; # change 12,34 to 12.34 (for european locales)
        $y =~ s/,/\./g; # change 12,34 to 12.34 (for european locales)

        $x = -$x;       # changing neg numbers to pos, and vice versa
        $x =~ s/\+//;   # removing '+' character

        $maplines[$currentline] .= "$x,$y ";
    }
}

foreach my $line (@maplines) {
    print $startline;
    print $line;
    print "\"/>\n\n";
}


print "</svg>";
