#!/bin/sh
# @vasm : vxconf
# @level: root
# @description: Set up X.org configuration
# 
# (c) Eko M. Budi, 2003
# (c) Vector Linux, 2003
#
# Released under GNU GPL

vdir=$(dirname $0)
source $vdir/vasm-functions

dbug "Starting $0"

check_root

# Properties
config_file="/etc/X11/xorg.conf"
source_file="$HOME/xorg.conf.new"

basic_file=""
old_file=""
new_file=""

xprog=""
xprogconfig=""

## Things that we need to change
XMOUSE="WHEEL"
XDEPTH="24"
XMODES='"800x600" "640x480"'
XDRIVER="unknown"
XBOARD="Generic"
XDRI=""

function initialize()
{
   ## Find the program
   if which Xorg &> /dev/null; then
      xprog="Xorg"
      xprogconfig="Xorg -configure"
      config_file="/etc/X11/xorg.conf"
      new_file="$HOME/xorg.conf.new"
   elif which XFree86 &> /dev/null; then
      xprog="XFree86"
      xprogconfig="XFree86 -configure"
      config_file="/etc/X11/XF86Config-4"
      new_file="$HOME/XF86Config.new"
   else
      errorbox "I cannot find X-Window system.\nCheck installation or PATH"
      clean_exit 1
   fi

   ## Find old configuration
   if [ -f /etc/X11/xorg.conf ]; then
      old_file="/etc/X11/xorg.conf"
   elif [ -f /etc/X11/XF86Config ]; then
      old_file="/etc/X11/XF86Config"
   elif [ -f /etc/X11/XF86Config-4 ]; then
      old_file="/etc/X11/XF86Config-4"
   fi

   ## Find VESA
   if [ -f /etc/X11/xorg.conf-vesa ]; then
      basic_file="/etc/X11/xorg.conf-vesa"
   elif [ -f /etc/X11/XF86Config-vesa ]; then
      basic_file="/etc/X11/XF86Config-vesa"
   fi
   
}

# Autodetect video mode
function auto_detect()
{
  # this will produce the initial xorg.conf
  clear
  echo "Autodetecting X-Window"
  echo "Hold tight ... the screen will be shaken a bit !!!"
  sleep 3
  $xprogconfig
  return $?
}

# Get configuration from the source
# This will read from stdin, so it must be called
# get_config < the_file
# Cheap ??? Well, I figured it out after 6 months 
# banging head why I cannot set variables using
# "cat the_file | while read line; do ... done".
# This one does.
function get_config()
{
  while read line; do
    if echo $line | grep -qe '\<Section ' ; then
      section=$(echo $line | cut -f 2 -d '"')
      dbug $line
    elif echo $line | grep -qe '\<EndSection' ; then
      section=""
    elif [ "$section" = "Device" ]; then
      if echo $line | grep -qe '^\<Driver'; then
        XDRIVER=$(echo $line | cut -f 2 -d '"')
	dbug "Driver $XDRIVER"
      elif echo $line | grep -qe '^\<BoardName'; then
	XBOARD=$(echo $line | cut -f 2 -d '"')
	dbug "BoardName $XBOARD"
      fi
    fi
  done
}

