#!/usr/bin/perl
use strict;
use warnings;

sub sess_names {
    map { m{^ \s* (\d+) \. (.+?) \s+}smx } @_;
}

# Get list of detached/attached screen sessions and hostname
my @screen_out=qx(screen -list);

my %detached_sessions=reverse sess_names grep {m{\(Detached\)}} @screen_out;
my %attached_sessions=reverse sess_names grep {m{\(Attached\)}} @screen_out;

print <<'EOL' if %detached_sessions;
AddToMenu /Screens "Reattach first" StartTerminal screen -d -RR
EOL

print <<'EOL';
AddToMenu /Screens "New Session" StartTerminal screen
EOL

print <<'EOL' if %detached_sessions;
AddToMenu /Screens "" NoP
EOL

print <<"EOL" for sort keys %detached_sessions;
AddToMenu /Screens "$_ (a)" StartTerminal screen -r "$detached_sessions{$_}"
EOL

print <<'EOL' if %attached_sessions;
AddToMenu /Screens "" NoP
EOL

print <<"EOL" for sort keys %attached_sessions;
AddToMenu /Screens "$_ (d)" StartTerminal screen -d "$attached_sessions{$_}"
EOL
