#!/bin/sh
# This is /usr/sbin/xbase-configure; it may be called on its own or from
# xbase.postinst or xserver.postinst.

set -e

# 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
# if 'force' is supplied as $1 then perform configuration even if things look
#   all right

# Variables
#  boolean (0 = false, 1 = true)
#    interactive     we are interactive
#    preserveconf    preserve existing XF86Config
#    realxserver     xserver is not "XF86_NONE"
#    sourced         are we being sourced by another script?
#    testconfig      user wants to test configuration file
#    xconfig         we should configure the X server
#    xconfigok       the configuration file is ok
#    xdmserver       start XDM (not presently used)
#    xserverpresent  we have an executable X server
#  integer
#    count           loop counter for XF86Config configuration
#    xserverpid      process ID of X server being tested
#  string
#    arch            machine architecture
#    condecho        conditional echo; output formatting hack
#    input           user response to prompts
#    serverpath      full path name of selected X server

# condecho, count, amd command line args may be set from calling script

sourced=0
if [ "`basename $0`" != "xbase-configure" ]; then
  sourced=1
fi

# disabled for the time being
# xdmserver=0

# assume session is interactive if not called with "remove" argument
if [ "$1" = "remove" ]; then
  interactive=0
else
  interactive=1
fi

# configure even if things look okay to this script
if [ "$1" = "force" ]; then
  xconfig=1
else
  xconfig=0
fi

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

# migrate from older versions
if [ "$serverpath" = "/usr/X11R6/bin/XF86_NONE" ]; then
  serverpath=/usr/bin/X11/XF86_NONE
fi

if [ "$serverpath" = "/usr/bin/X11/XF86_NONE" ]; then
  realxserver=0
else
  realxserver=1
fi

# $xserverpresent will be 0 if $realxserver is 0, unless some yahoo
# made a /usr/bin/X11/XF86_NONE binary

if [ -x $serverpath ]; then
  xserverpresent=1
else
  xserverpresent=0
fi

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

# before going any further interactively, /etc/X11/Xserver must be sane
if [ $interactive -eq 1 ]; then
  if [ $realxserver -eq 0 ]; then
    ${condecho:=:}
    echo "The current X server is set to '$serverpath',"
    echo "which means that no X server package has been installed."
    echo "Please install an X server package unless you do not intend to run"
    echo "any X sessions locally.  Most people will want an X server installed."
    echo "If you wish to run an X server for which there is no package file,"
    echo "edit the first line of /etc/X11/Xserver to contain the full pathname"
    echo "of the desired X server binary."
    condecho=echo
  elif [ $xserverpresent -eq 0 ]; then
    $condecho
    echo "The desired X server, '$serverpath', was not found."
    echo "Please install an X server package unless you do not intend to run"
    echo "any X sessions locally.  Most people will want an X server installed."
    echo "If you wish to run an X server for which there is no package file,"
    echo "edit the first line of /etc/X11/Xserver to contain the full pathname"
    echo "of the desired X server binary."
    condecho=echo
  fi
fi

