#!/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, you can find it on the World Wide
#  Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
#  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#  MA 02110-1301, 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"

# Source ltsp-common-functions if we have not already (needed in some functions)
(PATH="" boolean_is_true True 2>/dev/null) || . /usr/share/ltsp/ltsp-common-functions || true

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

# Figure out SCREEN_NUM
if [ -n "$1" ]; then
    SCREEN_NUM="$1"
else 
    SCREEN_NUM="$(tty)"
fi

if [ -x /usr/bin/openvt ]; then
    openvt=/usr/bin/openvt
elif [ -x /bin/openvt ]; then
    openvt=/bin/openvt
fi

# initialize the tty with openvt, so that it's possible to switch to later.
if [ -x "${openvt}" ]; then
    ${openvt} -f -c ${SCREEN_NUM#0} /bin/true
fi

# Wait until this is the active vt before launching the screen script
while [ $(fgconsole) -ne ${SCREEN_NUM} ]; do
    sleep 2
done


export SCREEN_NUM

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|CURTTY_NUM' | /usr/bin/cut -d= -f1)
        . /usr/share/ltsp/ltsp_config
        eval $(getltscfg-cluster -a -l prompt)
    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 [ -z "${openvt}" ]; then
                return 2
            fi

            # Execute the session script in the correct VT 
            ${openvt} -s -f -w -c ${SCREEN_NUM#0} -- ${SCRIPT_DIR}/${SCREEN_SCRIPT} ${SCREEN_ARGS}&
            PID=$!
            wait $PID
            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:${SCREEN_NUM#0} - SCREEN_SCRIPT: ${SCREEN_SCRIPT} not found!"
            echo "Screen:${SCREEN_NUM#0} - SCREEN_SCRIPT: ${SCREEN_SCRIPT} not found!"
            exit 1
            ;;
        2)
            logger -t LTSP "Screen:${SCREEN_NUM#0} - openvt not found!  openvt is required for proper operation"
            echo "Screen:${SCREEN_NUM#0} - openvt not found!  openvt is required for proper operation"
            exit 1
            ;;
    esac
done
