#!/bin/sh
# Copyright 2001 BSDi, Inc, Concord, CA.  Written by volkerdi@slackware.com.
# Copyright 2004 Slackware Linux, Inc., Concord, CA.
# 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.
#
# Modified by rs Lange of VLocity Linux. Formally called xorgsetup
vdir=/sbin  ##$(dirname $0)
. $vdir/vasm-functions
check_root

if [ $DISPLAY ]; then
  DIALOG=Xdialog
else
  DIALOG=dialog
fi

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

# Now, this wouldn't make much sense either:
if [ ! -x /usr/bin/Xorg ]; then
  echo "No X found so bailing............"
  sleep 8
  exit
fi

$DIALOG --title "CONFIGURE X SERVER?" \
  --menu "Would you like to probe for your video hardware and mouse, \n
and write an initial configuration file to /etc/X11/xorg.conf? \n\n
The VXCONF option will give you the classic VL vxconf setup. \n
You need to use VXCONF for proprietary drivers or if you want to \n
specify depth or resolution. \n \n
The AUTO option will try to setup X for you with ZERO questions. \n
The vesa option works for most cards including virtual machines. \n
The fbdev option uses the kernel frame buffer device." 20 90 6 \
  "VXCONF" "Auto probe to setup xorg.conf" \
  "AUTO" "FULL-AUTO-PROBE no questions" \
  "VESA" "Use default xorg.conf-vesa" \
  "FBDEV" "Use default xorg.conf-fbdev" \
  "EXIT" "Exit and skip xorg.conf config" 2> $TMP/color
if [ ! $? = 0 ]; then
rm -f $TMP/color
exit 1
fi
choice=$(cat $TMP/color)
if [ "$choice" = "EXIT" ]; then
	rm -f $TMP/color
	exit 0
fi
if [ "$choice" = "AUTO" ]; then
	/sbin/VLsetup
	/sbin/VLXconf
	exit 0
fi
if [ "$choice" = "VESA" ]; then
	mv /etc/X11/xorg.conf /etc/X11/xorg.conf.back 2>/dev/null
  	ln -s /etc/X11/xorg.conf-vesa /etc/X11/xorg.conf
	exit 0
fi
if [ "$choice" = "FBDEV" ]; then
	mv /etc/X11/xorg.conf /etc/X11/xorg.conf.back 2>/dev/null
  	ln -s /etc/X11/xorg.conf-fbdev /etc/X11/xorg.conf
	exit 0
fi
if [ ! -d $TMP ]; then
 mkdir -p $TMP
 chmod 700 $TMP
fi

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

if ( ls /var/log/packages | grep -iq fglrx ) then
  #/usr/share/ati/fglrx-uninstall.sh >/dev/null && removepkg fglrx >/dev/null
 /sbin/installpkg /usr/src/vl-pkgs/lzma* >/dev/null
fi

# check for vmware video device
if ( lspci | grep -i vga | grep -iq vmware ) then
	cp /etc/X11/xorg.conf.in /$HOME/xorg.conf.new
	vmware=1
#check for ati video card
elif ( lspci | grep -i vga | grep -iq " ati " ) then
 fixati=1
 if [ -x $vdir/vlati ];then
  $vdir/vlati
    ati=1
 else
    ati=1
  # Have the X server create a default config file:
  echo "Detecting Hardware"
  /usr/bin/X -configure :2 2> /dev/null
  if [ ! $? = 0 ]; then
   # failure, bail.
   exit
  fi
 fi
else
#check for nvidia video card
  if ( lspci | grep -i vga | grep -iq nvidia ) then
    nvidia=1
  fi
#check for problem intel video
  if ( lspci | grep -i vga | grep -iq 82815 ) then
    i815=1
  fi
#check for problem trident video
  if ( lspci | grep -i vga | grep -iq CyberBlade ) then
    trident=1
  fi
#check for problem via video
  if ( lspci | grep -i vga | grep -iq VIA ) then
    via=1
  fi
  # Have the X server create a default config file:
  echo "Detecting Hardware"
  /usr/bin/X -configure :2 2> /dev/null
  if [ ! $? = 0 ]; then
   # failure, bail.
   exit
  fi
fi

