#! /usr/bin/perl

$INPUT = $ARGV[0];

if ($INPUT eq "") {
    die "call: makesvg <solution>\n";
}
    
# read log file

open (INP, $INPUT) or die "can't find $INPUT\n";

while ($line = <INP>) {
    ($var,$value,$rest) = split " ", $line;
    $cpx{$var}=$value+0.0;
}

close (INP);


# parse variable values from log file

$numberOfAreas = 0;
%mipopt = ();

foreach $var (sort keys %cpx) {
    if ($var =~ /^x\#(.*)/) {
	    $x[$1] = $cpx{$var};
        if ($1 > $numberOfAreas) { $numberOfAreas = $1 + 0; }
    }
    elsif ($var =~ /^y\#(.*)/) {
        $y[$1] = $cpx{$var};
        if ($1 > $numberOfAreas) { $numberOfAreas = $1 + 0; }
    }
    elsif ($var =~ /^width\#(.*)/) {
        $width[$1] = $cpx{$var};
        if ($1 > $numberOfAreas) { $numberOfAreas = $1 + 0; }
    }
    elsif ($var =~ /^height\#(.*)/) {
        $height[$1] = $cpx{$var};
        if ($1 > $numberOfAreas) { $numberOfAreas = $1 + 0; }
    }
    elsif ($var eq "X") {
        $X = $cpx{$var};
    }
    elsif ($var eq "Y") {
        $Y = $cpx{$var};
    }
}


# generate graphic

$scale = 60;

open (SVG, ">$INPUT.svg") or die "can't create $INPUT.svg\n";

print SVG "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
print SVG "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:ev=\"http://www.w3.org/2001/xml-events\" version=\"1.1\" width=\"",20+$X*$scale,"\" height=\"",20+$Y*$scale,"\">\n";

for $i (1..$numberOfAreas) {
    if ($x[$i] eq "") { $x[$i] = 0 };
    if ($y[$i] eq "") { $y[$i] = 0 };
    print "$i) x=$x[$i], y=$y[$i], width=$width[$i], height=$height[$i], area=",$width[$i]*$height[$i],"\n";
	print SVG "<rect x=\"",10+$x[$i]*$scale,"\" y=\"",10+$y[$i]*$scale,"\" width=\"",$width[$i]*$scale,"\" height=\"",$height[$i]*$scale,"\" style=\"fill:lightblue; stroke-width:2; stroke:black\"/>\n";
    print SVG "<text style=\"font-size:20\" x=\"",10+($x[$i]+$width[$i]/2)*$scale,"\" y=\"",10+($y[$i]+$height[$i]/2)*$scale,"\">$i</text>\n";
}

print "X=$X, Y=$Y, area=",$X*$Y,"\n";
print SVG "</svg>\n";
close (SVG);


