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

# Check if dpkg-reconfigure exists
if ! type dpkg-reconfigure &>/dev/null; then
  echo "No dpkg-reconfigure! This script only works for Debian based system!"
  exit 1
fi

# Load functions
. /opt/drbl/sbin/drbl-conf-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 /etc/init.d/Forcevideo-drbl-live\" to configure it again. Use '1: Medium mode' or '2: Expert mode', then say 'no' to 'Attempt to autodetect video buffer' so that you can choose 'vesa' as the X server driver. When 'Attempt monitor autodetection', say 'no' either so that you can choose 1024x768, 800x600 or 640x480 as your resolution. For the rest of settings, you can accept the default values if you have no idea about them."
  echo "  (b): Run \"sudo /etc/init.d/gdm restart\" 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
