#!/bin/bash

# This is /usr/sbin/xbase-configure; it is run whenever an X server
# configuration change might have occured.

# Rules:
# If the selected X server exists then
#   make sure /etc/X11/XF86Config exists; create if it doesn't
#   see if the user has indicated whether xdm should run the X server

# Arguments:
# If 'remove' is supplied as $1 then never prompt the user

# Variables:
#  interactive     1 if we're interactive
#  xserver         full path name of selected X server
#  xserverpresent  1 if we have an executable X server
#  xdmserver       what we know about whether to make xdm start a server:
#            0 = unknown
#            1 = definitely don't start a server
#  xconfig         1 if we should configure the X server
#  xconfigok       1 if the configuration file is ok

# Comments:
# I'm no good at shell scripts. Perhaps I should write this in perl instead.

set -e

xdmserver=0
xserverpresent=0

if [ "$1" = "remove" ]; then
    interactive=0
else
    interactive=1
fi

# Do we have an X server?
if [ -f /etc/X11/Xserver ]
then
    xserver=`head -1 /etc/X11/Xserver`
else
    xserver=/usr/X11R6/bin/XF86_NONE
fi

if [ -x $xserver ]
then
    xserverpresent=1
fi

if [ -e /etc/X11/XF86Config ]
then
    xconfigok=1
else
    xconfigok=0
fi

# If we have an X server, make sure it's configured.
# Don't do this if we're non-interactive
if [ $xserverpresent = 1 -a $interactive = 1 ]
then
    while [ $xconfigok = 0 ]
    do
	echo
	echo -n 'Do you want to create the XFree86 '
	echo -n 'configuration file? (y/n) [y] '
	read input
	if [ "$input" = "n" ]
	then
	    echo 'The XFree86 configuration file will not be created.'
	    echo 'To create it later, run /usr/sbin/xbase-configure as root.'
	    break
	fi

	if [ -e /usr/X11R6/bin/XF86Setup -a -e /usr/X11R6/bin/XF86_VGA16 ]; then
	    set +e
	    /usr/X11R6/bin/XF86Setup
	    set -e
	    if [ -f /etc/X11/XF86Config ]; then
		echo XF86Config created.
		xconfigok=1
	    else
		echo -e \\n\\nThe XF86Config file was not created.
		xconfigok=0
	    fi
	else
	    /usr/bin/X11/xf86config
	    if [ $? = 0 ]; then
		echo
		echo 'The XFree86 configuration file has been created.'
		echo
		echo -n 'Do you want to test the configuration? (y/n) [y] '
		read input
		if [ "$input" = "n" ]
		then
		    testconfig=0
		else
		    testconfig=1
		fi
		
		if [ $testconfig = 1 ]
		then
		    echo
		    echo 'The X server will start in 10 seconds. If the configuration'
		    echo 'file is correct, you should see a grey patterned screen'
		    echo 'with an X shaped cursor in the middle. The mouse should'
		    echo 'move the cursor.'
		    echo
		    echo 'If the virtual screen resolution is higher than the default'
		    echo 'one, you may not be able to see the cursor immediately.'
		    echo 'It should become visible when you move the mouse.'
		    echo
		    echo 'This display should disappear after another 10 seconds; you'
		    echo 'will then be asked whether the test worked.'
		    echo
		    echo 'Waiting 10 seconds before starting X server...'
		    sleep 10
		    echo 'Starting X server...'
		    /usr/X11R6/bin/X :9 &
		    xserverpid=$!
		    sleep 11
		    set +e
		    kill $!
		    set -e
		    sleep 3
		    echo -n 'Did the X server work properly? (y/n) [n] '
		    read input
		    if [ "$input" = "y" ]
		    then
			xconfigok=1
		    else
			xconfigok=0
			rm -f /etc/X11/XF86Config
		    fi
		else
		    # Assume the file is ok without testing
		    xconfigok=1
		fi
	    fi
	fi
    done
fi

if grep -q ^xdm-start-server /etc/X11/config
then
    xdmserver=2
fi

if grep -q ^no-xdm-start-server /etc/X11/config
then
    xdmserver=1
fi

if [ $xconfigok = 0 -o $xserverpresent = 0 ]
then
    xdmserver=1
fi

if [ $xdmserver = 0 ]
then
    echo
    echo -n 'Do you want xdm to run an X server on your display? (y/n) [y] '
    read input
    if [ "$input" = "n" ]
    then
	echo no-xdm-start-server >>/etc/X11/config
	xdmserver=1
    else
	echo xdm-start-server >>/etc/X11/config
	xdmserver=2
    fi
fi

if [ $xdmserver = 2 ]
then
    # Insert / keep any X server reference in /etc/X11/xdm/Xservers
    xcommand=":0 local /usr/X11R6/bin/X"
    sed '/^:.* local/d; /^#debian-not-configured/d' /etc/X11/xdm/Xservers \
	    >/etc/X11/xdm/Xservers.new
    echo $xcommand >> /etc/X11/xdm/Xservers.new
    mv /etc/X11/xdm/Xservers /etc/X11/xdm/Xservers.bak
    mv /etc/X11/xdm/Xservers.new /etc/X11/xdm/Xservers
else
    # Remove any references to the X server in /etc/X11/xdm/Xservers
    sed '/^:.* local/d; /^#debian-not-configured/d' /etc/X11/xdm/Xservers \
	    >/etc/X11/xdm/Xservers.new
    mv /etc/X11/xdm/Xservers /etc/X11/xdm/Xservers.bak
    mv /etc/X11/xdm/Xservers.new /etc/X11/xdm/Xservers
fi

exit 0
