#!/bin/bash
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL
# Description: From X version 7.3, "dpkg-reconfigure xserver-xorg" won't ask about VGA driver and screeen resolution, therefore we have to drop the "dpkg-reconfigure xserver-xorg" mechanism.
# Ref: http://forums.debian.net/viewtopic.php?t=26577

if [ ! -e /etc/X11/X ]; then
  echo "No X program! Skip X configure!"
  exit 0
fi

xconf="/etc/X11/xorg.conf"

# Load functions
. /usr/bin/gl-functions

# functions
#
choose_resolution() {
  echo '*****************************************************'
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "This program will create $xconf with screen freq 60 Hz."
  echo "Which resolution do you want ?"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "0: 1024x768"
  echo "1: 800x600"
  echo "2: 640x480"
  echo -n "[0] "
  read ans_res
  case "$ans_res" in
    1) gtf_param="800 600 60" 
       pmode="800x600_60.00"
       ;;
    2) gtf_param="640 480 60"    
       pmode="640x480_60.00"
       ;;
    *) gtf_param="1024 768 60"   
       pmode="1024x768_60.00"
       ;;
  esac
  echo '*****************************************************'
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "What driver for your VGA card ? Ex. vesa, i810, nv, ati..."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[vesa] "
  read ans_vga
  case "$ans_vga" in
    "") vga_driver="vesa" ;;
    *) vga_driver="$ans_vga"  ;;
  esac
  echo '*****************************************************'
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "What color depth do you want ?"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "0: 24"
  echo "1: 16"
  echo "2: 15"
  echo "3: 8"
  echo -n "[0] "
  read ans_dep
  case "$ans_dep" in
    1) depth="16" ;;
    2) depth="15" ;;
    3) depth="8"  ;;
    *) depth="24" ;;
  esac
} # end of choose_resolution
#
gen_xorg.conf() {
cat <<-XCONF_END > $xconf
# xorg.conf (X.Org X Window System server configuration file)
#
# This file was generated by GParted live.
#
# Edit this file with caution, and see the xorg.conf manual page.

Section "InputDevice"
        # ///NOTE/// The keyboard layout will be overwritten when enter X by loadkey command. Therefore here this section is useless. Just for the X to start normally.
        Identifier	"Generic Keyboard"
        Driver		"kbd"
        Option		"XkbRules"	"xorg"
        Option		"XkbModel"	"pc104"
        Option		"XkbLayout"	"us"
EndSection

Section "InputDevice"
        Identifier	"Configured Mouse"
        Driver		"mouse"
EndSection

Section "Device"
        Identifier	"Configured Video Device"
XCONF_END

cat <<-XCONF_END >> $xconf
        Driver		"$vga_driver"
EndSection

XCONF_END

cat <<-XCONF_END >> $xconf
Section "Monitor"
        Identifier	"Configured Monitor"
XCONF_END

gtf $gtf_param  >> $xconf

cat <<-XCONF_END >> $xconf
        Option      "PreferredMode"      "$pmode"
XCONF_END

cat <<-XCONF_END >> $xconf
EndSection

Section "Screen"
        Identifier	"Default Screen"
        Monitor		"Configured Monitor"
        DefaultDepth   $depth 
        SubSection "Display"
                    Depth   $depth
                    Modes   "$pmode"
        EndSubSection
EndSection

XCONF_END
} #
#
reconfig_Xorg() {
  local RC
  choose_resolution
  gen_xorg.conf
  echo "$xconf was created."
  return 0
} # end of reconfig_Xorg
#
# Clean the old config if exists before reconfigure
rm -f $xconf
reconfig_Xorg
