#!/bin/sh
#
# rdesktop
#
# rdesktop screen script for the linux terminal server project.
# This script lives in the /opt/ltsp/i386/etc/screen.d directory
#
# Call it through lts.conf with the `SCREEN_xx = rdesktop` option.
#
# You can pass options to rdesktop 2 different ways.
#
#   1)  Place all of the options you want after the 'rdesktop' setting
#       on the screen_script.
#
#       For example:
#
#           SCREEN_01   = rdesktop -u jam w2kserver.ltsp.org
#
#
#   2)  Place the rdesktop options in the RDP_OPTIONS option
#       and the name or IP address of the rdp server in
#       the RDP_SERVER option.
#
#       For example:
#
#           RDP_OPTIONS	 = "-f -u jam"
#           RDP_SERVER   = w2kserver.ltsp.org
#           SCREEN_01    = rdesktop
#

PATH=/bin:$PATH; export PATH
. /etc/ltsp_functions

#
# Get the lts.conf entries
#
eval `/bin/getltscfg -a`

RDP_OPTIONS=${RDP_OPTIONS:-"-f"}

reg_info RDP_SERVER
reg_info RDP_OPTIONS

TTY=`/usr/bin/basename \`/usr/bin/tty\``
XFCFG=/tmp/XF86Config.${TTY}
export TTY

RDESKTOP_OPTIONS="${RDP_OPTIONS} $* ${RDP_SERVER}"

#   
# Setup the DISP variable
#   
export DISP=:$(expr ${TTY} - 1).0

################################################################################
#
# Setup the XF86Config file
#

XSERVER=${XSERVER:-"auto"}

if [ "${XSERVER}" = "auto" ]; then
    echo "Scanning for video card"
    XSERVER=`/sbin/pci_scan /etc/vidlist`
    if [ -z "${XSERVER}" ]; then
        echo
        echo "  ERROR: Auto probe of the video card failed !"
        echo "         You need to specify the proper X server in lts.conf"
        echo
        echo -n "Press <enter> to continue "
        read CMD
        exit 1
    fi
fi

if [ "${XSERVER}" = "i810" ]; then
    if [ ! -f /tmp/i810_loaded ]; then
        modprobe agpgart
        >/tmp/i810_loaded
    fi
fi

if [ ! -z "${XF86CONFIG_FILE}" ]; then
    #
    # If XF86CONFIG_FILE is defined in the lts.conf file, then
    # it points to an XF86Config file that is pre-made for a workstation
    #
    if [ -f /etc/${XF86CONFIG_FILE} ]; then
	cp /etc/${XF86CONFIG_FILE} ${XFCFG}
    else
        echo
        echo "Error! - ${XF86CONFIG_FILE} - File not found!"
        echo
    fi
else
    #
    # Build the XF86Config file from entries in the lts.conf file
    # If it starts with 'XF86_', then we use XFree86 3.3.6.  Otherwise,
    # we use Xorg
    #
    case ${XSERVER} in

        XF86_*)  /etc/build_x3_cfg ${XSERVER} >${XFCFG}
                 ;;

        *)       /etc/build_x4_cfg ${XSERVER} >${XFCFG}
                 ;;
    esac
fi

################################################################################
#
# Figure out how to run the X server
#
 
case ${XSERVER} in

    XF86_*)  XBINARY="${XSERVER}"
             XOPTS=""
             ;;

    Xvesa)   XBINARY="Xvesa"
             case "${X_MODE_0}" in
                 1280x1024)   XOPTS="-shadow -mode 0x011A"
                              ;;
                 1024x768)    XOPTS="-shadow -mode 0x0117"
                              ;;
                 800x600)     XOPTS="-shadow -mode 0x0114"
                              ;;
                 640x480)     XOPTS="-shadow -mode 0x0111"
                              ;;
                 *)           XOPTS="-shadow -mode 0x0117"
                              ;;
             esac
             ;;

    *)       XBINARY="Xorg"
             XOPTS=""
             ;;
esac

ACCESS_CONTROL=${DISABLE_ACCESS_CONTROL:-"N"}
if [ "${ACCESS_CONTROL}" = "Y" ]; then
    ACC_CTRL="-ac"
else
    ACC_CTRL=""
fi

clear
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
echo
#echo -n "                           Press <enter> to Login "
#read

if [ -w /proc/progress ]; then
    /usr/X11R6/bin/xinit      /usr/bin/rdesktop.wrapper           \
                              -u ""                       \
    			      ${RDESKTOP_OPTIONS}         \
			      -- /usr/X11R6/bin/${XBINARY}\
                              ${DISP}                     \
                              -xf86config ${XFCFG}        \
                              vt${TTY} >/dev/null 2>&1
else
    /usr/X11R6/bin/xinit      /usr/bin/rdesktop.wrapper           \
                              -u ""                       \
    			      ${RDESKTOP_OPTIONS}         \
			      -- /usr/X11R6/bin/${XBINARY}\
                              ${DISP}                     \
                              -xf86config ${XFCFG}        \
                              vt${TTY}
fi