##We may need horiz and vert refresh rates later so lets get them now
##novmsplice module blocks ddcxinfo so we disable it here for a second
rmmod novmsplice 2>/dev/null
ddcxinfo -hsync > $TMP/hsync
ddcxinfo -vsync > $TMP/vsync
##and reenable it here
modprobe novmsplice 2>/dev/null
vert=$(cat $TMP/vsync)
horiz=$(cat $TMP/hsync)
if [ "$vert" = "0-0" ] || [ "$vert" = "" ] ; then
  vert="40-90"
fi
if [ "$horiz" = "0-0" ] || [ "$horiz" = "" ]; then
  horiz="31.5-64"
fi
if [ "$trident" = 1 ];then
  vert="40-90"
  horiz="31.5 - 50.0"
fi
#check the keyboard
if [ "$XKB" = 1 ];then
$DIALOG --title "------------KEEP DEFAULT KEYBOARD MAP AND LAYOUT------------" \
--yesno "Would you like to KEEP the default (US) keyboard layout? \n
If you choose NO you can change keymap and keyboard type \n
from the default setting (US and pc102)." 8 70 2> /dev/null
if [ ! $? = 0 ]; then
  vxkbset
fi	
fi


# OK, so now that we have a default file in $HOME/xorg.conf.new
# we can set up a default color depth:
clear
$DIALOG --title "------------------SELECT DEFAULT COLOR DEPTH-------------------" \
  --menu "Please select your default color quality. \n
24 bit provides a high quality image with millions of colors. \n
If your video card can not achieve max resolution using 24 bit, \n
you may want to try 16 bit (High Color)." 18 70 7 \
  "24" "24 bit True Color" \
  "16" "16 bit Pseudo Color" \
  "8" "8 bit 256 Color" \
  "4" "4 bit 16 Color" \
  "1" "1 bit Mono (Black and White)" \
  "none" "Go with driver default (usually 8-bit)" 2> $TMP/colordepth
if [ ! $? = 0 ]; then
  rm -f $TMP/colordepth
  exit 1
fi
depth=$(cat $TMP/colordepth)
if [ "$via" = 1 ];then
 depth=16
fi
sleep 2
##############################################################
##now lets get a default screen resolution
clear
$DIALOG --title "----------------SELECT DEFAULT MONITOR RESOLUTION--------------------" \
 --menu "Please select your default screen resolution. \n
The resolution selected should be the best your monitor will handle. \n
AutoConfig will get the max for monitors with full DPMS support. \n
For most modern monitors a selection of 1024x768 is a good choice. \n
Older monitors may only be able to do 800x600." 20 80 10 \
"1024x768" "If you have a 15 or 17 inch monitor OK" \
"1280x1024" "Good for 19 inch CRT's or lcd screens" \
"1280x800" "New widescreen monitors and laptops" \
"1440x900" "Another common widescreen format" \
"1600x1200" "You know if you have this one...nice!" \
"1024x600" "8.9\" widescreen format" \
"1000x600" "8.9\" Via widescreen format" \
"800x480" "7\" widescreen format" \
"800x600" "Any SVGA monitor should be able to do this"  \
"640x480" "The most basic vga monitor" \
"1920x1080" "HDTV 1080p" \
"AutoConfig" "Choose this and X will figure it out" 2> $TMP/resolution
if [ ! $? = 0 ]; then
rm -f $TMP/resolution
exit 1
fi
res=$(cat $TMP/resolution)

##set resolution modes
if [ "$res" = "640x480" ]; then
  finalres="Modes \"640x480\" \"800x600\""
fi
if [ "$res" = "800x600" ]; then
  finalres="Modes \"800x600\" \"640x480\""
fi
if [ "$res" = "1024x768" ]; then
  finalres="Modes \"1024x768\" \"800x600\" \"640x480\""
fi
if [ "$res" = "1280x1024" ]; then
  finalres="Modes \"1280x1024\" \"1024x768\" \"800x600\" \"640x480\""
fi
if [ "$res" = "1440x900" ]; then
  finalres="Modes \"1440x900\" \"1280x800\" \"1024x768\" \"800x600\" \"640x480\""
fi
if [ "$res" = "1280x800" ]; then
  finalres="Modes \"1280x800\" \"1024x768\" \"800x600\" \"640x480\""
