#!/bin/ash
# let user choose a language from installed mo files for app
# was implemented in welcome1stboot.bac first
# L18L Dec 2014 - Feb 2015 GPL

die() { echo $1; exit 1;}

usage()
{
 echo "
 usage: ${0##*/} textdomain [  \"app [ argument(s) ]...\" ]
 
 app only needed if different from textdomain
 choose a language from available textdomains
 app will be launched in chosen language or English if cancelled
 
 -h this help
 -v version
 
Examples:
 ${0##*/} geany \"geany $HOME/my-applications/bin/README-my-applications-bin.txt\"   
 "    
}

config()
{
  # some files
  all_LANGUAGE_names=/usr/share/i18n/lang_names
  [ -f $all_LANGUAGE_names ] || all_LANGUAGE_names=lang_names
  [ -f $all_LANGUAGE_names ] || die "missing file lang_names, exiting"
  these_languages=/tmp/language_names
  chosen_lang=/tmp/chosen_lang_code
  ICON=/usr/share/pixmaps/midi-icons/language48.png
  # window height
  win_h_min=18
  win_h_max=38
}

#input $0 $1 $2
[ "$1" = '-h' ] && { usage; exit 0; }
[ "$1" = '-v' ] && { echo ${0##*/} version $version; exit 0; }

textdomain="${1% *}" #1st word of ex: 'fatdog fatdog-choose-locale.sh'
[ "$textdomain" ] || { echo "which textdomain?"; read textdomain; }
[ "$textdomain" ] || die "which textdomain?"

app="${1#* }" #2nd word of ex: 'xwin xwin'
              # or same !!!
[ "$app" = $1 ] && app=$2
[ "$app" ] || app=$textdomain

#available languages (English + */textdomain.mo)
langs="en `find /usr/share/locale/*/LC_MESSAGES/${textdomain}.mo|cut -d'/' -f5`"

num_languages="`echo $langs | wc -w`"
[ "$num_languages" -lt 2 ] && { "$app"; exit 0; }
# English only user got app without further questions

config
rm -f $these_languages $chosen_lang
echo -n '' > $these_languages #all available languages
for x in $langs;do
 thisLANGUAGE="`grep -m 1 ^$x $all_LANGUAGE_names`"
 [ "`echo $thisLANGUAGE | grep failed`" ] && continue
 [ "$thisLANGUAGE" ] || thisLANGUAGE="${x}:`grep -m 1 ^${x%_*} $all_LANGUAGE_names | cut -d':' -f2`_${x##*_}"
 [ "`echo $thisLANGUAGE | grep failed`" ] && continue
 thisLANGUAGE=${thisLANGUAGE// /_}
 echo "${thisLANGUAGE}" | sed 's/:/ /' >> $these_languages
done

echo '' > $chosen_lang
if [ "$DISPLAY" ]; then
  h=$(($win_h_min + `wc -l $these_languages | cut -d' ' -f1 `))
  [[ $h -gt $win_h_max ]] && h=$win_h_max
  Xdialog --icon "$ICON" --no-cancel --title " " --menu "${app##*/}" $h 28 0 `cat $these_languages` 2>$chosen_lang
 #Xdialog --icon "$ICON" --no-cancel --title "$app" --menu " " -1 -1 0 `cat $these_languages` 2>$chosen_lang
else
  dialog --clear --nocancel --menu " " 0 0 0 `cat $these_languages` 2>$chosen_lang
fi

#filter lines in $chosen_lang length between 2 and 6
chosen_lang_code=$(awk 'length < 7' $chosen_lang | awk 'length > 1') #example: "nl"
echo $chosen_lang_code > $chosen_lang

#prepend chosen_lang_code to envirinment variable LANGUAGE
[ "$LANGUAGE" ] && LANGUAGE=":${LANGUAGE}"          #example: ":fr:de:"
LANGUAGE="${chosen_lang_code}${LANGUAGE}"           #example: "nl:fr:de:"

LANGUAGE=$LANGUAGE exec "$app" #launch application

exit 0
#end

