#! /usr/bin/perl

use strict;

my $txt;
my $i = 0;
my $barwidth = 20;

my @data = <DATA>;
&gen( $_, @data ) for 1..2;

open( my $fh, ">", "xipergrid1.grid" ) || die;
print $fh "; grid: xipergrid1\n; generated: " . localtime() . "\n\n\n[Groups]\n\nNumberOfGroups = $i\n\n\n";
print $fh $txt;


sub gen
{
  my( $m, @lines ) = @_;
  
  foreach my $line ( @lines )
  {
    chomp( $line );
    if( $line =~ m{^\s*$} || $line =~ m{^\s*#} ) { next }
    
    my( $t, $g ) = split( "=", $line );
    my( $t_from, $t_to ) = split( "-", $t );
    $t_to ||= $t_from;
    
    my $t_from_x = substr( $t_from, 0, 1 );
    my $t_from_y = substr( $t_from, 1, 1 );
    $t_from_x = { a=>0,b=>1,c=>2,d=>3,e=>4,f=>5,g=>6,h=>7,i=>8,j=>9,'@'=>'@' }->{$t_from_x};
    
    my $t_to_x = substr( $t_to, 0, 1 );
    my $t_to_y = substr( $t_to, 1, 1 );
    $t_to_x = { a=>0,b=>1,c=>2,d=>3,e=>4,f=>5,g=>6,h=>7,i=>8,j=>9,'@'=>'@' }->{$t_to_x};
    $t_to_x++ if $t_to_x ne '@';
    $t_to_y++ if $t_to_y ne '@';
    
    my( $g_dock ) = substr( $g, 0, 2 );
    $g = substr( $g, 2 );
    my( $g_width, $g_height ) = split( "x", $g );
    $g_width = { 1=>"/3", 2=>"/2", 3=>"/3*2", 4=>"", 5=>"/5*4" }->{$g_width};
    $g_height = { 1=>"/3", 2=>"/2", 3=>"/3*2", 4=>"", 5=>"/5*4" }->{$g_height};
    
    print "trigger: from ($t_from_x,$t_from_y), to ($t_to_x,$t_to_y)\n";
    print "grid: dock $g_dock, width $g_width, height $g_height\n";
    print "\n";
    
    $i++;
    $txt .= "[$i]\n\n";
    #$txt .= "; $line\n";
    
    if( $t_from_y eq '@' ) { $txt .= "TriggerTop    = [Monitor${m}Top]  + ([Monitor${m}Height] / 2) - $barwidth\n" }
    else                   { $txt .= "TriggerTop    = [Monitor${m}Top]  + ([Monitor${m}Height] / 10 * $t_from_y)\n" }
    if( $t_to_y   eq '@' ) { $txt .= "TriggerBottom = [Monitor${m}Top]  + ([Monitor${m}Height] / 2) + $barwidth\n" }
    else                   { $txt .= "TriggerBottom = [Monitor${m}Top]  + ([Monitor${m}Height] / 10 * $t_to_y)\n" }
    if( $t_from_x eq '@' ) { $txt .= "TriggerLeft   = [Monitor${m}Left] + ([Monitor${m}Width]  / 2) - $barwidth\n" }
    else                   { $txt .= "TriggerLeft   = [Monitor${m}Left] + ([Monitor${m}Width]  / 10 * $t_from_x)\n" }
    if( $t_to_x   eq '@' ) { $txt .= "TriggerRight  = [Monitor${m}Left] + ([Monitor${m}Width]  / 2) + $barwidth\n" }
    else                   { $txt .= "TriggerRight  = [Monitor${m}Left] + ([Monitor${m}Width]  / 10 * $t_to_x)\n" }
    
    if   ( $g_dock =~ m{[clr]c} ) { $txt .= "GridTop       = [Monitor${m}Top] + ([Monitor${m}Height] / 2) - (([Monitor${m}Height] $g_height)/2)\n" }
    elsif( $g_dock =~ m{t}      ) { $txt .= "GridTop       = [Monitor${m}Top]\n" }
    elsif( $g_dock =~ m{b}      ) { $txt .= "GridTop       = [Monitor${m}Bottom] - ([Monitor${m}Height] $g_height)\n" }
    if   ( $g_dock =~ m{[clr]c} ) { $txt .= "GridBottom    = [Monitor${m}Top] + ([Monitor${m}Height] / 2) + (([Monitor${m}Height] $g_height)/2)\n" }
    elsif( $g_dock =~ m{t}      ) { $txt .= "GridBottom    = [Monitor${m}Top] + ([Monitor${m}Height] $g_height)\n" }
    elsif( $g_dock =~ m{b}      ) { $txt .= "GridBottom    = [Monitor${m}Bottom]\n" }
    if   ( $g_dock =~ m{[ctb]c} ) { $txt .= "GridLeft      = [Monitor${m}Left] + ([Monitor${m}Width] / 2) - (([Monitor${m}Width] $g_width)/2)\n" }
    elsif( $g_dock =~ m{l}      ) { $txt .= "GridLeft      = [Monitor${m}Left]\n" }
    elsif( $g_dock =~ m{r}      ) { $txt .= "GridLeft      = [Monitor${m}Right] - ([Monitor${m}Width] $g_width)\n" }
    if   ( $g_dock =~ m{[ctb]c} ) { $txt .= "GridRight     = [Monitor${m}Left] + ([Monitor${m}Width] / 2) + (([Monitor${m}Width] $g_width)/2)\n" }
    elsif( $g_dock =~ m{l}      ) { $txt .= "GridRight     = [Monitor${m}Left] + ([Monitor${m}Width] $g_width)\n" }
    elsif( $g_dock =~ m{r}      ) { $txt .= "GridRight     = [Monitor${m}Right]\n" }
    
    $txt .= "\n\n";
    
  }  # end foreach line
}  # end gen()


__DATA__

# syntax: trigger=grid
# trigger: from_xy[-to_xy]
#          x is a-j, y is 0-9, @ is centered
# grid: dock,widthxheight
# dock: t=top,b=bottom,l=left,r=right,c=center
#       tl, tr, br, bl   tc, rc, bc, lc    cc
# width/height:
#   1 = 1/3 screen
#   2 = 1/2 screen
#   3 = 2/3 screen
#   4 = 1/1 screen
#   5 = 4/5 screen

# top center
@0=tc1x1
@1=tc1x2
@2=tc1x3
@3=tc1x4

# bottom center
@9=bc1x1
@8=bc1x2
@7=bc1x3
@6=bc1x4

# left center
a@=lc1x1
b@=lc2x1
c@=lc3x1
d@=lc4x1

# right center
j@=rc1x1
i@=rc2x1
h@=rc3x1
g@=rc4x1

# top left
a0=tl1x1
b0=tl2x1
c0=tl3x1
a1=tl1x2
b1=tl2x2
c1=tl3x2
a2=tl1x3
b2=tl2x3
c2=tl3x3

# top
d0-g0=tl4x1
d1-g1=tl4x2
d2-g2=tl4x3

# top right
j0=tr1x1
i0=tr2x1
h0=tr3x1
j1=tr1x2
i1=tr2x2
h1=tr3x2
j2=tr1x3
i2=tr2x3
h2=tr3x3

# right
j3-j6=tr1x4
i3-i6=tr2x4
h3-h6=tr3x4

# bottom right
j9=br1x1
i9=br2x1
h9=br3x1
j8=br1x2
i8=br2x2
h8=br3x2
j7=br1x3
i7=br2x3
h7=br3x3

# bottom
d9-g9=br4x1
d8-g8=br4x2
d7-g7=br4x3

# bottom left
a9=bl1x1
b9=bl2x1
c9=bl3x1
a8=bl1x2
b8=bl2x2
c8=bl3x2
a7=bl1x3
b7=bl2x3
c7=bl3x3

# left
a3-a6=bl1x4
b3-b6=bl2x4
c3-c6=bl3x4

# center
e4-f5=cc3x3
c3-g6=cc5x5
