#!/bin/sh
#
# Screen script that launches xfreerdp/rdesktop. Can be called from lts.conf
# like this:
#   SCREEN_07="xfreerdp -f --plugin rdpsnd server"
# or like this:
#   SCREEN_07="rdesktop"
#
# Copyright (c) 2011 Alkis Georgopoulos <http://alkisg.mysch.gr>
#
# This software is licensed under the GNU General Public License version 2,
# the full text of which can be found in the COPYING file.

. /usr/share/ltsp/screen-x-common
# xfreerdp segfaults if HOME is unset.
export HOME=${HOME:-/root}

# The same screen script can be used for rdesktop too, by just symlinking
# screen.d/rdesktop to screen.d/xfreerdp.
basename=${0##*/}

if ! type $basename >/dev/null 2>&1; then
    echo "$basename couldn't be found."
    if [ "$basename" = "rdesktop" ]; then
        echo "Some distributions have now switched to 'xfreerdp' instead of 'rdesktop'."
        echo "You may want to change your SCREEN_XX entry to that instead."
        echo "Please note that RDP_OPTIONS will need updating for xfreerdp."
    fi
    read nothing
    exit 1
fi

# Make XINITRC_DAEMON default to "True", to prevent X from restarting after
# logout. If you don't want that, force XINITRC_DAEMON=False in lts.conf.
export XINITRC_DAEMON="${XINITRC_DAEMON-True}"

# If no parameters were passed, set some reasonable defaults.
if [ -z "$*" ]; then
	case $basename in
		rdesktop)
			RDP_OPTIONS=${RDP_OPTIONS:-"-f -u ''"}
			RDP_SERVER=${RDP_SERVER:-"server"}
			;;
		*)
			RDP_OPTIONS=${RDP_OPTIONS:-"/f"}
			RDP_SERVER=/v:${RDP_SERVER:-"server"}
			;;
	esac
fi

COMMAND="$basename $* $RDP_OPTIONS $RDP_SERVER"

# The following logic is described at the top of xinitrc.
exec xinit /usr/share/ltsp/xinitrc "$COMMAND" -- "$DISPLAY" "vt${TTY}" -nolisten tcp $X_ARGS >/dev/null
