#!/bin/bash
#GNU script by The GALPon MiniNo Team

case $LANG in
  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>"
    TEXT2=" Para que el cambio de idioma sea efectivo debe reiniciar la sesión.\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"
  ;;
  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>"
    TEXT2=" Para que o cambio de idioma sexa efectivo debe reiniciar a sesión.\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"
  ;;
  *)
    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<bSelect the language.</b>"
    TEXT2="You must restart the session 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>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="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"
  ;;
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

# 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 --list --title="$TIT" --text="$TEXT4" \
		--width="325" --height="365" --image="config-language" \
		--button "_No cambiar":1 --button "_Cambiar":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
	[ $ret2 != 0 ] && [ "$IDIOMA" == "$KBD.UTF-8" ] && exit
}

IDIOMA=$(yad --list --title="$TIT" --text="$TEXT1" \
	     --width="325" --height="285" --image="config-language" \
	     --column="$CODE" --column="$DES" \
	     "es_ES" "$CAS" \
	     "gl_ES" "$GAL" \
	     "en_US" "$EUS" \
	     "" "" \
	     "$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
            yad --image="config-language" --button gtk-no:1 --button gtk-ok:0 --title "$TIT" --text "$TEXT2"
	    if [ $? = 0 ]; then
              sudo shutdown -r now
            else
	      yad  --image="config-language" --button gtk-ok:0 --text="$TEXT3"
            fi
	  fi
          ;;
        1) exit ;;
      esac
    else exit
    fi
    ;;
  1) exit ;;
esac


