#!/bin/sh
#
#  Copyright (c) 2002 McQuillan Systems, LLC
#
#  Author: James A. McQuillan <jam@McQuil.com>
#
#  2005, Matt Zimmerman <mdz@canonical.com>
#  2006, Oliver Grawert <ogra@canonical.com>
#  2007, Scott Balneaves <sbalneav@ltsp.org>
#  2008, Warren Togami <wtogami@redhat.com>
#        Stephane Graber <stgraber@ubuntu.com>
#        Vagrant Cascadian <vagrant@freegeek.org>
#        Gideon Romm <ltsp@symbio-technologies.com>
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
#  USA
#


# Deprecated: /usr/lib/ltsp/screen.d is for compatibility reasons, will be removed one day
SCRIPT_DIRS="/etc/ltsp/screen.d /usr/share/ltsp/screen.d /usr/lib/ltsp/screen.d"

# Load LTSP configuration
. /usr/share/ltsp/ltsp-common-functions
if [ "True" != "$LTSP_CONFIG" ]; then
    . /usr/share/ltsp/ltsp_config
fi

# Figure out SCREEN_NUM
if [ -n "$1" ]; then
    SCREEN_NUM="$1"
    TTY_NUM=${SCREEN_NUM#0}
    TTY=/dev/tty$TTY_NUM
    exec <$TTY >$TTY 2>&1           # redirect stdin/out/err to our tty
else
    # If screen_session is called from inittab/upstart, already with a
    # controlling terminal, then just figure out what screen number we are.
    TTY=`tty`
    case $TTY in
        /dev/tty?) SCREEN_NUM="0${TTY##/dev/tty}" ;;
        /dev/tty??) SCREEN_NUM="${TTY##/dev/tty}" ;;
    esac
fi

export SCREEN_NUM

if [ "${SCREEN_NUM}" = "01" ]; then
    # This is not an error on Fedora.
    if [ ! -e /etc/rwtab ]; then
        logger -t LTSP "Screen scripts should not be run on SCREEN_01"
        echo "SCREEN_01 Contains a screen script ${SCREEN_SCRIPT} ${SCREEN_ARGS} Please change to another screen"
        exit 1
    fi
fi

main() {
    if [ -f /etc/ltsp/getltscfg-cluster.conf ]; then
        # Reset the environement
        unset $(env | egrep '^(\w+)=(.*)$' | egrep -vw 'PWD|USER|PATH|HOME|SCREEN_NUM|SCREEN_DIRS|TTY_NUM' | /usr/bin/cut -d= -f1)
        . /usr/share/ltsp/ltsp_config
        eval $(getltscfg-cluster -a -l refresh)
    fi
    eval SCREEN_CMD=\$\{SCREEN_$SCREEN_NUM\}
    SCREEN_SCRIPT=`echo $SCREEN_CMD | cut -f1 -d" "`
    SCREEN_ARGS=`echo $SCREEN_CMD | cut -f2- -d" " -s`

    if [ -d "/usr/share/ltsp/screen-session.d/" ]; then
        for script in $(run_parts_list /usr/share/ltsp/screen-session.d/ S); do
            . $script
        done
    fi

    for SCRIPT_DIR in ${SCRIPT_DIRS}; do
        if [ -x ${SCRIPT_DIR}/${SCREEN_SCRIPT} ]; then
            if [ "${SCRIPT_DIR}" = "/usr/lib/ltsp/screen.d" ]; then
                echo "Warning: /usr/lib/ltsp/screen.d is deprecated and will be removed in the future."
                echo "         ${SCREEN_SCRIPT} requires updating to use /usr/share/ltsp."
            fi
            if [ -x /usr/bin/openvt ]; then
                openvt=/usr/bin/openvt
            elif [ -x /bin/openvt ]; then
                openvt=/bin/openvt
            else
                return 2
            fi
            ${openvt} -f -w -c ${TTY_NUM} -- ${SCRIPT_DIR}/${SCREEN_SCRIPT} ${SCREEN_ARGS}
            return 0
        fi
    done

    if [ -d "/usr/share/ltsp/screen-session.d/" ]; then
        for script in $(run_parts_list /usr/share/ltsp/screen-session.d/ K); do
            . $script
        done
    fi

    return 1
}

while :;
do
    main
    case "$?" in
        1)
            logger -t LTSP "Screen:${TTY} - SCREEN_SCRIPT: ${SCREEN_SCRIPT} not found!"
            echo "Screen:${TTY} - SCREEN_SCRIPT: ${SCREEN_SCRIPT} not found!"
            exit 1
            ;;
        2)
            logger -t LTSP "Screen:${TTY} - openvt not found!  openvt is required for proper operation"
            echo "Screen:${TTY} - openvt not found!  openvt is required for proper operation"
            exit 1
            ;;
    esac
done