fi
if [ "$res" = "1600x1200" ]; then
  finalres="Modes \"1600x1200\" \"1280x1024\" \"1024x768\" \"800x600\" \"640x480\""
fi
if [ "$res" = "1920x1080" ]; then
  finalres="Modes \"1920x1080\" \"1600x1200\" \"1280x1024\" \"1024x768\" \"800x600\" \"640x480\""
fi
if [ "$res" = "800x480" ]; then
  finalres="Modes \"800x480\" \"640x480\""
fi
if [ "$res" = "1024x600" ]; then
  finalres="Modes \"1024x600\" \"800x480\" \"640x480\""
fi
if [ "$res" = "1000x600" ]; then
  finalres="Modes \"1000x600\" \"800x480\" \"640x480\""
fi

# I know this completely hoses the indentation of the xorg.conf file, but
# really don't know a good way around that.  Shoulda used perl.  ;)
START_LOOKING=false
rm -f /etc/X11/xorg.conf
. /etc/sysconfig/vlocity

if grep "HorizSync" $HOME/xorg.conf.new|grep -v "#";then
  fixsync="no"
else
  fixsync="yes"
fi
DPMS="   Option \"DPMS\"" 
##"$( sed -n '/^ *Section *\"Monitor\" *$/,/^ *EndSection *$/p' $HOME/xorg.conf.new | grep "DPMS")"

cat $HOME/xorg.conf.new | while read LINE ; do
  echo "$LINE" >> /etc/X11/xorg.conf
  if echo $LINE | grep Section | grep Screen 1> /dev/null ; then
    START_LOOKING=true
  fi
  if [ "$START_LOOKING" = "true" ]; then
   if echo $LINE | grep Monitor 1> /dev/null ; then
     if [ ! "$depth" = "none" ]; then
       echo "DefaultDepth $depth" >> /etc/X11/xorg.conf
     fi
     START_LOOKING=false
   fi
 fi
 ## add cyrillic fonts to font path
 if echo $LINE | grep 100dpi 1> /dev/null ; then
   echo "FontPath     \"/usr/lib/X11/fonts/truetype/\"
FontPath     \"/usr/lib/X11/fonts/local/\"
FontPath     \"/usr/lib/X11/fonts/cyrillic/\" " >> /etc/X11/xorg.conf
 fi
 ##lets try a default resolution
 if [ ! "$res" = "none" ]; then 
  if echo $LINE | grep ^Depth 1> /dev/null ; then
    echo $finalres >> /etc/X11/xorg.conf
  fi
 fi
if [ "$nvidia" = 1 ];then
    if echo $LINE | grep Section | grep Module\" 1> /dev/null ; then
      if ! cat $HOME/xorg.conf.new | grep Load | grep glx\" 1> /dev/null ; then
        echo Load  \"glx\" >>/etc/X11/xorg.conf
      fi
    fi
    if echo $LINE |grep Section|grep \"Device\" 1> /dev/null ; then
      echo  "Option \"AddARGBGLXVisuals\" \"True\" " >>/etc/X11/xorg.conf
      echo  "Option \"RenderAccel\" \"true\" " >>/etc/X11/xorg.conf
      echo  "Option \"AllowGLXWithComposite\" \"true\" " >>/etc/X11/xorg.conf
      echo  "Option \"USEdidDPI\" \"false\" " >>/etc/X11/xorg.conf
      echo  "Option \"DPI\" \"96 x 96\" " >>/etc/X11/xorg.conf
    fi
fi
if echo $LINE |grep Section|grep \"Monitor\" 1> /dev/null ; then
  grep $res /etc/X11/96DPI >>/etc/X11/xorg.conf
  echo "  Option \"UseEdidFreqs\" \"1\" " >>/etc/X11/xorg.conf
fi

 ##now the wheel for a mouse
#if echo $LINE | grep Device | grep mouse 1> /dev/null ; then
#  echo "Option  \"ZAxisMapping\" \"4 5\"" >> /etc/X11/xorg.conf
#fi

 
 ##now keyboard setting if none us
 if [ ! $XKBMAP ]; then
   XKBMAP=us
 fi
 if [ $XKBMAP ]; then
  if echo $LINE | grep Driver | grep kbd 1> /dev/null ; then
    echo "Option  \"XkbLayout\"  \"$XKBMAP\"    ## KEYBOARD_MAP!" >> /etc/X11/xorg.conf
  fi
 fi
