#! /bin/bash
#**************************************************************************
#* update-X-configfunc - Function for set parameters of X config.
#* Version:     0.0.3 02/09/2005
#* File:        update-X-configfunc.
#* Description: Function for set parameters of X config.
#* Platform(s):	GNU/Linux, All
#* Author(s):	Della Bianca Giuseppe <bepi@adria.it><bepii@libero.it>
#*
#* Copyright (C) 2005 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
#**************************************************************************

function CheckExeOpt(){
    local FileList

    if ! [ "$1" ]; then
	UpdGo="ko"
	echo -e "Usage: \n update-X-configfunc xxx1.conf xxx2.conf ..." \
	          "\n\nValid xxx.conf file: "
	echo -n " "
	FileList=`cd "$BaseDir"; ls *.conf | grep -v test.conf`
	echo $FileList
	echo -e "\nAfter program finish correctly, ' $FileXConf.act ' is the copy of actual" \
                "X configuration file, ' $FileXConf.new ' is the new file."
	echo -e "\nFor first check and next overwrite the actual X configuration file with new file:" \
	        "\n - exit from X server (graphics server)." \
	        "\n - execute login as ' root ' (' su - ' command)." \
	        "\n - run confirm-X-update command (is part of update-X-config)."
    else
	FilesConf="$*"
    fi
}

# read list of update for X config
function ListUpdConf(){
    local FileConf=$1

# set the section identifier
    SerGrp="ServerLayout"
    SerId=`$GcX -l $SerGrp: Identifier`

    ScrId=`$GcX -l "$SerGrp:$SerId" Screen`
    ScrId=${ScrId/#* \"/}
    ScrId=${ScrId/%\" */}
    VideoId=`$GcX -g "Screen:$ScrId" Device`

    CorePointId=`$GcX -l "$SerGrp:$SerId" InputDevice | grep -i 'Mouse\|CorePointer'`
    CorePointId=${CorePointId/#\"/}
    CorePointId=${CorePointId/%\" */}
    CoreKeybId=`$GcX -l "$SerGrp:$SerId" InputDevice | grep -i 'Keyboard\|CoreKeyboard'`
    CoreKeybId=${CoreKeybId/#\"/}
    CoreKeybId=${CoreKeybId/%\" */}

    o_grp=""
    IFS=$GFS
# read the news parameters for update of X config
    while read c_stat c_grp c_key c_val; do
	SetUpdConf $c_stat $c_grp $c_key $c_val
    done < <(IFS=$OLDIFS; $GcKey -fm $FileConf -l "" "" K)
    IFS=$OLDIFS
}

# set update of X config
function SetUpdConf(){
    local stat=$1 grp=$2 key=$3 val=$4

# set the action
    ActType="und"
    a_grp=${grp/#DELETE:/}
    g_grp=""
    k_grp=""
    v_grp=""
    o_val=""
    if [ "$a_grp" != "$grp" ]; then
	ActType="del"
    else
	a_grp=${grp/#UPDATE:/}
	if [ "$a_grp" != "$grp" ]; then
	    ActType="upd"
	fi
    fi
    x_grp=$a_grp

# set the group/key/value pointer at actual group/key
    SetGrpKey

# manage the load/option key
    if [ "$key" == Load ] || [ "$key" == Option ] || [ "$key" == CorePointer ]; then
        if [ "$key" == CorePointer ]; then
	    x_val=$CorePointId
	    key=InputDevice
	else
	    x_val=${val/%\" */}
	fi
	o_val=`IFS=$OLDIFS; $GcX -l "$x_grp" "$key" K | grep -i "$x_val"`
	if ! [ "$o_val" ]; then
	    o_val=$val
	fi
    fi
# manage the identifier key
    if [ $ActType != del ] && [ "$k_grp" ] && [ "$x_grp" != "$o_grp" ] ; then
	f_grp=`IFS=$OLDIFS; $GcX -l "$x_grp" "" G`
	if ! [ "$f_grp" ]; then
	    echo $ActType" -> "$g_grp $k_grp $v_grp
	    echo "$g_grp$GFS$k_grp$GFS$v_grp" >> "$BaseDir/update-X-config.$ActType.tmp"
	fi
	o_grp=$x_grp
    fi
    
    if [ "$o_val" ]; then
	echo $ActType" -> "$x_grp $key $o_val $val
	echo "$x_grp$GFS$key$GFS$o_val$GFS$val">> "$BaseDir/update-X-config.$ActType.tmp"
    else
	echo $ActType" -> "$x_grp $key $val
	echo "$x_grp$GFS$key$GFS$val">> "$BaseDir/update-X-config.$ActType.tmp"
    fi
}

# set the group/key/value pointer at actual group/key
function SetGrpKey(){
# first level
    t_grp=""
    if t_grp=${a_grp/#CoreKeyboard/} && [ "$a_grp" != "$t_grp" ]; then
	g_grp=InputDevice
	k_grp="Identifier"
	v_grp=$CoreKeybId
    elif t_grp=${a_grp/#CorePointer/} && [ "$a_grp" != "$t_grp" ]; then
	g_grp=InputDevice
	k_grp="Identifier"
	v_grp=$CorePointId
    elif t_grp=${a_grp/#Video/} && [ "$a_grp" != "$t_grp" ]; then
	g_grp=Device
	k_grp="Identifier"
	v_grp=$VideoId
    elif t_grp=${a_grp/#Screen/} && [ "$a_grp" != "$t_grp" ]; then
	g_grp=Screen
	k_grp="Identifier"
	v_grp=$ScrId
    elif t_grp=${a_grp/#Server/} && [ "$a_grp" != "$t_grp" ]; then
	g_grp=$SerGrp
	k_grp="Identifier"
	v_grp=$SerId
    fi
    a_grp=$t_grp

# second level
# manage the Display section of Screen
    if t_grp=${a_grp/#:Display:/} && [ "$a_grp" != "$t_grp" ]; then
	g_grp=$g_grp:$v_grp:Display
	k_grp="Depth"
	v_grp=$t_grp
    elif t_grp=${a_grp/#:/} && [ "$a_grp" != "$t_grp" ]; then
	v_grp=$t_grp
	a_grp=$t_grp 
    fi
    a_grp=$t_grp

# set the group/key translated
    if [ "$g_grp" ]; then
	x_grp=$g_grp:$v_grp
    fi
}
