#!/bin/sh
export TEXTDOMAIN=slint-scripts
STANDARD=$(mktemp)
MBALL=$(mktemp)
MBINSTALLED=$(mktemp)
MBNOTINSTALLED=$(mktemp)
espeak-ng --voices| \
sed "s?[ ]\{1,\}? ?g;s|File|Voice|;s|Age/Gender|Code|"| \
cut -d" " -f3-5|cut -d" " --complement -f 2 >"$STANDARD"
column -t "$STANDARD"
echo
THISVOICE="$(tail -n 1 "$STANDARD"|cut -d" " -f2)"
THISCODE="$(tail -n 1 "$STANDARD"|cut -d" " -f1)"
gettext "To use one of theses voices with espeakup, set it in /etc/espeakup.conf."; echo
gettext "For instance, to use the voice: "; echo
echo  "$THISVOICE"
gettext  "write:"; echo
echo  "voice=$THISCODE"
gettext "in /etc/espeakup.conf."; echo
echo
question=$(gettext "Do you want to list also the mbrola voices? [y/N]")
printf '%s' "$question "
read -r ANSWER
if [ "$ANSWER" = "y" ] || [ "$ANSWER" = "Y" ];  then
	unset MISSING
	espeak-ng --voices=mb| \
	sed "s, ,,;s;[ ]\{1,\};,;g;s|File|Voice|;s|Other|Code|"| \
	cut -d, -f4-5|sed s,mb/,, >> "$MBALL"
	echo "Code Voice" > "$MBINSTALLED"
	while read -r i; do
		F1=$(echo "$i"|cut -d, -f2)
		F2=$(echo "$i"|cut -d, -f1)
		thisfile="$(echo "$F1"|sed "s|mb-||")"
		if [ -d /usr/share/mbrola/"$thisfile" ]; then
			echo "$F1 $F2" >> "$MBINSTALLED"
		else
			echo "$F1 $F2" >> "$MBNOTINSTALLED"
		fi
	done < "$MBALL"
	gettext "Installed mbrola voices:"; echo
	column -t "$MBINSTALLED"
	THISVOICE="$(tail -n 1 "$MBINSTALLED"|cut -d" " -f2)"
	THISCODE="$(tail -n 1 "$MBINSTALLED"|cut -d" " -f1)"
	gettext "To use one of theses voices with espeakup, set it in /etc/espeakup.conf."; echo
	gettext "For instance, to use the voice: "; echo
	echo  "$THISVOICE"
	gettext  "write:"; echo
	echo  "voice=$THISCODE"
	gettext "in /etc/espeakup.conf."; echo
	echo
	gettext "Other mbrola voices could be used if installed."; echo
	question=$(gettext "Do you want to display them? [Y/n] ")
	printf '%s' "$question "
	read -r ANSWER
	if [ "$ANSWER" = "n" ] || [ "$ANSWER" = "n" ];  then
		exit
	fi
	echo
	gettext "Not Installed mbrola voices:"; echo
	column -t "$MBNOTINSTALLED"
	THISVOICE="$(tail -n 1 "$MBNOTINSTALLED"|cut -d" " -f2)"
	THISCODE="$(tail -n 1 "$MBNOTINSTALLED"|cut -d" " -f1)"
	gettext "Other mbrola voices could be used if installed."; echo
	gettext "For instance, to use the voice: "; echo
	echo  "$THISVOICE"
	gettext "type as root:"; echo
	printf '%s' "slapt-get -i mbrola-voice-"
	echo "$THISCODE"|sed 's|^mb-||'
	gettext "then write:"; echo
	echo "voice=$THISCODE"
	gettext "in /etc/espeakup.conf."; echo
	echo
fi
rm "$STANDARD" "$MBALL" "$MBINSTALLED" "$MBNOTINSTALLED"
