#!/usr/bin/perl

#it doesn't make senes to use both -wrapx and -wrapy, and you always use
# the opposite axis in the -wrapd? option

if ($#ARGV == -1) {
  &usage;
  exit(0);
}

$opts{"group"}="mkxlax";
$opts{"x"}=0;
$opts{"y"}=0;
$opts{"dx"}=0;
$opts{"dy"}=0;
$opts{"wrapx"}=0;
$opts{"wrapy"}=0;
$opts{"wrapdx"}=0;
$opts{"wrapdy"}=0;

while (substr($ARGV[0],0,1) eq "-") {
  $o=shift(@ARGV);
  last if ($o eq "--");
  $o=substr($o,1);
  if (! defined($opts{$o})) {
    print "unknown option: -$o\n";
    &usage;
    exit(1);
  }
  $opts{$o}=shift(@ARGV);
}

$xlax_group=$opts{"group"};
$ix=$opts{"x"};
$iy=$opts{"y"};
$dx=$opts{"dx"};
$dy=$opts{"dy"};
$wrapx=$opts{"wrapx"};
$wrapy=$opts{"wrapy"};
$wrapdx=$opts{"wrapdx"};
$wrapdy=$opts{"wrapdy"};

if ($dx == 0 && $dy == 0) {
  $dx=20; $dy=50;
  if ($wrapx == 0 && $wrapy == 0) {
    $wrapy=750; $wrapdx=200;
  }
}

$x=$ix; $y=$iy;
for ($i=0; $i<=$#ARGV; ++$i) {
  system("xterm -geometry 80x24+$x+$y -name \"$xlax_group:$ARGV[$i]\" -xrm 'XTerm*allowSendEvents: true' &");
  sleep(1);
  $x+=$dx;  if ($wrapx && $x>$wrapx) { $x=$ix; $y+=$wrapdy; }
  $y+=$dy;  if ($wrapy && $y>$wrapy) { $y=$iy; $x+=$wrapdx; }
}

sleep(2);

$x=(0)+2;
system("xlax -geometry +$x-2 -prefix $xlax_group: -find &");

#
# END OF MAIN
#

sub usage {
  print "Usage: mkxlax [options] string [more strings]\n";
  print "Options:\n";
  print "  -group name          the prefix to use in xlax\n";
  print "  -x N                 the initial x position of first window\n";
  print "  -y N                 the initial x position of first window\n";
  print "  -dx N                the offset in x for additional windows\n";
  print "  -dy N                the offset in y for additional windows\n";
  print "  -wrapx N             wrap x back to initial value when if >= N\n";
  print "  -wrapy N             wrap y back to initial value when if >= N\n";
  print "  -wrapdx N            when wrapping (either x or y) add N to x\n";
  print "  -wrapdy N            when wrapping (either x or y) add N to y\n";
  print "  --                   stop option processing here\n";
  print "         (in case a window string needs to start with a dash)\n";
}
