#!/bin/bash

# This program 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 3 of the License, or
# (at your option) any later version.

# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>

# (c) Antonio Sánchez León <introlinux[fix@]gmail[fix.]com>
# (c) Miguel Anxo Bouzada <mbouzada[fix@]gmail[fix.]com>
# (c) GALPon MinNo Team <minino[fix@]galpon[fix.]org>
# july 2012

## -

case $LANG in
  ca*)
    ERR="ERROR"
    NOROOT="El Canviador d'idioma s'ha d'executar amb privilegis de <b>root</b>! \n\nSigui prudent!"
    TIT="Canviar l'idioma del sistema"
    TEXT1="<b> NOTA:</b>\n Aquest ajust d'idioma tindrà\n efecte en el proper inici. \n\n<b>Seleccioneu l'idioma.</b>"
    TEXT2a=" Perquè el canvi sigui efectiu s'ha de reiniciar la sessió. \n\n <b>Voleu reiniciar ara?</b>"
    TEXT2b=" Perquè el canvi sigui efectiu s'ha de reiniciar el sistema. \n\n <b>Voleu reiniciar ara?</b>"
    TEXT3=" Canviada la configuració d'idioma\n predeterminat del sistema. \n\n Recordeu, aquest canvi tindrà efecte \n en el proper inici."
    TEXT4=" <b>Voleu canviar el mapa de teclat?</b> \n 
 <i><b>Recordeu</b>: hi ha idiomes diferents que \n\tcomparteixen el mateix mapa de 
\tteclat, p.ex. el gallec (gl) i el \n\tcastellà (es) fan servir el mapa «<b>es</b>»</i> \n"
    KBD="Teclat"
    KBDTEXT="Canviar només el teclat"
    LAY="Mapa"
    CODE="Codi ISO"
    DES="Idioma"
    CAS="Castellà"
    GAL="Gallec"
    CAT="Català"
    EUK="Euskera"
    PPT="Portuguès (Portugal)"
    PBR="Portuguès (Brasil)"
    FFR="Francès (França)"
    FCA="Francès (Canadà)"
    EGB="Anglés (Anglaterra)"
    EUS="Anglés (EUA)"
    LATAM="Castellà (Amèrica Llatina)"
    SPAIN="Castellà"
    BUT0="_Canviar"
    BUT1="_No canviar"
  ;;
  es*)
    ERR="ERROR"
    NOROOT="El Cambiador de idioma debe ejecutarse con privilegios de <b>root</b>! \n\nSea prudente!"
    TIT="Cambiar el idioma del sistema"
    TEXT1="<b> NOTA:</b>\n Este ajuste de idioma tendrá\n efecto en el próximo inicio. \n\n<b>Seleccione el idioma.</b>"
    TEXT2a=" Para que el cambio sea efectivo debe reiniciar la sesión. \n\n <b>¿Desea reiniciar ahora?</b>"
    TEXT2b=" Para que el cambio sea efectivo debe reiniciar el sistema. \n\n <b>¿Desea reiniciar ahora?</b>"
    TEXT3=" Cambiada la configuración de idioma\n predeterminado del sistema. \n\n Recuerde, este cambio tendrá efecto \n en el próximo inicio."
    TEXT4=" <b>¿Desea cambiar el mapa de teclado?</b> \n 
 <i><b>Recuerde</b>: hay idiomas distintos que \n\tcomparten el mismo mapa de 
\tteclado, p.ej. el gallego (gl) y el \n\tcastellano (es) usan el mapa «<b>es</b>»</i> \n"
    KBD="Teclado"
    KBDTEXT="Cambiar solo el teclado"
    LAY="Mapa"
    CODE="Código ISO"
    DES="Idioma"
    CAS="Castellano"
    GAL="Gallego"
    CAT="Catalán"
    EUK="Euskera"
    PPT="Portugués (Portugal)"
    PBR="Portugués (Brasil)"
    FFR="Francés (Francia)"
    FCA="Francés (Canada)"
    EGB="Inglés (Inglaterra)"
    EUS="Inglés (EE.UU.)"
    LATAM="Español (America Latina)"
    SPAIN="Español"
    BUT0="_Cambiar"
    BUT1="_No cambiar"
  ;;
  gl*)
    ERR="ERRO"
    NOROOT="O Cambiador de idioma debe executarse con privilexios de <b>root</b>! \n\nSexa prudente!"
    TIT="Cambiar o idioma do sistema"
    TEXT1="<b> NOTA:</b>\n Este axuste de idioma terá efecto\n no próximo inicio. \n\n<b>Seleccione o idioma.</b>"
    TEXT2a=" Para que o cambio sexa efectivo debe reiniciar a sesión. \n\n <b>Desexa reiniciar agora?</b>"
    TEXT2b=" Para que o cambio sexa efectivo debe reiniciar o sistema. \n\n <b>Desexa reiniciar agora?</b>"
    TEXT3="Cambiada a configuración de idioma\n predeterminado do sistema. \n\n Lembre, este cambio terá efecto \n no próximo inicio."
    TEXT4=" <b>Desexa cambiar o mapa do teclado?</b> \n 
 <i><b>Lembre</b>: hai idiomas distintos que \n\tcomparten o mesmo mapa de 