# if $xconfig = 1 then we're forcing a configuration run
if [ $xconfig -eq 1 ]; then
  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 -eq 1 -a $interactive -eq 1 ]; then
  while [ $xconfigok = 0 ]; do
    $condecho
    echo -n "Do you want to create the XFree86 configuration file? (y/n) [y] "
    condecho=echo
    read input
    case $input in
      ''|Y|y) ;;
      N|n)
        $condecho
        echo "The XFree86 configuration file will not be created."
        echo "To create it later, run /usr/sbin/xbase-configure as root."
        # scratch these lines when xbase-configure has a manpage
        echo "It may be necessary to add the \`force' argument, i.e."
        echo "/usr/sbin/xbase-configure force"
        # get out of here
        break
        ;;
      *)
        echo "'$input' not understood.  Using default of 'y'."
        ;;
    esac
    # is there already an /etc/X11/XF86Config?
    if [ -f /etc/X11/XF86Config ]; then
      if [ ${count:=0} -eq 0 ]; then
        $condecho
        echo "An /etc/X11/XF86Config is already present.  If this is a known"
        echo "good XFree86 configuration, or contains information you would"
        echo "like to preserve (modelines, for example), it can be backed up."
        echo
        echo -n "Preserve existing /etc/X11/XF86Config? (y/n) [y] "
      else
        echo -n "Preserve /etc/X11/XF86Config just generated? (y/n) [y] "
      fi
      condecho=echo
      read input
      case $input in
      ''|Y|y) preserveconf=1 ;;
      N|n) preserveconf=0 ;;
      *)
        echo "'$input' not understood.  Using default of 'y'."
	preserveconf=1
	;;
      esac
      if [ $preserveconf -eq 1 ]; then
        $condecho
        echo -n "Saving current XF86Config in "
        echo "/etc/X11/XF86Config.debian.$$.$count."
        condecho=echo
        mv /etc/X11/XF86Config /etc/X11/XF86Config.debian.$$.$count
      fi
    fi

    arch=$(dpkg --print-installation-architecture)
    if [ "$arch" = "m68k" ]; then
      cp /usr/X11R6/lib/X11/XF86Config.eg /etc/X11/XF86Config
      $condecho
      echo "The new XF86Config file has been created in /etc/X11/.  Please note"
      echo "that this is only a \"template\" file which might require further"
      echo "editing as there is currently no XFree86 setup program for"
      echo "Linux/m68k.  If, while editing this file, you make a mistake, you"
      echo "can find a fresh template in /usr/X11R6/lib/X11/XF86Config.eg."
      condecho=echo
      # done
      xconfigok=1
    elif [ "$arch" = "i386" -o "$arch" = "alpha" ]; then
      if [ -e /usr/bin/X11/XF86Setup -a -e /usr/bin/X11/XF86_VGA16 -a -e \
           /usr/lib/X11/fonts/misc/fonts.dir -a -e \
           /usr/lib/X11/fonts/75dpi/fonts.dir ]; then
        # XF86Config, if it exists, was already backed up; we get rid of it
        # so the subsequent test will be useful
        if [ -e /etc/X11/XF86Config ]; then
          rm /etc/X11/XF86Config
        fi
        set +e
        # we cannot know if xlib6g's postinst has already been run
        LD_LIBRARY_PATH=/usr/X11R6/lib /usr/bin/X11/XF86Setup
        set -e
        echo
        if [ -f /etc/X11/XF86Config ]; then
          $condecho
          echo "The /etc/X11/XF86Config file was created."
          condecho=echo
          # done
          xconfigok=1
        else
          $condecho
          echo "The /etc/X11/XF86Config file was not created."
          condecho=echo
          xconfigok=0
        fi
      else
        set +e
        /usr/bin/X11/xf86config
        set -e
        if [ $? -eq 0 ]; then
          $condecho
          echo "The XFree86 configuration file has been created."
          echo
          echo -n "Do you want to test the configuration? (y/n) [y] "
          condecho=echo
          read input
	  case $input in
	    ''|Y|y) testconfig=1 ;;
	    N|n) testconfig=0 ;;
	    *)
	      echo "'$input' not understood.  Using default of 'y'."
	      testconfig=1
	      ;;
	  esac
          if [ $testconfig -eq 1 ]; then
            $condecho
            echo "The X server will start in 10 seconds.  If the configuration"
            echo "file is correct, you should see a gray patterned screen with"
            echo "an X shaped cursor in the middle.  The mouse should move the"
            echo "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.  It"
            echo "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 &
            xserverpid=$!
            sleep 10
            set +e
            kill $!
            set -e
            sleep 3
            echo
            echo -n "Did the X server work properly? (y/n) [n] "
            condecho=echo
            read input
	    case $input in
	      Y|y) xconfigok=1 ;;
	      ''|N|n) xconfigok=0 ;;
	      *)
	        echo "'$input' not understood.  Using default of 'n'."
		xconfigok=0
		;;
	    esac
          else
            # user didn't want to test X server; assume the file is ok
            xconfigok=1
          fi
        fi # if xf86config exited ok
      fi # if exist XF86Setup and XF86_VGA16
    fi # if arch...
  count=`expr $count + 1`
  done
  # offer to clean up those crufty files
  if [ `echo /etc/X11/XF86Config.debian.$$.*` != \
       "/etc/X11/XF86Config.debian.$$.*" ]; then
    $condecho
    echo "Remove preserved XF86Config files generated in this session (files"
    echo "matching /etc/X11/XF86Config.debian.$$.* -- this will not remove the"
    echo -n "\"official\" X server configuration file, /etc/X11/XF86Config)? "
    echo -n "(y|n) [n] "
    condecho=echo
    read input
    case $input in
      Y|y) rm -f /etc/X11/XF86Config.debian.$$.* ;;
      ''|N|n) ;; 
      *)
        echo "'$input' not understood.  Using default of 'n'."
        ;;
    esac
  fi
fi

# if grep xdm-start-server /etc/X11/config; then
#   if grep -q ^xdm-start-server /etc/X11/config; then
#     xdmserver=1
#   elif grep -q ^no-xdm-start-server /etc/X11/config; then
#     xdmserver=0
# else
#   # prompt for decision if things are otherwise all right
#   if [ $xconfigok = 1 -a $xserverpresent = 1 ]; then
#     echo
#     echo -n 'Do you want xdm to run an X server on your display? (y/n) [n] '
#     read input
#     if [ "$input" = "y" ]
#     then
# 	echo xdm-start-server >> /etc/X11/config
# 	xdmserver=0
#     else
# 	echo no-xdm-start-server >> /etc/X11/config
# 	xdmserver=1
#     fi
# fi

if [ $sourced = 0 ]; then
  exit
fi
