#!/bin/sh
# $Id: startx 2108 2006-08-29 02:54:27Z francis $
#
# remote_x
#
# This script will launch the Xserver on the workstation with
# a -qeary pointing back to the server.
#

PATH=/bin:$PATH; export PATH

# Be quiet if necessary
QUIET=false
for x in $(cat /proc/cmdline); do
        case $x in
        quiet)
                QUIET=true
                ;;
        esac
done

silent()
{
	if [ $QUIET = "true" ]; then
		if test -c /dev/null; then
			"$@" >/dev/null 2>&1
		else
			"$@" 2>&1 | read dummyvar
		fi
	else 
		"$@"
	fi
}

. /etc/ltsp_functions

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

DEFAULT_SERVER=${SERVER:-"UNKNOWN"}
XDM_SERVER=${XDM_SERVER:-${DEFAULT_SERVER}}
reg_info XDM_SERVER
#JMD:
export XFS_SERVER=${XFS_SERVER:-${XDM_SERVER}}

TTY=`/bin/basename \`/usr/bin/tty\`|cut -d"y" -f2`
XFCFG=/tmp/XF86Config.${TTY}
export TTY

if [ $# -lt 1 ]; then
    XF_ARGS=""
else
    XF_ARGS=$*
fi

#
# Swapoff & Swapon to clear the swap memory
#
if [ "${USE_NBD_SWAP}" = "Y" ]; then
    if [ -e /dev/nbd/0 ]; then
        NBD_DEVICE=/dev/nbd/0
    elif [ -e /dev/nbd0 ]; then
        NBD_DEVICE=/dev/nbd0
    else
	    echo "NBD device not found"
    fi
    swapoff $NBD_DEVICE
    swapon $NBD_DEVICE
fi

################################################################################
#
# 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 "  Using vesa driver by default"
	XSERVER="vesa"
    fi
fi
reg_info XSERVER

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

if [ ! -z "${XF86CONFIG_FILE}" ]; then
    reg_info XF86CONFIG_FILE
    #
    # 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 XFree86 4.x
    #
    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="X"
             XOPTS=""
             ;;
esac
#
# Setup the DISP variable
#
export DISP=:$(expr ${TTY} - 1).0

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

if [ "${XF_ARGS}" = "" ]; then
    if [ "${XDM_SERVER}" = "UNKNOWN" ];then
      XF_ARGS="-broadcast"
    else
      XF_ARGS="-query ${XDM_SERVER}"
    fi
fi

silent wget --no-check-certificate -O /tmp/xkey $XDM_SERVER/id_dsa.pub
#JMD: get the ssh public key from the appserver
if [ -s /tmp/xkey ]; then
  mkdir -p /root/.ssh
  cat /tmp/xkey >> /root/.ssh/authorized_keys2
  if [ -f /root/.ssh/authorized_keys2.orig ]; then
    cat /tmp/xkey >> /root/.ssh/authorized_keys2.orig
  fi
  rm -f /tmp/xkey
fi


# Note: adding the option "-terminate" to kill the x server when the user logout.

silent  /usr/X11R6/bin/${XBINARY} -terminate ${ACC_CTRL}             \
                              ${XF_ARGS}               \
                              -xf86config ${XFCFG}     \
                              vt${TTY} ${DISP} 
    XSTATUS=$?

if [ $XSTATUS -ne 0 ];then
    echo "X failed, Trying with VESA driver"
    /etc/build_x4_cfg vesa >${XFCFG}
    /usr/X11R6/bin/${XBINARY} -terminate ${ACC_CTRL}              \
                              ${XF_ARGS}               \
                              -xf86config ${XFCFG}     \
                              vt${TTY} ${DISP}
    XSTATUS=$?
fi

if [ $XSTATUS -ne 0 ];then
    echo -n -e "\n\rxserver failed, press <enter> to continue "
    read CMD
fi

