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

# Default settings
xconf="/etc/X11/xorg.conf"
start_x="yes"

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

# functions
USAGE() {
    echo "Usage:"
    echo "To configure X config"
    echo "$0 [OPTION]"
    echo "OPTION:"
    echo "-s, --skip-start-x     After xorg.conf is created, skip asking if want to start X"
} # end of USAGE
#
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"
       ;;
    2) gtf_param="640 480 60"    
       pmode="640x480"
       ;;
    *) gtf_param="1024 768 60"   
       pmode="1024x768"
       ;;
  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
#
# This following was generated by GParted live.
#
# Edit this file with caution, and see the xorg.conf manual page.

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
} # end of gen_xorg.conf
#
reconfig_Xorg() {
  local RC
  choose_resolution
  gen_xorg.conf
  echo "$xconf was created."
  return 0
} # end of reconfig_Xorg

############
### MAIN ###
############
while [ $# -gt 0 ]; do
  case "$1" in
    -s|--skip-start-x)
	    start_x="no"
            shift ;;
    -*)     echo "${0}: ${1}: invalid option" >&2
            USAGE >& 2
            exit 2 ;;
    *)      break ;;
  esac
done

#
reconfig_Xorg

#
if [ "$start_x" = "yes" ]; then
  echo '*****************************************************'
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "Do you want to run \"sudo startx\" to enter graphical environment ?"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[Y/n] "
  read enter_X
  case "$enter_X" in
    n|N|[nN][oO])
       exit 1
       ;;
    *)
       sudo startx
       ;;
  esac
fi