# Modify the initial x.org conf
function set_config()
{
   if [ -f $config_file ]; then
      cp $config_file  "${config_file}.backup"   
   fi   
   echo '## xorg.conf' > $config_file
   echo '## generated by Xorg -configure and vxconf' >> $config_file
   section=""
   subsection=""
   indent=""
   cat $source_file | while read line; do
     if echo $line | grep -qe '\<Section ' ; then
        section=$(echo $line | cut -f 2 -d '"')
	indent="    "
        echo $line >> $config_file
	dbug $line
        if [ "$section" = "DRI" ] ; then
          XDRI="no";
        fi
     elif echo $line | grep -qe '\<EndSection' ; then
        echo $line >> $config_file
        section=""
	indent=""
     elif echo $line | grep -qe '\<SubSection' ; then
        echo "$indent$line" >> $config_file
	dbug $line
	indent="        "
        subsection=$(echo $line | cut -f 2 -d '"')
     elif echo $line | grep -qe '\<EndSubSection' ; then
        subsection=""
	indent="    "
        echo "$indent$line" >> $config_file
     elif [ "$section" = "InputDevice" ]; then
        if echo $line | grep -qe '\<Driver *"mouse"' ; then
          if [ "$XMOUSE" = "WHEEL" ]; then 
	    echo "$indent"'Option          "ZAxisMapping" "4 5"' >> $config_file
	    dbug 'Option    "ZAxisMapping" "4 5"'
	  elif [ "$XMOUSE" = "2" ]; then
	    echo "$indent"'Option          "Emulate3Buttons" "true"' >> $config_file
	    dbug 'Option      "Emulate3Buttons" "true"'
	  fi
	elif echo $line | grep -qe 'Emulate3Buttons' || \
	     echo $line | grep -qe 'ZAxisMapping' ; then
	  continue
	fi
        echo "$indent$line" >> $config_file 
     elif [ "$section" = "Device" ]; then
        if echo $line | grep -qe '\<Driver'; then
          driver=$(echo $line | cut -f 2 -d '"')
          ## Fix sw_cursor
	  case $driver in ati|radeon|trident|neomagic) 
            echo "$indent"'Option    "sw_cursor"' >> $config_file
	    dbug 'Option      "SWcursor"   "true"'
	  esac 
	  ## add DRI
	  if [ -z "$XDRI" ]; then
	     case $driver in radeon|sis|mga|r128|nv|tdfx|i810|via) 
               XDRI="yes"
	     esac 
	  fi   
	elif echo $line | grep -qe 'sw_cursor'; then
	  continue
	fi
	echo "$indent$line" >> $config_file
     elif [ "$section" = "Screen" ] ; then
         if echo $line | grep -qe '\<Monitor '; then
            echo "    DefaultDepth $XDEPTH" >> $config_file
	    dbug "DefaultDepth $XDEPTH"
         elif [ "$subsection" = "Display" ]; then
            if echo $line | grep -qe '\<Depth'; then
               echo "${indent}Modes $XMODES" >> $config_file
	       dbug "Modes $XMODES"
	    elif echo $line | grep -qe 'Modes' ; then
	       continue
	    fi
         elif echo $line | grep -qe 'DefaultDepth'; then
	    continue
	 fi
         echo "$indent$line" >> $config_file
      else
         echo "$indent$line" >> $config_file
      fi
   done
   
   ## Add DRI if not set yet
if [ "$XDRI" = "yes" ]; then
echo '
Section "DRI"
    Mode 0666
EndSection' >> $config_file
fi
   
   [ "$new_file" ] && rm -f $new_file   
}

##################################################
# AUTO DETECT
function menuA1()
{
dbug "Autodetect"
TITLE="AUTODETECT X-WINDOW"
if [ -z $(skill -n X) ]; then 
  auto_detect
  if [ $? = 0 ] && [ -r $new_file ]; then
    source_file=$new_file
    get_config < $source_file
    return 0
  fi
  errorbox "Sorry, unable to autodetect X-Window.\nTry VESA."
else
  case $(runlevel | cut -f 2) in 
    4|5) 
TEXT="\
Cannot autodetect because the GUI system is running.\n
Please change run level first.\n
  - go to the console (press Ctrl-Alt-F1)\n
  - login as root\n
  - call init 2\n
  - call $0\n"
DIMENSION="12 60"
;;
    *)
TEXT="\
Hmmm ... X is running. Cannot autodetect.\n
Please kill or logoff from it first.\n"
DIMENSION="8 60"
  esac

  $DCMD --backtitle "$BACKTITLE" --title "ERROR" --msgbox "$TEXT" $DIMENSION 
  return 1
fi
}

##################################################
# Use OLD
function menuA2()
{
    if [ "$old_file" ] && [ -r $old_file ]; then
        source_file="${old_file}.backup"
        cp $old_file ${source_file}
	get_config < $source_file
        return 0
    fi
    errorbox "Cannot find the old configuration file.\nTry AUTO or VESA."
    return 1
}

