#! /usr/bin/gtkdialog -e
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Gtkdialog script to configure Keyboard layout
# Author: Grobsch(Flavio de Oliveira) <http://www.goblinx.com.br>

# path to config file for X
XConfig=/etc/X11/xorg.conf
XTemp=/tmp/xorg.conf.new

command_line() {
  command="setxkbmap"

  [ "$MP02" != "" ] && command="$command -layout $MP02"
  [ "$MP03" != "" ] && command="$command -model $MP03"
    
  [ "$FILE" ] && command="$command"
  echo $command | commandparser 
}

command_exec() {
rm $HOME/command.sh

echo "#!/bin/sh" > $HOME/command.sh
command_line >> $HOME/command.sh
echo "" >> $HOME/command.sh
chmod 755 $HOME/command.sh
cd $HOME
./command.sh
}

last_exec(){
if [ -e $HOME/command.sh ]; then
cd $HOME
./command.sh
else
zenity --title "gtkKeyboard Layout" --info "Could not find an old keyboard layout modification command" 0 0
fi
}

_list(){
ls -1b /etc/X11/xkb/symbols | while read LINE; do if [ "`echo $LINE | cut -b3`" = "" ]; then cat /etc/X11/xkb/symbols/$LINE | grep "name\[Group1\]" | head -n 1 | cut -f2 -d "\"" | sed 's/[,| ]/_/g'; fi; done 
}

_entry(){
echo "$D" >/tmp/_key
ls -1b /etc/X11/xkb/symbols | while read LINE; do if [ "`echo $LINE | cut -b3`" = "" ]; then echo $LINE; cat /etc/X11/xkb/symbols/$LINE | grep "name\[Group1\]" | head -n 1 | cut -f2 -d "\"" | sed 's/[,| ]/_/g'; fi; done >/tmp/_keylayouts
NUM="`cat /tmp/_keylayouts | nl -nln | grep $D | cut -f 1 -d " "`" 
NUMI="`echo $NUM - 1 | bc`"
cat /tmp/_keylayouts | nl -nln | egrep "^$NUMI " | cut -f2 >/tmp/_layout
}

_key(){
cat /tmp/_key
}

_lay(){
cat /tmp/_layout
}

_symbols(){
LAY="`cat /tmp/_layout`"
cat /etc/X11/xkb/symbols/$LAY | grep xkb_symbols | cut -f2 -d "\"" | uniq -i 
}

_notify(){
if [ "$MP02" != "" ]; then 
DISPLAY=:0.0 
dbus-launch notify-send -i gtk-dialog-warning -t 2000 'Keyboard Layout' "Your keyboard changed to use $MP01's layout..."
fi
}

xkbl(){
cat $XConfig | grep "Option \"XkbLayout\"" | cut -f 4 -d "\""
}

xkbm(){
cat $XConfig | grep "Option \"XkbModel\"" | cut -f 4 -d "\""
}

xorg_exec(){
zenity --question --text "Do you wish to configure Xorg to recognize this keyboard layout everytime?"
if [ $? -eq 0 ]
then
XkbModel="$(cat $XConfig | grep "Option \"XkbModel\"" | cut -f 4 -d "\"")"
XkbLayout="$(cat $XConfig | grep "Option \"XkbLayout\"" | cut -f 4 -d "\"")"

if [ "$XkbModel" = "" ]; then
sed '10,$s/Keyboard0/Keyboard1/' $XConfig > $XTemp

cat <<EOF>> $XTemp
Section "InputDevice"
        Identifier  "Keyboard0"
        Driver      "kbd"
	Option "AutoRepeat" "500 30"
    	Option "XkbRules"   "xorg"
    	Option "XkbModel"   "$MP03"
    	Option "XkbLayout"  "$MP02"

EndSection
EOF
cp $XTemp $XConfig

