#!/bin/bash

# Copyright Jean-Philippe Guillemin <jp.guillemin@free.fr>. 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 2 of the License,
# or (at your option) any later version. Please take a look at http://www.gnu.org/copyleft/gpl.htm

# This script :
# - probes for available locales
# - asks the user for his choice
# - configures /etc/profile.d/lang.sh

# Version 1.0 - 01/09/2005
# Version 1.1 - 01/09/2005
#  * Added Xdialog support
#  * added icon support
# Version 1.2 - 22/10/2005
#  * Improved/secured tempdir management
# Version 1.3 - 06/04/2006
#  * Delete temp file
# Ported to yad (X-only, no gettext) by Claude.

if ! command -v yad >/dev/null 2>&1; then
  echo "yad is required but was not found in PATH." >&2
  exit 1
fi

if [ ! "$DISPLAY" ]; then
  echo "This program requires a running X session." >&2
  exit 1
fi

# Path needs to be extended in case you're only half a root :)
export PATH="${PATH}:/usr/sbin:/sbin"

function setLocale(){
  file="$1"
  if [[ ! "$(grep "export LANG=" $file)" ]] ; then
    echo "export LANG=$locale" >> $file
  else
    sed -i "s/^[[:space:]]*export *LANG=.*/export LANG=$locale/g" $file
  fi
  if [[ ! "$(grep "export LANGUAGE=" $file)" ]] ; then
    echo "export LANGUAGE=$locale" >> $file
  else
    sed -i "s/^[[:space:]]*export *LANGUAGE=.*/export LANGUAGE=$locale/g" $file
  fi
  if [[ ! "$(grep "export LC_MESSAGES=" $file)" ]] ; then
    echo "export LC_MESSAGES=$locale" >> $file
  else
    sed -i "s/^[[:space:]]*export *LC_MESSAGES=.*/export LC_MESSAGES=$locale/g" $file
  fi
}


# probe for available locales and generate a list

currentlocale="$LANG"

(
echo "10" ; echo "# Probing for locales..."
locale -cva > /tmp/.localeconfig.$$ 2>/dev/null
echo "100" ; echo "# Done."
) | yad --center --title "Probing for locales" --text "Please wait..." \
  --progress --auto-close --width=380

# Génération de la liste directement transmise à YAD via un pipe
locale=$( (
    code="" ; title=""
    while IFS= read -r line; do
        case "$line" in
            locale:*)
                case "$code" in
                    nb_NO|nb_NO.*|"") ;;
                    *) printf "%s\n%s\n" "$code" "${title:-$code}" ;;
                esac
                code="$(echo "$line" | sed -e 's/^locale: \([^ ]*\).*$/\1/')"
                title=""
                ;;
            *title\ \|*)
                title="$(echo "$line" | sed -e 's/^ *title | //')"
                ;;
        esac
    done < /tmp/.localeconfig.$$

    case "$code" in
        nb_NO|nb_NO.*|"") ;;
        *) printf "%s\n%s\n" "$code" "${title:-$code}" ;;
    esac
    rm -f /tmp/.localeconfig.$$
) | yad --center --width=520 --height=520 \
    --title "System language configuration" \
    --text "Please select your preferred system language (locale).\nThe current locale is: <b>$currentlocale</b>" \
    --list --no-headers \
    --column="Locale" --column="Description" \
    --button="Exit:1" --button="Select:0" \
    --print-column=1)

rc=$?
locale="${locale%|}"

if [ $rc -eq 0 ] && [ "$locale" ]; then
  touch /etc/profile.d/lang.sh $HOME/.profile
  setLocale $HOME/.profile
  if [ "$(id -u)" == "0" ]; then
    setLocale /etc/profile.d/lang.sh
  fi
  . /bin/bash "$HOME/.profile"
  if [ "$locale" != "$currentlocale" ]; then
    yad --center --title "Changes at next login" \
      --text "You will need to login again for changes to take effect." \
      --button="OK:0"
  fi
fi
# end
