#!/bin/sh
# Copyright 2001 BSDi, Inc, Concord, CA.  Written by volkerdi@slackware.com.
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#  This script was heavily modified by ROBERT LANGE vec7@vectorlinux.com for use with the 
#  VECTOR LINUX distribution. The above conditions and disclaimer still apply.



#  We are setting $HOME too /root so this will work wih the installation also.
HOME=/root 

# If we aren't root, bail:
if [ "$USER" = "root" ]; then
  TMP=/var/log/setup/tmp
else
  echo "Only root can configure X."
  exit
fi

# Now, this wouldn't make much sense either:
if [ ! -r /usr/X11R6/bin/XFree86 ]; then
  exit
fi

dialog --title "CONFIGURE X SERVER?" --yesno \
"If you like, XFree86 can attempt to probe for your video hardware and mouse, and \
write an initial configuration file to /etc/X11/XF86Config.  Would you like to do \
this now?" 7 66
if [ ! $? = 0 ]; then
  exit
fi

if [ ! -d $TMP ]; then
 mkdir -p $TMP
 chmod 700 $TMP
fi

# OK, we'll warn the user if there's already an existing XF86Config:
CONFIG_EXISTS=false
for xf86config in /etc/X11/XF86Config-4 /etc/XF86Config-4 /usr/X11R6/lib/X11/XF86Config-4 $HOME/XF86Config-4 ; do
  if [ -r $xf86config ]; then
    CONFIG_EXISTS=$xf86config
  fi  
done
if [ ! "$CONFIG_EXISTS" = "false" ]; then
  dialog --title "FOUND EXISTING XF86Config in `dirname $CONFIG_EXISTS`" \
  --msgbox "A previous X Window System configuration file has been found. \
You can now reconfigure X, replacing the file with a new version (and \
keeping a backup of the old file), or you can abort leaving the existing \
config file in place.  Hit ENTER to rename the XF86Config file to \
XF86Config.backup and create a new one, or ESC to abort." 9 72
  if [ ! $? = 0 ]; then
    exit
  fi
fi

# Have the X server create a default config file:
/usr/X11R6/bin/XFree86 -configure
if [ ! $? = 0 ]; then
  # failure, bail.
  exit
fi

# Move any existing config file(s) aside:
for xf86config in /etc/X11/XF86Config-4 /etc/XF86Config-4 /usr/X11R6/lib/X11/XF86Config-4 $HOME/XF86Config-4 ; do
  if [ -r $xf86config ]; then
    mv $xf86config ${xf86config}.backup
  fi  
done

mv $HOME/XF86Config.new /etc/X11/XF86Config
# Okay now we edit the little bugger to make it more usable
xf86cfg -textmode

# I know this completely hoses the indentation of the XF86Config file, but
# really don't know a good way around that.  Shoulda used perl.  ;)

cat /etc/X11/XF86Config | while read LINE ; do
  echo "$LINE" >> /etc/X11/XF86Config-4
  if echo $LINE | grep Load | grep type1 1> /dev/null ; then
    # X -configure is pretty broken... it leaves out the type1 and freetype
    # modules.  We'll work around this.  BTW, it matters not if these appear
    # multiple times, but matters A LOT if they don't appear at all.
    # echo "Load  \"type1\"" >> /etc/X11/XF86Config-4
    echo "Load  \"freetype\"" >> /etc/X11/XF86Config-4
  fi
# lets add wheel mouse support won't hurt if no wheel
    if echo $LINE | grep Option | grep Device | grep /dev/mouse 1> /dev/null ; then
        echo "Option     \"ZAxisMapping\"  \"4 5\"" >> /etc/X11/XF86Config-4
      fi
      
  done
rm -f $HOME/XF86Config.new
rm -f /etc/X11/XF86Config
# make it so normal users can use this config file

echo "Section \"DRI\"" >> /etc/X11/XF86Config-4
echo "	Mode 0666" >> /etc/X11/XF86Config-4
echo "EndSection" >> /etc/X11/XF86Config-4


dialog --title "XFree86 CONFIGURED" \
  --msgbox "Your new XFree86 configuration file has been saved to /etc/X11/XF86Config-4. \
You may still need to add or adjust some values in the file to achieve the desired \
results. For complete information about \
making these adjustments, please refer to \"man XF86Config\"." \
  11 66