else
cp $XConfig $XTemp
if [ "$MP02" != "" ]; then sed -r "s|$XkbLayout|$MP02|" $XConfig > $XTemp; cp $XTemp $XConfig; fi
if [ "$MP03" != "" ]; then sed -r "s|$XkbModel|$MP03|" $XConfig > $XTemp; cp $XTemp $XConfig; fi
fi

else
continue
fi
}

clean(){
for i in _layout _key _keylayouts ; do
if [ -e /tmp/$i ]; then rm -rf /tmp/$i; fi
done
}

export MAIN_DIALOG='
<window title="gtkKeyboard Layout" icon-name="gtk-execute">
<vbox>
<text>
<label>"The gtkKeyout - Keyboard Map Configurator"</label>
</text> 
<hbox>
<frame>
<hbox>
<table>
<label>Keyboard Layouts (Layout Map)</label>
<input>_list</input>
<input>echo $CURRENT_COMMAND | commandparser -f a</input>
<action>_entry</action>
<action type="refresh">MP01</action>
<action type="refresh">MP02</action>
<action type="clear">MP03</action>
<action type="refresh">MP03</action>
<action type="refresh">COMMAND</action>
<variable>D</variable>
<height>120</height><width>100</width>
</table>
</hbox>
<text>
<label>""</label>
</text>
<hbox>
<text>
<label>"Name[Group1]	:"</label>
</text>
<entry>
<input>_key</input>
<variable>MP01</variable>
</entry>
</hbox> 
<hbox>
<text>
<label>"Layout Type	:"</label>
</text>
<entry>
<input>_lay</input>
<variable>MP02</variable>
</entry>
</hbox> 
<text>
<label>"---"</label>
</text>
<text>
<label>"select extra xkb symbols"</label>
</text>
<table>
<label>Extra Xkb Symbols (Model)</label>
<input>_symbols</input>
<variable>MP03</variable>
<input>echo $CURRENT_COMMAND | commandparser -f a</input>
<action type="refresh">COMMAND</action>
<height>100</height><width>100</width>
</table>
</frame>
<text>
<label>" "</label>
</text>
<text>
<label>" "</label>
</text>
<frame>
<button>
<visible>disabled</visible>
<input file>/usr/share/images/gtkkeyout01.png</input>
</button>

<text>
<label>"- Setxkbmap Command -"</label>
<visible>disabled</visible>
</text>
<entry width_chars="15">
<variable>COMMAND</variable>
<input>command_line</input>
</entry>

<text>
<label>" "</label>
</text>

<text>
<visible>disabled</visible>
<label>"- Xorg Config Entries -"</label>
</text>
<hbox>
<text>
<label>Current XkbModel (Xorg):</label>
<visible>disabled</visible>
</text>
<entry width_chars="15">
<input>xkbm</input>
<variable>MP04</variable>
</entry>
</hbox>
<hbox>
<text>
<label>Current XkbLayout (Xorg):</label>
<visible>disabled</visible>
</text>
<entry width_chars="15">
<input>xkbl</input>
<variable>MP05</variable>
</entry>
</hbox>
<text>
<label>" "</label>
</text>

<text>
<label>"- Current Layout Test Area -"</label>
<visible>disabled</visible>
</text>

<hbox>
<text>
<label>Use this entry area to test.</label>
<visible>disabled</visible>
</text>
<entry>
</entry>
</hbox>
</frame> 
<text>
<label>""</label>
</text>
</hbox>
<text>
<label>""</label>
</text> 
<hbox>
<button>
<input file stock="gtk-refresh"></input>
<action type="refresh">COMMAND</action>
</button>
<button>
<input file stock="gtk-execute"></input>
<label>"Execute"</label>
<action>command_exec</action>
<action>xorg_exec</action>
</button>
<button>
<input file stock="gtk-quit"></input>
<label>"Exit"</label>
<action>clean</action>
<action type="exit">OK</action>
</button>
<text>
<label>"    "</label>
</text>
</hbox>
<text>
<label>""</label>
</text>
</vbox>
</window>
'