#!/bin/sh
##
##  screen-multiuser -- easy screen(1) multi-user session management
##  Copyright (c) 2004-2009 Ralf S. Engelschall <rse@engelschall.com>
##  Licensed under General Public License (GPL)
##

#   usage
if [ $# -eq 0 ]; then
    echo "screen-multiuser:ERROR: invalid arguments" 1>&2
    echo "screen-multiuser:USAGE: screen-multiuser master <session-name> [<servant-name> ...]" 1>&2
    echo "screen-multiuser:USAGE: screen-multiuser servant <session-name> <master-name>" 1>&2
    exit 1
fi

#   determine screen(1) and path to a temporary run-command script
screenrc="$HOME/.screenrc-multiuser"
screen="@l_prefix@/bin/screen"

#   dispatch run-time mode
mode="$1"
shift
case "$mode" in
    master )
        #   run a new session as the session master
        if [ $# -lt 1 ]; then
            echo "screen-multiuser:ERROR: invalid arguments to mode \"master\"" 1>&2
            echo "screen-multiuser:USAGE: $0 master <session-name> [<servant-name>[:<permission>] ...]" 1>&2
            exit 1
        fi
        session="$1"; shift
        master="${LOGNAME-${USERNAME-"unknown"}}"
        title="screen-multiuser:"
        title="${title} ${master}%?/%t%?"
        title="${title}%? %{= rW} %n %{-}%?"
        title="${title}%? [%u]%?"
        title="${title} %=%-w%{= rW}%50>%n %t%{-}%+w"
        (   if [ -f $HOME/.screenrc ]; then
                echo "source \"$HOME/.screenrc\""
            fi
            echo "escape          ^Aa"
            echo "startup_message off"
            echo "vbell           off"
            echo "defscrollback   10000"
            echo "defmonitor      off"
            echo "msgminwait      1"
            echo "msgwait         1"
            echo "hardstatus      alwayslastline \" $title \""
            echo "multiuser       on"
            echo "acladd          $master"
            echo "aclchg          $master +rwx \"#?\""
            echo "aclumask        ?+rwx"
            echo "aclumask        ??+rwx"
            if [ $# -gt 0 ]; then
                for servant in "$@"; do
                    permission="+rwx"
                    if expr "$servant" : '.*:.*' >/dev/null; then
                        permission=`expr "$servant" : '.*:\(.*\)'`
                        servant=`expr "$servant" : '\(.*\):.*'`
                    fi
                    echo "acladd $servant"
                    echo "aclchg $servant $permission \"#?\""
                done
            else
                echo "password"
            fi
        ) >$screenrc
        $screen -t "$session" -c $screenrc -S "$session" -d -R
        rm -f $screenrc >/dev/null 2>&1 || true
        ;;
    servant )
        #   attach to an existing session as one session servant
        if [ $# -ne 2 ]; then
            echo "screen-multiuser:ERROR: invalid arguments to mode \"servant\"" 1>&2
            echo "screen-multiuser:USAGE: $0 servant <session-name> <master-name>" 1>&2
            exit 1
        fi
        session="$1"; shift
        master="$1"; shift
        $screen -c $screenrc -r "$master/$session" -x
        ;;
    * )
        echo "screen-multiuser:ERROR: invalid mode \"$mode\"" 1>&2
        echo "screen-multiuser:USAGE: $0 master <session-name> [<servant-name>[:<permission>] ...]" 1>&2
        echo "screen-multiuser:USAGE: $0 servant <session-name> <master-name>" 1>&2
        exit 1
        ;;
esac

exit 0

=pod

=head1 NAME

B<screen-multiuser> -- easy screen(1) multi-user session management

=head1 SYNOPSIS

B<screen-multiuser> B<master> I<session-name> [I<servant-name>[:I<permission>] ...]

B<screen-multiuser> B<servant> I<session-name> I<master-name>

=head1 DESCRIPTION

B<screen-multiuser> is a tiny frontend to screen(1) for controlling
multi-user sessions. The idea is that a session master is opening a
screen(1) based multi-user session and one or multiple session servants
then join it afterwards.

=head1 EXAMPLE

 foo$ screen-multiuser master  support bar:+rwx baz:+r
 bar$ screen-multiuser servant support foo
 baz$ screen-multiuser servant support foo

=head1 AUTHOR

 Ralf S. Engelschall
 rse@engelschall.com
 www.engelschall.com

=cut