# if [ $XKBMODEL ]; then
  if echo $LINE | grep Driver | grep kbd 1> /dev/null ; then
    echo "Option  \"XkbModel\"  \"$XKBMODEL\"    ## KEYBOARD_MODEL!" >> /etc/X11/xorg.conf
  fi
# fi
# if [ $XKBVAR ]; then
  if echo $LINE | grep Driver | grep kbd 1> /dev/null ; then
    echo "Option  \"Xkbvariant\"  \"$XKBVAR\"    ## KEYBOARD_VARIANT!" >> /etc/X11/xorg.conf
  fi

# fi
 ##last monitor refresh if not auto detected
#echo $fixsync
 if [ "$fixsync" = "yes" ]; then
  if echo $LINE | grep ModelName 1> /dev/null ; then
    echo "HorizSync $horiz" >> /etc/X11/xorg.conf
  fi
 fi
 if [ "$fixsync" = "yes" ]; then
  if echo $LINE | grep ModelName 1> /dev/null ; then
    echo "VertRefresh $vert" >> /etc/X11/xorg.conf
  fi
 fi				
done

rm -f $TMP/colordepth
rm -f $TMP/resolution
rm -f $TMP/vsync
rm -f $TMP/hsync
rm -f $HOME/xorg.conf.new
clear

##  Test for 2 video cards
if [ "$(grep "^Driver" /etc/X11/xorg.conf | grep -v kbd | grep -v mouse | wc | cut -d " " -f7)" -gt 1 ]; then
     cards=$(grep "^Vendor" /etc/X11/xorg.conf | grep -v Mon | cut -d " " -f3 | cut -c 2-)
     for i in $cards; do
       echo $i >>/tmp/cards
       card[$j]="$i"
       j=$(expr $j + 1)
     done
     $DCMD --backtitle "$BACKTITLE" --title "Choose Video Card" --menu "\n
     We have found more than 1 video card, and for X to work \n
     we need to know which card your monitor is connected to.\n
     Choose which video card you would like to use" \
     0 0 8 \
     ${card[0]} ${card[0]} \
     ${card[1]} ${card[1]} \
     ${card[2]} ${card[2]} \
     2> $freply

     cp /etc/X11/xorg.conf /etc/X11/xorg.conf.backup

     card1=$(cat $freply)
     card2=$(cat /tmp/cards | grep -v $card1)
     sed '/Card[0-2]/ d' /etc/X11/xorg.conf > /etc/X11/xorg.conf.1
     rm /etc/X11/xorg.conf
     cat /etc/X11/xorg.conf.1 | while read LINE ; do
       echo "$LINE" >> /etc/X11/xorg.conf.2
       if echo $LINE | grep $card1 1> /dev/null ; then
         echo Identifier  \"Card0\" >> /etc/X11/xorg.conf.2
       fi
       if echo $LINE | grep Section | grep Screen 1> /dev/null ; then
         echo Device     \"Card0\" >> /etc/X11/xorg.conf.2
       fi
     done
     cat /etc/X11/xorg.conf.2 | while read LINE ; do
       echo "$LINE" >> /etc/X11/xorg.conf
       if echo $LINE | grep $card2 1> /dev/null ; then
         echo Identifier  \"Card1\" >> /etc/X11/xorg.conf
       fi
     done
       rm /tmp/cards
       rm /etc/X11/xorg.conf.1
       rm /etc/X11/xorg.conf.2
fi

### intel i810 video has a problem without this hack
cat /etc/X11/xorg.conf | grep Driver | grep i810 1>/dev/null
# echo "##This is added to fix xfce terminal bug with i810 driver
# export XLIB_SKIP_ARGB_VISUALS=1" >>/etc/profile.d/vasm.sh
if [ $? = 0 ]; then
uname -r | cut -f 1,2 -d '.' > $TMP/kernel
kernel=$(cat $TMP/kernel)
rm -f $TMP/kernel
 if [ $kernel = "2.6" ]; then
  /sbin/modprobe intel-agp 2>/dev/null && echo "/sbin/modprobe intel-agp" >> /etc/rc.d/rc.modules
 fi
