#!/bin/bash
# License: GPL
# Modified by Steven Shiau <steven _at_ nchc org tw>
# 
if [ ! -e /etc/X11/X ]; then
  echo "No X program! Skip X configure!"
  exit 0
fi

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

# functions
set_priority() {
  echo '*****************************************************'
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "Which mode do you want when configuring X ?"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "0: Beginner mode, use whatever the default priority of question is."
  echo "1: Medium mode."
  echo "2: Expert mode, very verbose."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "///NOTE/// If graphical environment (X-window) fails to start, you can:"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "  (a): Run \"sudo Forcevideo\" to configure it again. VESA driver should always work, and 1024x768 resolution, too."
  echo "  (b): Run \"sudo startx\" to enter graphical environment."
  echo -n "[0] "
  read ans_ver
  case "$ans_ver" in
    # Beginner means only ask high priority question
    # Expert means ask low priority question
    1) PRIORITY="medium" ;;
    2) PRIORITY="low"    ;;
    *) PRIORITY="high"   ;;
  esac
} # end of set_prority
#
reconfig_Xorg() {
  local RC
  set_priority
  echo -n "Running \"dpkg-reconfigure --priority=$PRIORITY xserver-xorg\"... "
  LC_ALL=C dpkg-reconfigure --priority=$PRIORITY xserver-xorg
  RC=$?
  if [ "$RC" -eq 0 ]; then
    echo "X is now configured."
    exit 0
  else
    echo "Failed to configure X config. Something went wrong..."
    exit 1
  fi
} # end of reconfig_Xorg
#
# Clean the old config if exists before reconfigure
rm -f /etc/X11/xorg.conf
reconfig_Xorg
