#!/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.
#

# 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/Xorg ]; then
  exit
fi

dialog --title "CONFIGURE X SERVER?" --yesno \
"If you like, X can attempt to probe for your video hardware and mouse, and \
write an initial configuration file to /etc/X11/xorg.conf.  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 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. \
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 xorg.conf file to \
xorg.conf.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/X -configure
if [ ! $? = 0 ]; then
  # failure, bail.
  exit
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

# OK, so now that we have a default file in $HOME/xorg.conf.new
# we can set up a default color depth:
dialog --title "SELECT DEFAULT COLOR DEPTH" \
  --menu "Now you may select a default color depth for the X server:" 13 70 6 \
  "24" "24 bit True Color" \
  "16" "16 bit Pseudo Color" \
  "8" "8 bit 256 Color" \
  "4" "4 bit 16 Color" \
  "1" "1 bit Mono B/W" \
  "none" "Go with driver default (usually 8-bit)" 2> $TMP/colordepth
if [ ! $? = 0 ]; then
  rm -f $TMP/colordepth
  exit
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
cat $HOME/xorg.conf.new | while read LINE ; do
  echo "$LINE" >> /etc/X11/xorg.conf
  if echo $LINE | grep Load | grep type1 1> /dev/null ; then
    # X -configure leaves out the freetype module.
    # We'll work around this.
    echo "Load  \"freetype\"" >> /etc/X11/xorg.conf
  fi
  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 [ ! "`cat $TMP/colordepth`" = "none" ]; then
        echo "DefaultDepth `cat $TMP/colordepth`" >> /etc/X11/xorg.conf
      fi
      START_LOOKING=false
    fi
  fi
done
rm -f $TMP/colordepth
rm -f $HOME/xorg.conf.new

dialog --title "X CONFIGURED" \
  --msgbox "Your new X configuration file has been saved to /etc/X11/xorg.conf. \
You may still need to add or adjust some values in the file to achieve the desired \
screen resolution.  For example, some monitors would require \"HorizSync 30-55\" in \
the \"Monitor\" section of the configuration file.  For complete information about \
making these adjustments, please refer to \"man xorg.conf\"." \
  11 66