fi
cat /etc/X11/xorg.conf | grep Driver | grep intel 1> /dev/null
if [ $? = 0 ]; then
uname -r | cut -f 1,2 -d '.' > $TMP/kernel
kernel=$(cat $TMP/kernel)
rm -f $TMP/kernel
 if [ $kernel = "2.6" ]; then
  /sbin/modprobe intel-agp 2>/dev/null && echo "/sbin/modprobe intel-agp" >> /etc/rc.d/rc.modules
  /sbin/modprobe drm && echo 2>/dev/null "/sbin/modprobe drm" >> /etc/rc.d/rc.modules
  /sbin/modprobe i915 2>/dev/null && echo "/sbin/modprobe i915" >> /etc/rc.d/rc.modules
 fi
fi

fixati=0
if [ ! "$nvidia" = 1 ];then
 echo Section \"DRI\" >> /etc/X11/xorg.conf
 echo 	Mode         0666 >> /etc/X11/xorg.conf
 echo EndSection >> /etc/X11/xorg.conf
 echo "              " >> /etc/X11/xorg.conf
 echo Section \"Extensions\" >> /etc/X11/xorg.conf
 echo  Option \"Composite\" \"on\" >> /etc/X11/xorg.conf
 echo EndSection >> /etc/X11/xorg.conf
elif [ "$i810" = 1 ];then
 echo #Section \"Extensions\" >> /etc/X11/xorg.conf
 echo  #Option \"Composite\" \"on\" >> /etc/X11/xorg.conf
 echo #EndSection >> /etc/X11/xorg.conf
else
 echo Section \"Extensions\" >> /etc/X11/xorg.conf
 echo  Option \"Composite\" \"on\" >> /etc/X11/xorg.conf
 echo EndSection >> /etc/X11/xorg.conf
fi
if [ "$nvidia" = 1 ];then
  sed s'/Load  \"dri\"/#Load  \"dri\"/'g /etc/X11/xorg.conf > /etc/X11/xorg.conf.new
  mv /etc/X11/xorg.conf.new /etc/X11/xorg.conf
  if [ -x /sbin/vlnvidia ];then
    /sbin/vlnvidia
  else
    sed s'/vesa/nv/'g /etc/X11/xorg.conf > /etc/X11/xorg.conf.new
    mv /etc/X11/xorg.conf.new /etc/X11/xorg.conf
  fi
fi
if [ "$i810" = 1 ];then
  sed s'/Load  \"dri\"/#Load  \"dri\"/'g /etc/X11/xorg.conf > /etc/X11/xorg.conf.new
  mv /etc/X11/xorg.conf.new /etc/X11/xorg.conf
fi
  sed s'/4 5 6 7/4 5/'g /etc/X11/xorg.conf > /etc/X11/xorg.conf.new
  mv /etc/X11/xorg.conf.new /etc/X11/xorg.conf

sleep 1
if [ "$vmware" = 1 ];then
 sed s'/vesa/vmware/'g /etc/X11/xorg.conf > /etc/X11/xorg.conf.new
 mv /etc/X11/xorg.conf.new /etc/X11/xorg.conf
fi
if [ "$ati" = 1 ];then
  if grep "Driver" /etc/X11/xorg.conf|grep fglrx;then
    $DIALOG --title "X WAS CONFIGURED"  \
    --msgbox "Your new X configuration file has been saved to /etc/X11/xorg.conf. \n
    You have chosen to use the Proprietary FGLRX driver. This driver works for many newer ati radeon cards. However we can not test this driver without having you reboot Please reboot and check if this driver works. if not run vxconf from vasm and choose an alternate driver." \
    0 0
    exit 0
  fi
fi
dpms=$(sed -n '/^ *Section *\"Monitor\" *$/,/^ *EndSection *$/p' /etc/X11/xorg.conf | grep "DPMS")
if [ "$dpms" = "" ];then
  cat /etc/X11/xorg.conf | while read LINE ; do
    echo "$LINE" >> /etc/X11/xorg.conf.new
    if echo $LINE |grep Section|grep \"Monitor\" 1> /dev/null ; then
      echo $DPMS >> /etc/X11/xorg.conf.new
    fi
  done
  mv /etc/X11/xorg.conf.new /etc/X11/xorg.conf
