#!/usr/bin/perl
$APPENV = $ENV{'APPENV'} || '/app/env';
$ENV{'PATH'} = "$APPENV/bin:". $ENV{'PATH'};

$nullcmd = '';
$sshcmd2 = '"exec ssh `sed -n TL_NUMp $XDO_TERMIN`"';
$sshcmd  = '"exec ssh TL_FQDN"';
  $nullcmd = '$HOME/bin/`archit`/tcsh -l',
  $sshcmd2 = 'exec csh -c to\ `sed -n TL_NUMp $XDO_TERMIN`',
  $sshcmd  = q("exec csh -c 'to TL_HOST'") if $ENV{'USER'} eq 'kinzler';

# terminator-layout - output a terminator layout config section from the args
# Steve Kinzler, steve@kinzler.com, Sep 22
# https://kinzler.com/me/home.html#x11

$usage = "usage: $0 title [ host[#group] | - ] ...\n";
die $usage if $#ARGV < 0 || $#ARGV > 6 || $ARGV[0] =~ /[^-+,\w\s]/;

$title = shift @ARGV;

foreach (@ARGV) {
	($h, $g) = split(/#+/);
	$ssh = ($h =~ s/^ALT://) ? $sshcmd2 : $sshcmd;
	$h = '' if $h =~ /[^-\w\.]/;
	$g = '' if $g =~ /[^-\w\.]/;
	$c = ($h =~ /^\s*-*\s*$/) ? $nullcmd : $ssh;
	$c =~ s/TL_HOST/$h/g; $c =~ s/TL_FQDN/`fqdn $h | tr -d '\\012'`/ge;
	$c =~ s/TL_NUM/$#grp + 2/ge;
	push(@grp, $g); push(@cmd, $c);
}

print(($#cmd == 0) ? &tmpl_1x1() :
      ($#cmd == 1) ? &tmpl_2x1() :
      ($#cmd == 2) ? &tmpl_3x1() :
      ($#cmd == 3) ? &tmpl_2x2() : &tmpl_3x2());

###############################################################################

sub tmpl_1x1 { <<EOF;
  [[$title]]
    [[[child0]]]
      fullscreen = False
      last_active_term = 03cb6d0d-b010-4500-9857-dc61c93252a3
      last_active_window = True
      maximised = False
      order = 0
      parent = ""
      position = 1338:613
      size = 578, 560
      title = $title
      type = Window
    [[[terminal1]]]
      command = $cmd[0]
      group = $grp[0]
      order = 0
      parent = child0
      profile = xterm+2
      type = Terminal
      uuid = 03cb6d0d-b010-4500-9857-dc61c93252a3
EOF
}

###############################################################################

sub tmpl_2x1 { <<EOF;
  [[$title]]
    [[[child0]]]
      fullscreen = False
      last_active_term = f65dfa0b-6476-4dbd-82b7-c30d440c40de
      last_active_window = True
      maximised = False
      order = 0
      parent = ""
      position = 142:4
      size = 1157, 394
      title = $title
      type = Window
    [[[child1]]]
      order = 0
      parent = child0
      position = 577
      ratio = 0.501739130435
      type = HPaned
    [[[terminal2]]]
      command = $cmd[0]
      group = $grp[0]
      order = 0
      parent = child1
      profile = xterm+2
      type = Terminal
      uuid = d229fa30-b3b4-473d-8bda-33aa26386898
    [[[terminal3]]]
      command = $cmd[1]
      group = $grp[1]
      order = 1
      parent = child1
      profile = xterm+2
      type = Terminal
      uuid = f65dfa0b-6476-4dbd-82b7-c30d440c40de
EOF
}

###############################################################################

sub tmpl_3x1 { <<EOF;
  [[$title]]
    [[[child0]]]
      fullscreen = False
      last_active_term = d229fa30-b3b4-473d-8bda-33aa26386898
      last_active_window = True
      maximised = False
      order = 0
      parent = ""
      position = 142:4
      size = 1740, 394
      title = $title
      type = Window
    [[[child1]]]
      order = 0
      parent = child0
      position = 578
      ratio = 0.334104046243
      type = HPaned
    [[[child3]]]
      order = 1
      parent = child1
      position = 574
      ratio = 0.499130434783
      type = HPaned
    [[[terminal2]]]
      command = $cmd[0]
      group = $grp[0]
      order = 0
      parent = child1
      profile = xterm+2
      type = Terminal
      uuid = d229fa30-b3b4-473d-8bda-33aa26386898
    [[[terminal4]]]
      command = $cmd[1]
      group = $grp[1]
      order = 0
      parent = child3
      profile = xterm+2
      type = Terminal
      uuid = f65dfa0b-6476-4dbd-82b7-c30d440c40de
    [[[terminal5]]]
      command = $cmd[2]
      group = $grp[2]
      order = 1
      parent = child3
      profile = xterm+2
      type = Terminal
      uuid = 63c444a2-ef37-424b-a574-2618d1e764c0
EOF
}

###############################################################################

sub tmpl_2x2 { <<EOF;
  [[$title]]
    [[[child0]]]
      fullscreen = False
      last_active_term = cd0cd81e-7e28-4086-b025-d362a94ec566
      last_active_window = True
      maximised = False
      order = 0
      parent = ""
      position = 142:4
      size = 1157, 1158
      title = $title
      type = Window
    [[[child1]]]
      order = 0
      parent = child0
      position = 577
      ratio = 0.501739130435
      type = HPaned
    [[[child2]]]
      order = 0
      parent = child1
      position = 577
      ratio = 0.500433651344
      type = VPaned
    [[[child5]]]
      order = 1
      parent = child1
      position = 577
      ratio = 0.500433651344
      type = VPaned
    [[[terminal3]]]
      command = $cmd[0]
      group = $grp[0]
      order = 0
      parent = child2
      profile = xterm+2
      type = Terminal
      uuid = cb53e9e8-9169-4a48-ad62-897d1ccd139d
    [[[terminal4]]]
      command = $cmd[2]
      group = $grp[2]
      order = 1
      parent = child2
      profile = xterm+2
      type = Terminal
      uuid = cd0cd81e-7e28-4086-b025-d362a94ec566
    [[[terminal6]]]
      command = $cmd[1]
      group = $grp[1]
      order = 0
      parent = child5
      profile = xterm+2
      type = Terminal
      uuid = 8ca79b58-5fe8-4b4d-9a41-c3567f0cd5c7
    [[[terminal7]]]
      command = $cmd[3]
      group = $grp[3]
      order = 1
      parent = child5
      profile = xterm+2
      type = Terminal
      uuid = ad56aa8a-953c-4a22-b696-906968af6985
EOF
}

###############################################################################

sub tmpl_3x2 { <<EOF;
  [[$title]]
    [[[child0]]]
      fullscreen = False
      last_active_term = cb53e9e8-9169-4a48-ad62-897d1ccd139d
      last_active_window = True
      maximised = False
      order = 0
      parent = ""
      position = 142:4
      size = 1740, 1158
      title = $title
      type = Window
    [[[child1]]]
      order = 0
      parent = child0
      position = 577
      ratio = 0.333526011561
      type = HPaned
    [[[child2]]]
      order = 0
      parent = child1
      position = 577
      ratio = 0.500433651344
      type = VPaned
    [[[child5]]]
      order = 1
      parent = child1
      position = 577
      ratio = 0.501303214596
      type = HPaned
    [[[child6]]]
      order = 0
      parent = child5
      position = 577
      ratio = 0.500433651344
      type = VPaned
    [[[child9]]]
      order = 1
      parent = child5
      position = 577
      ratio = 0.500433651344
      type = VPaned
    [[[terminal10]]]
      command = $cmd[2]
      group = $grp[2]
      order = 0
      parent = child9
      profile = xterm+2
      type = Terminal
      uuid = 1fa457c9-f0ca-4f3f-93be-8a88c37aac1c
    [[[terminal11]]]
      command = $cmd[5]
      group = $grp[5]
      order = 1
      parent = child9
      profile = xterm+2
      type = Terminal
      uuid = 93005de2-cf77-4a46-a120-fd5b68980e58
    [[[terminal3]]]
      command = $cmd[0]
      group = $grp[0]
      order = 0
      parent = child2
      profile = xterm+2
      type = Terminal
      uuid = cb53e9e8-9169-4a48-ad62-897d1ccd139d
    [[[terminal4]]]
      command = $cmd[3]
      group = $grp[3]
      order = 1
      parent = child2
      profile = xterm+2
      type = Terminal
      uuid = cd0cd81e-7e28-4086-b025-d362a94ec566
    [[[terminal7]]]
      command = $cmd[1]
      group = $grp[1]
      order = 0
      parent = child6
      profile = xterm+2
      type = Terminal
      uuid = 8ca79b58-5fe8-4b4d-9a41-c3567f0cd5c7
    [[[terminal8]]]
      command = $cmd[4]
      group = $grp[4]
      order = 1
      parent = child6
      profile = xterm+2
      type = Terminal
      uuid = ad56aa8a-953c-4a22-b696-906968af6985
EOF
}