##################################################
# Use VESA
function menuA3()
{
    if [ "$basic_file" ]; then
        source_file="$basic_file"
	get_config < $source_file
        return 0
    fi
    errorbox "Cannot find the basic VESA config.\nSomebody has removed it !!!"
    return 1
}

##################################################
# Ask method
function menuA() {

while [ 1 ]; do
TITLE="X-WINDOW CONFIGURATION"
TEXT="\n
Welcome to X-Window configurator. It is supposed\n
to be an easy way to make X-Window works for you.\n
Finger crossed :)\n\n
First, select what config do you want to use."
DIMENSION="15 60 3"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
"AUTO"   "Autodetect a new configuration" \
"CURRENT" "Update the current configuration" \
"BASIC"   "Use the basic (VESA) configuration" \
2> $freply

  status=$?
  [ $status != 0 ] && return $status
  
  reply=$(cat $freply)
  case $reply in
    AUTO)
      menuA1
      ;;
    CURRENT)
      menuA2
      ;;
    BASIC)
      menuA3
      ;;
  esac 
  [ $? = 0 ] && return 0
done;
}

########################################################
# Ask resolution
function menuB() {
TITLE="SET SCREEN RESOLUTION"
TEXT="\n
X-Window will be configured with:\n
  Driver=  $XDRIVER\n
  Board = $XBOARD\n\n
Select the largest screen resolution you wish to use:"
DIMENSION="20 76 8"

$WCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
"640"  '640x480   Low resolution' \
"800"  '800x600   Medium resolution' \
"1024" '1024x768  High resolution' \
"1152" '1152x864  High resolution' \
"1280" '1280x1024 High resoulution' \
'1600' '1600x1200 Very high resolution' \
"1800" '1800x1440 I wish I have this one' \
2> $freply

status=$?
[ $status != 0 ] && return $status

case $(cat $freply) in
 '1800')
 XMODES='"1800x1440" "1600x1200" "1280x1024" "1152x864" "1024x768" "800x600" "640x480"'
 ;;
 '1600')
 XMODES='"1600x1200" "1280x1024" "1152x864" "1024x768" "800x600" "640x480"'
 ;;
 '1280')
 XMODES='"1280x1024" "1152x864" "1024x768" "800x600" "640x480"'
 ;;
 '1152')
 XMODES='"1152x864" "1024x768" "800x600" "640x480"'
 ;;
 '1024')
 XMODES='"1024x768" "800x600" "640x480"'
 ;;
 '800')
 XMODES='"800x600" "640x480"'
 ;;
 *)
 XMODES='"640x480"'
 ;;
esac
dbug "XMODES=$XMODES"

return 0
}

########################################################
# Ask depth
function menuC() {
TITLE="SET COLOR DEPTH"
TEXT="\n
Select the color depth you want. High colors\n
is good for your eye, but might not be supported\n
by your video card and generally slower.\n
Most users will be happy with 16 bit colors.\n
If you have a good computer and video card,\n
24 bit true colors is recomended.\n"
DIMENSION="18 60 4"

$WCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
"8" "8 bit 256 pseudo colors" \
"16" "16 bit 65535 pseudo colors" \
"24" "24 bit True Color" \
"32" "32 bit True Color" \
2> $freply

status=$?
[ $status != 0 ] && return $status

XDEPTH=$(cat $freply)
dbug "XDEPT=$XDEPTH"
return 0
}


##################################################
# Ask Mouse
function menuD() {
WHEEL=""
DIMENSION="12 60 3"
TITLE="X-WINDOW MOUSE"
TEXT="\n
Please select your mouse type:"

$WCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
"2"     "Mouse with 2 buttons" \
"3"     "Mouse with 3 buttons" \
"WHEEL" "Mouse with 2 buttons and a whell" 2> $freply

status=$?
[ $status != 0 ] && return $status

XMOUSE=$(cat $freply)
dbug "XMOUSE=$XMOUSE"
return 0 
}

###############################################################
# Save settings
menuE()
{
   infobox "Setting up X Window system"
   set_config
   clean_exit 0
}

###############################################################
# MAIN 

initialize
wizard menuA menuB menuC menuD menuE