fi

# Look for touchpad
if [ "$(grep -i touchpad /proc/bus/input/devices)" ]||[ "$(grep -i glidepoint /proc/bus/input/devices)" ];then
$DIALOG --title "-------Touchpad Detected-------" \
  --menu "Do you want to activate touchpad specific features?" 0 0 2 \
  "YES" "Use touchpad driver" \
  "NO" "Use basic mouse driver" 2> $TMP/touchpad
 if [ ! $? = 0 ]; then
  rm -f $TMP/touchpad
 fi
 if [ "$(cat $TMP/touchpad)" == "YES" ];then
  # add touchpad section in xorg.conf right above the Monitor section
  sed -i '/Section "Monitor"/ i\Section "InputDevice"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\ Identifier    "Synaptics Touchpad"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\ Driver        "synaptics"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\ Option        "SendCoreEvents"    "true"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\ Option        "Device"            "/dev/psaux"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\ Option        "Protocol"          "auto-dev"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\ Option        "SHMconfig"         "true"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\ Option        "HorizScrollDelta"  "0"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\ Option        "VertEdgeScroll"  "1"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\ Option        "TapButton1"  "1"' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/ i\EndSection' /etc/X11/xorg.conf
  sed -i '/Section "Monitor"/{x;p;x;}' /etc/X11/xorg.conf
  # add line in section ServerLayout
  sed -i '/X.org Configured/ a\InputDevice    "Synaptics Touchpad"' /etc/X11/xorg.conf
  rm -f $TMP/touchpad
 fi
 if [ "$(cat $TMP/touchpad)" == "NO" ];then
  rm -f $TMP/touchpad
 fi
fi


echo "Testing configuration...........hold tight!!!!"
if [ ! "$vmware" = 1 ];then
Xorg -probeonly :2 2> /dev/null
if [ "$?" = 0 ];then
$DIALOG --title "X WAS CONFIGURED SUCCESSFULLY"  \
--msgbox "Your new X configuration file has been saved to /etc/X11/xorg.conf. \n
You may still need to add or adjust some values in the file to \n
achieve the desired screen resolution. \n \n
For example, some monitors would require \"HorizSync 30-55\" in \n
the \"Monitor\" section of the configuration file. \n \n
For complete information about making these adjustments, \n
please refer to \"man xorg.conf\"." \
0 0
 exit 0

else
  $DCMD --backtitle "$BACKTITLE" --title "---------------X configuration ERROR----------------" --menu \
   "There was an error in your X configuration.\n
Autoconfig must have stumbled somewhere. \n
Please Choose one of the following actions." 0 0 4\
    "RETRY" "Retry X configuration" \
    "EDIT" "edit xorg.conf with mcedit" \
    "VESA" "try the default xorg.conf-vesa" \
    "Exit" "Exit X config and fix xorg.conf manually" \
    2> $freply
     result=$?
     if [ $result != 0 ];then
       exit 0
     fi
     ANS="`cat $freply`"
     case "$ANS" in
      "RETRY")
       /sbin/vxconf
       exit 0
       ;;
      "EDIT")
       /usr/bin/mcedit /etc/X11/xorg.conf
       exit 0
       ;;
      "VESA")
       mv /etc/X11/xorg.conf /etc/X11/xorg.conf.back 2>/dev/null
       ln -s /etc/X11/xorg.conf-vesa /etc/X11/xorg.conf
       exit 0
       ;;
      "EXIT")
       exit 0
       ;;
    esac
 exit 1
fi
fi
# yesnobox "There was an error in your X configuration. Would you like to edit the \n
# /etc/X11/xorg.conf file as autoconfig must have stumbled somewhere? \n
# Choosing yes here will start a text mode configuration utility. \n
# Choose no if you wish to edit later from the installation." 
# if [ $? = 0 ]; then
# /usr/bin/mcedit /etc/X11/xorg.conf
#	mv /etc/X11/xorg.conf /etc/X11/xorg.conf.back 2>/dev/null
#  	ln -s /etc/X11/xorg.conf-vesa /etc/X11/xorg.conf
 #/usr/X11R6/bin/xorgcfg -textmode
# fi

