#!/bin/sh

# fatdog-quick-locale-switcher
# switch between locales without restart of X server
# icon by courtesy of http://www.languageicon.org/
  Version=1.3
# copyright L18L November 2014 - Mar 2015 
# License: GPL Version 3 or laterGPL


  Version=1.5 # oct 20, 2015


export TEXTDOMAIN=fatdog

WDIR=/tmp/toggleLANG; mkdir -p $WDIR # working directory WD

#trap 'rm -rf $WDIR; exit; ' 0 INT HUP TERM # cleanup

die() { Xdialog --title "${0##*/} " --backtitle "$(gettext 'ERROR')" --msgbox "$1" 0 0; exit 1; }

usage()
{
 local ACTION TEXT URL
 TEXT=
 ACTION=
 
 if [ ! -d /usr/share/locale/af/ ]; then
  [ "`whoami`" = "root" ] || TEXT="$(gettext 'Your administrator can add Native Language Support.')"  	
  TEXT="$TEXT 
  $(gettext 'Download and load Native Language Support now?')"	
  ACTION=download
 else
  TEXT=" $(gettext 'Usage:')
  ${0##*/} [$(gettext 'OPTION')] [ $(gettext 'newLANG') ]

 $(gettext 'OPTION:') 
  -h $(gettext 'this help')
 
 $(gettext 'Examples:')
  ${0##*/} en
  ${0##*/} en_AU
  ${0##*/} en_CA
  ${0##*/} en_US
  ${0##*/} nl_BE
 
  ${0##*/} _______ $(gettext 'GUI')


  "
 
  [ "`whoami`" = "root" ] || TEXT="$TEXT
  $(gettext 'Your administrator can add a locale.')"  
  TEXT="$TEXT
  $(gettext 'Add a locale now?')"	
 fi

 Xdialog --icon "$LOCALES_ICON"  --title "${0##*/}" --left --backtitle "$(eval echo `gettext 'Version $Version'`)" --default-no --yesno "
 $(gettext 'Purpose:')
 $(gettext 'switch to one of your existing UTF-8 locales')

 $TEXT " 0 0 
 case $? in 0) # yes button pressed
  case $ACTION in 
    download) # something like this from sfsmanager?
      . /etc/DISTRO_SPECS # ex: DISTRO_VERSION=700  DISTRO_FILE_PREFIX=fd64
      NLS_FILE=${DISTRO_FILE_PREFIX}-nls_${DISTRO_VERSION}.sfs
      
      SFSDEST=$(readlink /mnt/home) # DVD saves is okay - it has /mnt/home symlink
      [ "$SFSDEST" ] || { SFSDEST=$WDIR/sfs; mkdir $SFSDEST 2> /dev/null; }
      cd $SFSDEST

      FD_DL_URL=http://distro.ibiblio.org/pub/linux/distributions/fatdog # config???
      FD_DL_URL=http://ftp.cc.uoc.gr/mirrors/linux/fatdog # much faster
      URL=${FD_DL_URL}/sfs/${DISTRO_VERSION}/${NLS_FILE} 

      urxvt -e wget -c $URL || die "Download of ${NLS_FILE} to $SFSDEST not finished."
      load_sfs.sh --load ${SFSDEST}/${NLS_FILE} || die "Could not load $SFSDEST/${NLS_FILE}"
      ;;
  esac
  fatdog-choose-locale.sh ;;
 esac
 
 exit 1
}

config()
{
# some files
all_LANGUAGE_names=/usr/share/i18n/lang_names
[ -f $all_LANGUAGE_names ] || die "${all_LANGUAGE_names}. $(gettext 'This file not found; exiting.')"

allLOCALES=/usr/share/i18n/locales

my_languages=$WDIR/language_names
chosen_lang=$WDIR/chosen_lang_code

PUPPYPIN=$XDG_CONFIG_HOME/rox.sourceforge.net/ROX-Filer/PuppyPin # fatdog
[ -f $PUPPYPIN ] || PUPPYPIN=$HOME/Choices/ROX-Filer/PuppyPin    # puppy

LOCALES_ICON=/usr/share/pixmaps/midi-icons/language48.png
ICON=/usr/share/icons/fatdog32.png
LANG_CONFIG=$FATDOG_STATE_DIR/language # LANG
}

move_line_to_top_of_file ()  #  $1 is line; $2 is file
{ grep -v "$1" $2 > ${2}_ && { echo $1 > $2; cat ${2}_ >> $2; rm ${2}_ ; } ; }

list_my_languages() {
  locale -a | grep utf8 | cut -d'.' -f1 > $WDIR/mylocales
  
  move_line_to_top_of_file "`grep ${LANG%.*} $WDIR/mylocales`" $WDIR/mylocales
  rm $my_languages 2>/dev/null

  while read LINE; do
    L="`grep ^${LINE%%.*} $all_LANGUAGE_names`"
    [ "$L" ] || L="`grep -m 1 ^${LINE%%_*} $all_LANGUAGE_names`"
    [ "$L" ] || { L="`grep -m1 '^language' ${allLOCALES}/${LINE%%.*}`"; L="${L#*\"}"; L="${L%\"*}"; } #ex: Upper Sorbian
    L="${L// /_}" #ex: Upper_Sorbian
    [ "$L" ] || L="${LINE%_*}" # fallback to language code
    [ "${LINE:0:3}" = "en_" ] && C="${LINE#*_}_" || C=  # ex:US_English
    echo "$LINE ${C}${L#*:}" >>  $my_languages    # ex:en_US US_English
  done < $WDIR/mylocales
}

ask_for_new_language()
{
h=$((10 + `wc -l $my_languages | cut -d' ' -f1 `))
[[ $h -gt 26 ]] && h=26
Xdialog --buttons-style icon --icon "$LOCALES_ICON" --no-cancel --title " $app " --menu "..." $h 32 0 `cat $my_languages` 2>$chosen_lang

mv $chosen_lang ${chosen_lang}_  # filter
while read LINE; do
  case $LINE in
            "") continue ;;
        *Gtk-*) continue ;;
     *process*) continue ;;
    *fallback*) continue ;;
     *Xdialog*) continue ;;
             *) echo $LINE >> ${chosen_lang} ;;
  esac
done < ${chosen_lang}_
}

############# end of functions ####################
config

#input $0 $1
[ "$1" = '-h' ] && { usage; exit 0; }
new_LANG=$1

cd $WDIR
list_my_languages # cat $my_languages && exit

grep ^${new_LANG} $WDIR/mylocales || die "${new_LANG}. $(gettext 'This is NOT one of your locales.')"

read old_LANG < $LANG_CONFIG

> $chosen_lang
[ "$new_LANG" ] && echo "${new_LANG}" > $chosen_lang || ask_for_new_language 
[ -f $chosen_lang ] || exit 0 # no input
read new_LANG < $chosen_lang

# append default UTF-8 
[ "`echo ${new_LANG} | grep -i utf `" ] || new_LANG="${new_LANG}.UTF-8"

[ "$new_LANG" = "$old_LANG" ] && exit 0
#echo changing LANG from $old_LANG to $new_LANG # debug only

#update profile
# sed -i "s/^LANG=${old_LANG}/LANG=${new_LANG}/" /etc/profile
echo ${new_LANG} > $LANG_CONFIG
export LANG=${new_LANG}

#switch menu and desktop to another LANG without restarting X server
source /usr/sbin/fatdog-choose-locale.sh.i18n

exit 0
