#!/bin/sh
#
# Copyright (c) 2002 by James A. McQuillan (McQuillan Systems, LLC)
#
# This software is licensed under the Gnu General Public License version 2,
# the full text of which can be found in the COPYING file.
#

#
# Get the lts.conf entries
#
. /usr/lib/ltsp/ltsp_config

XDM_SERVER=${XDM_SERVER:-${SERVER}}

TTY=$(basename $(tty))
TTY=${TTY##tty}
export TTY

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

# XXX
PATH=$PATH:/usr/bin/X11

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

ltsp_x_config() {
    XSERVER=${XSERVER:-"auto"}


    if [ "${XSERVER}" = "auto" ]; then
        echo "Scanning for video card"
        XSERVER=`/sbin/pci_scan /etc/vidlist`
        if [ -z "${XSERVER}" ]; then
            echo
            echo "  WARNING: Auto probe of the video card failed !"
            echo "           You should specify the proper X server in lts.conf"
            echo "           Proceeding with the SLOW vesa server"
            echo 
            sleep 5s
            XSERVER=vesa
        fi
    fi

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

    if [ ! -z "${X_CONF}" ]; then
    #
    # If X_CONF is defined in the lts.conf file, then
    # it points to an XF86Config file that is pre-made for a workstation
    #
        if [ -f ${X_CONF} ]; then
            cp ${X_CONF} ${XFCFG}
        else
            echo
            echo "Error! - ${X_CONF} - 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
}

ubuntu_x_config() {
    # set default xserver, use same selection method as in
    # ltsp-client.ltsp-client-setup.init
    if [ -d /usr/share/doc/xserver-xorg/ ]; then
	XFCFG=/etc/X11/xorg.conf
    elif [ -d /usr/share/doc/xserver-xfree86/ ]; then
	XFCFG=/etc/X11/XF86Config-4
    else
        echo "WARNING: no known xserver detected"
        return
    fi
}

if true; then
    ubuntu_x_config
else
    ltsp_x_config
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"}
if [ "${DISABLE_ACCESS_CONTROL}" = "Y" ]; then
    ACC_CTRL="-ac"
else
    ACC_CTRL=""
fi

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

# Do this here instead of updating the configuration file, until
# Debian bug #323262 is fixed.
if [ Y = "${USE_XFS}" ] ; then
    if [ -z "${XFS_SERVER}" ] ; then
	XFS_SERVER="${SERVER}"
    fi
    XF_ARGS="$XF_ARGS -fp tcp/${XFS_SERVER}:7100"
fi

while :; do
    ${XBINARY} ${ACC_CTRL}              \
                              ${XF_ARGS}               \
                              -xf86config ${XFCFG}     \
                              vt${TTY} ${DISP}
    if [ $? -ne 0 ]; then
      echo -n -e "\n\rxserver failed, press <enter> to continue "
      read CMD
    fi
done
