#!/bin/bash
# switch-language__-__key_l.sh
# Description: Fast language switching in the console
#
# Copyright 2018, F123 Consulting, <information@f123.org>
# Copyright 2018, Storm Dragon, <storm_dragon@linux-a11y.org>
# Copyright 2020, Didier Spaier <didier at slint dot fr>
#
# This 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, or (at your option) any later
# version.
#
# This software 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 package; see the file COPYING.  If not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
#--code--

# we save the previous languages list. This allows to reset the index
# in case the language list has been modified.
savedlanguages=""
savedindex=0
CONFIGFILE="/etc/fenrirscreenreader/settings/settings.conf"
if [ ! -f  $CONFIGFILE ]; then
	exit
fi
rawlanguages="$(grep ^languages= $CONFIGFILE|sed s/languages=//);"
if [ "$rawlanguages" = "" ]; then
	exit
fi
# discard parenthesis, replace ',' and ';' by spaces, remove leading and
# trailing spaces, remove consecutive spaces but the first one.
languages="$(echo $rawlanguages|sed "s/[()]//g;s/[,;]\{1,\}/ /g;s/^[ ]\{1,\}//;s/[ ]\{,\}$//;s/[ ]\{1,\}/ /")"
if [ "$languages" = "" ]; then
	exit
fi
module="$(grep ^module= $CONFIGFILE|sed s/module=//)"
if [ $(spd-list -s |grep -c "$module"$) -eq 0 ]; then
	echo "As the module "$module" is not available we can't switch languages."
fi
LANGCOUNT=$(echo $languages|wc -w)
if [ $LANGCOUNT -lt 2 ]; then
	exit
fi
if [ "$savedlanguages" != "$languages" ]; then
	sed -i "s/^savedlanguages=.*/savedlanguages=\"$languages\"/" $0
	sed -i "s/^savedindex=.*/savedindex=0/" $0
fi
if [ "$savedindex" = "" ]; then
	index=0
	savedindex=0
else
	index=$savedindex
fi
languages=$(echo $languages|sed "s/ /\n/g")
i=0
set_language() {
	echo "$languages"|while read language; do
	[ $i -eq $index ] && echo $language && return
	i=$(($i + 1))
	done
}
language=$(set_language)

# All the initial setup is done. Apply the new language.
available=$(spd-list -ls $module|sed "s/Available languages codes for $module://;s/ /\n/g")
if [ $(echo "$available"|grep -c "^${language}$") -eq 0 ]; then
	echo "As the language $language is not available for the module"
	echo "$module, we can't switch languages."
	exit
fi
if [ "${LANG%%.*}" != "$language" ]; then
	for i in /tmp/fenrirscreenreader-*.sock; do
		echo "setting reset" | socat - UNIX-CLIENT:$i
	done
fi

for i in /tmp/fenrirscreenreader-*.sock ; do
	echo "setting set speech#language=$language;speech#module=$module;general#spellCheckLanguage=$language" | socat - UNIX-CLIENT:$i
done
if [ $savedindex -lt $(($LANGCOUNT - 1)) ]; then
	index=$(($savedindex + 1))
else
	index=0
fi
sed -i "s/^savedindex=.*/savedindex=$index/" $0
