#!/bin/bash -x
# gt-screen - using screen within gnome-terminal (or at least that was the
# original idea).  

###############################################################################
### Configuration #############################################################
###############################################################################

CONFIG_DIR=$HOME/.config/screen
BASENAME=gt-screen

###############################################################################
### main () ###################################################################
###############################################################################

if [ "x$1" != "x" ]; then
    NAME=$1
else 
    NAME=$BASENAME
fi

if [ -f "$CONFIG_DIR/$NAME" ]; then
    CONFIG="$CONFIG_DIR/$NAME"
else 
    CONFIG="$CONFIG_DIR/default"
fi

SCREEN="screen -c $CONFIG -S $NAME"

ID="$2"

if [ "x$ID" == "x" ]; then
    exec $SCREEN -xRR $NAME
fi

PID=`$SCREEN -list | grep tached | head -1 | awk -F. ' { print $1 }'`
if [ "x$PID" == "x" ]; then
    $SCREEN -d -m 
    PID=`$SCREEN -list | grep tached | head -1 | awk -F. ' { print $1 }'`
fi

SCREEN_RUN="screen -xR $PID"

if [ "x$3" != "x" ]; then
    $SCREEN_RUN -X screen -T rxvt -t $ID 1 $3 $4 $5 $6 $7 $8 $9
fi

exec $SCREEN_RUN -p $ID

###############################################################################
### Documentation #############################################################
###############################################################################

# Documentation.  Use a hack to hide this from the shell.  Because of the
# above exit line, this should never be executed.
DOCS=<<__END_OF_DOCS__

=head1 NAME

gt-screen - use screen within gnome-terminal

=head1 SYNOPSIS

B<gt-screen> I<name>

B<gt-screen> I<name> I<window>

B<gt-screen> I<name> I<window> I<cmd>

=head1 DESCRIPTION

gt-screen is a script to manipulate groups of screens, for use in a windowed
environment (and originally within gnome-terminal itself).  

=head1 USAGE

=over 4

=item B<gt-screen> I<name>

Creates a new screen group named I<name> and connects to it.

=item B<gt-screen> I<name> I<window>

Connects to the existing window I<window> in the screen I<name>.

=item B<gt-screen> I<name> I<window> I<cmd>

Creates a new window I<window> within the screen I<name>, and runs I<cmd> in it.

=back

=head1 FILES

=over 4

=item B<~/.config/screen/*>

Configuration files for the screens.  If there is a file named I<name>, then
we will create new screen sessions with that config file.

=item B<~/.config/screen/default>

Default configuration file.

=back

=head1 SEE ALSO

screen(1)

=head1 AUTHOR

Tim Skirvin <tskirvin@stanford.edu>

=cut

__END_OF_DOCS__
