#! /bin/bash
#**************************************************************************
#* confirm-X-update - Confirm the new X configuration.
#* Version:     0.0.2 28/08/2005
#* File:        confirm-X-update.
#* Description: Confirm the new X configuration.
#* Platform(s):	Gnu/Linux, All
#* Author(s):	Della Bianca Giuseppe <bepi@adria.it><bepii@libero.it>
#* This file is part of update-X-config.
#*
#* update-X-config is free software; you can redistribute it and/or modify
#* it under the terms of the GNU General Public License as published by
#* the Free Software Foundation; either version 2 of the License, or
#* (at your option) any later version.
#*
#* update-X-config is distributed in the hope that it will be useful,
#* but WITHOUT ANY WARRANTY; without even the implied warranty of
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#* GNU General Public License for more details.
#*
#* You should have received a copy of the GNU General Public License
#* along with update-X-config; if not, write to the Free Software
#* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#**************************************************************************

BaseDir=`dirname $0`

FileXConf="$BaseDir/xorg.conf"

function question(){
    echo -e "Confirm the new X server (graphics server) configuration?" \
        "\n \t c = Confirm overwrite actual ' xorg.conf ' with new file." \
        "\n other key = Leave unchanged actual ' xorg.conf '."
    read -n 1 -s action
# execute owerwrite of X configuration
    if [ "$action" == "c" ]; then
	cp $FileXConf.new /etc/X11/xorg.conf
	echo -e "\nExecuted overwrite of ' xorg.conf '." \
                "\nFile ' $FileXConf.act ' is the copy of original X config file." \
	        "\nWARNING, you lost this copy if re-run ' update-X-config ' command."
    fi
    echo -e "\nNow you can restart normally your X server (graphics server) session."
    sleep 1d
}

if ! [ "$1" ]; then
# run startx command with new X configuration
    if [ "$DISPLAY" ]; then
	echo "You must close X server."
	exit 1
    fi
    if [ $UID != "0" ]; then
	echo "You must be logged as root."
	exit 1
    fi
    startx "$BaseDir/confirm-X-update" "run-client" -- -config "$BaseDir/xorg.conf.new"
elif [ "$1" == "run-question" ]; then
# run question and owerwrite of X configuration
    question
elif [ "$1" == "run-client" ]; then
# run xterm with question and X client
    xterm -e "$BaseDir/confirm-X-update" "run-question" &

    xinit=~/.xinitrc
    if ! [ -f "$xinit" ]; then
	xinit="/etc/X11/xinit/xinitrc"
    fi

    . $xinit
else
    echo -e "Invalid option, dont use this program without know you make."
fi