\tteclado, p.ex. o galego (gl) e o \n\tcastelán (es) usan o mapa «<b>es</b>»</i> \n"
    KBD="Teclado"
    KBDTEXT="Cambiar só o teclado"
    LAY="Mapa"    
    CODE="Código ISO"
    COUN="País"
    DES="Idioma"
    CAS="Castelán"
    GAL="Galego"
    CAT="Catalán"
    EUK="Euscara"
    PPT="Portugués (Portugal)"
    PBR="Portugués (Brasil)"
    FFR="Francés (Francia)"
    FCA="Francés (Canada)"
    EGB="Inglés (Inglaterra)"
    EUS="Inglés (EE.UU.)"
    LATAM="Español (America Latina)"
    SPAIN="Español"
    BUT0="_Cambiar"
    BUT1="_Non cambiar"
  ;;
  *)
    ERR="ERROR"
    NOROOT="Locale Changer requires <b>root</b> priviledges! \n\nBe careful!"
    TIT="Change the system language"
    TEXT1="<b> NOTE:</b>\n This language adjustment will be\n effect the next rebooting. \n\n<b>Select the language.</b>"
    TEXT2a="You must restart the session in order to take effect the changes. \n\n <b>Do you want to reboot now?</b>"
    TEXT2b="You must restart the system in order to take effect the changes. \n\n <b>Do you want to reboot now?</b>"
    TEXT3=" Default language in the system has been changed. \n\n Remember, this changes will take effect \n the next rebooting. "
    TEXT4=" <b>Would you like to change keyboard layout?</b> \n 
 <i><b>Remember</b>: some keymaps are shared \n\teg. Galician (gl) and Castilian (es)  \n\tuse «<b>es</b>» map both</i> \n"
    KBD="Keyboard"
    KBDTEXT="Change only keyboard layout"
    LAY="Map"
    CODE="ISO Code"
    DES="Language"
    CAS="Castilian"
    GAL="Galician"
    CAT="Catalan"
    EUK="Euskara"
    PPT="Portuguese (Portugal)"
    PBR="Portuguese (Brazil)"
    FFR="French (France)"
    FCA="French (Canada)"
    EGB="English (UK)"
    EUS="English (US)"
    LATAM="Spanish (Latin American)"
    SPAIN="Spanish"
    BUT0="_Change"
    BUT1="_Not change"
  ;;
esac

# Se ejecuta si NO se accede como root
if [ $(whoami) != "root" ]; then
    yad --title "$ERR" --image important --button gtk-ok:0 --text "$NOROOT"
    exit
fi


function localeEND(){
	yad --window-icon="config-language" --image="config-language" \
	    --button gtk-no:1 --button gtk-ok:0 --title "$TIT" --text "$TEXT2a"
	if [ $? = 0 ]; then
          pkill -SIGTERM lxsession
        else
	  yad --window-icon="config-language"  --image="config-language" \
	      --button gtk-ok:0 --title "$TIT" --text="$TEXT3"
        fi
}

function keyboardEND(){
	yad --window-icon="config-language" --image="config-language" \
	    --button gtk-no:1 --button gtk-ok:0 --title "$TIT" --text "$TEXT2b"
	if [ $? = 0 ]; then
          sudo shutdown -r now
        else
	  yad --window-icon="config-language" --image="config-language" \
	      --button gtk-ok:0 --title "$TIT" --text="$TEXT3"
        fi
}

# Lista completa de mapas/layouts de teclado y detalles:
# /usr/share/doc/keyboard-configuration/xorg.list
# para configuraciones particulares consulte: man keyboard
function selectedKBD(){
	selectedKBD=$(yad --window-icon="config-language" --list --title="$TIT" --text="$TEXT4" \
		--width="325" --height="365" --image="config-language" \
		--button "$BUT1":1 --button "$BUT0":0 \
		--column="$LAY" --column="$COUN" \
		"br" "$PBR" \
		"es" "$SPAIN" \
		"gb" "$EGB" \
		"latam" "$LATAM" \
		"pt" "$PPT" \
		"us" "$EUS")
	ret2=$?

	echo $selectedKBD > /tmp/selectedKBD
	selectedKBD=`cut -f1 -d"|" /tmp/selectedKBD`
	rm /tmp/selectedKBD

	[ $ret2 = 0 ] && \
		sed -i "s+XKBLAYOUT=$CurrentKBD+XKBLAYOUT=\"$selectedKBD\"+" /etc/default/keyboard && \
		keyboardEND
	[ $ret2 != 0 ] && [ "$IDIOMA" == "$KBD.UTF-8" ] && exit
	[ $ret2 != 0 ] && [ "$IDIOMA" != "$KBD.UTF-8" ] && localeEND
}

IDIOMA=$(yad --window-icon="config-language" --list --title="$TIT" --text="$TEXT1" \
	     --width="325" --height="285" --image="config-language" \
	     --column="$CODE" --column="$DES" \
	     "ca_ES" "$CAT" \
	     "es_ES" "$CAS" \
	     "gl_ES" "$GAL" \
	     "en_US" "$EUS" \
	     "pt_BR" "$PBR" \
	     "" "" \
	     "$KBD" "$KBDTEXT"
	     ) 
ret1=$?



echo $IDIOMA > /tmp/idioma

IDIOMA=`cut -f1 -d"|" /tmp/idioma`.UTF-8
CurrentKBD=`cat /etc/default/keyboard | grep XKBLAYOUT | cut -f2 -d=`

rm /tmp/idioma

case $ret1 in
  0)
    if [ "$IDIOMA" != "" ]; then
      case $? in
        0)
	  if [ "$IDIOMA" == "$KBD.UTF-8" ]; then
	    selectedKBD
	  else
	    echo "#  File generated by ChangeLocale of MiniNo" > /etc/default/locale
            echo "LANG=\"$IDIOMA\"" >> /etc/default/locale
	    selectedKBD
          fi
          ;;
        1) exit ;;
      esac
    else exit
    fi
    ;;
  1) exit ;;
esac


