#!/bin/sh
# Didier Spaier didieratslintdotfr 2019
# I wrote this script from scratch and put it in the public domain.
# I assume that the packages espeakup and speechd-up are installed.
# If fenrir is installed, it can also be used.
export TEXTDOMAIN=slint-scripts
confnote="# File written by speak-with. Do not edit."
if [ ! "$(id -u)" -eq 0 ]; then
	gettext "Please run this script as root."; echo
	exit
fi
if [ -x /usr/bin/espeakup ]; then
	espeakup="espeakup (Console screen reader connecting espeak-ng and speakup)\n"
	n1="espeakup"
fi
if [ -x /usr/bin/speechd-up ]; then
	speechd_up="speechd-up (Console screen reader connecting Speech Dispatcher and speakup)"
	n2="speechd-up"
fi
if [ -x /usr/bin/fenrir ]; then
	fenrir="fenrir (Modular, flexible and fast console screen reader)\n"
	n3="fenrir"
fi
readers="$espeakup$fenrir$speechd_up"
hardlist="$(zgrep SPEAKUP_S /proc/config.gz|grep '[ym]'|grep -v DUMMY| \
sed 's/CONFIG_SPEAKUP_SYNTH_//;s/..$//'|tr '[:upper:]' '[:lower:]')"
usage() {
  gettext "Usage: speak_with <screen reader> or <hard synthesizer> or 'none'"; echo
  gettext "Choose a console screen reader to talk with among:"; echo
  printf %b "$readers"; echo
  gettext "or one of the supported hard synthesizers:"; echo
  echo "$hardlist"
  gettext 'or type  "speak-with none" to mute all screen readers.'; echo
  echo
  exit
}
to_lower() {
echo "$1"|tr '[:upper:]' '[:lower:]'
}
ARGUMENT="$(to_lower "$1")"
if [ $# -ne 1 ]; then usage; fi
if [ ! "$ARGUMENT" = "$n1" ] && \
   [ ! "$ARGUMENT" = "$n2" ] && \
   [ ! "$ARGUMENT" = "$n3" ] && \
   ! echo "$hardlist"| grep -qw "$ARGUMENT" && \
   [ ! "$ARGUMENT" = "none" ]; then
	printf '%s' "$ARGUMENT "
	gettext "is not a valid argument of this command."; echo
	usage
	exit
fi
# We first check which console screen reader is already running, if any.
FENRIR="n"
SPEECHDUP="n"
ESPEAKUP="n"
[ ! "$(ps -C fenrir --noheaders|wc -l)" = "0" ] && FENRIR="y"
if [ -f /sys/accessibility/speakup/synth ]; then
	SPKSYNTH="$(cat /sys/accessibility/speakup/synth)"
	if [ "$SPKSYNTH" = "soft" ]; then 
		[ ! "$(ps -C speechd-up --noheaders|wc -l)" = "0" ] && SPEECHDUP="y"
		[ ! "$(ps -C espeakup --noheaders|wc -l)" = "0" ] && ESPEAKUP="y"
		fi
	else
	# $HARD is the name of the speakup driver for the hard synthesizer currently in use
	# case occurring
	HARD="$SPKSYNTH"
fi
# If a hard synth is in use its name "$HARD" has been stored in /etc/speakup.conf where
# we need 
# use a soft synth instead if wished, but we need to restore it if the
# user does not want a permanent change.
if [ -f /etc/speakup.conf ]; then
# shellcheck source=/dev/null
	. /etc/speakup.conf
fi
case $ARGUMENT in
	espeakup)
		[ ! -x /usr/bin/espeakup ] && gettext "espeakup is not installed." && echo && exit
		[ "$ESPEAKUP" = "y" ] && gettext "espeakup is already started." && echo && exit
		[ "$FENRIR" = "y" ] && [ -f /etc/rc.d/rc.fenrir ] && sh /etc/rc.d/rc.fenrir stop
		[ "$SPEECHDUP" = "y" ] && sh /etc/rc.d/rc.speechd-up stop
		echo "$confnote" > /etc/speakup.conf
		echo "hard=none" >> /etc/speakup.conf
		
		sh /etc/rc.d/rc.espeakup start
		sleep 1
		gettext "Should espeakup be also started at next boot? [Y/n] "
		read -r ANSWER
		if [ ! "$(to_lower "$ANSWER")" = "n" ]; then
			chmod 755 /etc/rc.d/rc.espeakup
			chmod 644 /etc/rc.d/rc.speechd-up
			[ -f /etc/rc.d/rc.fenrir ] && chmod 644 /etc/rc.d/rc.fenrir
			gettext "Done."
			echo
		elif [ "$HARD" ]; then
			echo "$confnote" > /etc/speakup.conf
			echo hard="$HARD" >> /etc/speakup.conf
		fi
	;;
	speechd-up)
		[ ! -x /usr/bin/speechd-up ] && gettext "speechd-up is not installed." && echo && exit
		[ "$SPEECHDUP" = "y" ] && gettext "speechd-up is already started." && echo && exit
		[ "$FENRIR" = "y" ]  && [ -f /etc/rc.d/rc.fenrir ] && sh /etc/rc.d/rc.fenrir stop
		[ "$ESPEAKUP" = "y" ] && sh /etc/rc.d/rc.espeakup stop
		echo "$confnote" > /etc/speakup.conf
		echo "hard=none" >> /etc/speakup.conf
		sh /etc/rc.d/rc.speechd-up start
		sleep 1
		gettext "Should speechd-up be also started at next boot? [Y/n] "
		read -r ANSWER
		if [ ! "$(to_lower "$ANSWER")" = "n" ]; then
			chmod 644 /etc/rc.d/rc.espeakup
			[ -f /etc/rc.d/rc.fenrir ] && chmod 644 /etc/rc.d/rc.fenrir
			chmod 755 /etc/rc.d/rc.speechd-up
			gettext "Done."
			echo
		elif [ "$HARD" ]; then
			echo "$confnote" > /etc/speakup.conf
			echo hard="$HARD" >> /etc/speakup.conf
		fi
	;;
	fenrir)
		[ ! -x /usr/bin/fenrir ] && gettext "fenrir is not installed." && echo && exit
		[ "$FENRIR" = "y" ] && gettext "fenrir is already started." && echo && exit
		[ "$SPEECHDUP" = "y" ] && sh /etc/rc.d/rc.speechd-up stop
		[ "$ESPEAKUP" = "y" ] && sh /etc/rc.d/rc.espeakup stop
		[ "$SPKSYNTH" ] && echo none > /sys/accessibility/speakup/synth
		echo "$confnote" > /etc/speakup.conf
		echo "hard=none" >> /etc/speakup.conf
		sh /etc/rc.d/rc.fenrir start
		sleep 1
		clear
		gettext "Should fenrir be also started at next boot? [Y/n] "
		read -r ANSWER
		if [ ! "$(to_lower "$ANSWER")" = "n" ]; then
			chmod 644 /etc/rc.d/rc.espeakup
			chmod 644 /etc/rc.d/rc.speechd-up
			chmod 755 /etc/rc.d/rc.fenrir
			gettext "Done."
			echo
		elif [ "$HARD" ]; then
			echo "$confnote" > /etc/speakup.conf
			echo hard="$HARD" >> /etc/speakup.conf
		fi
	;;
	none)
		[ "$FENRIR" = "y" ] && sh /etc/rc.d/rc.fenrir stop
		[ "$ESPEAKUP" = "y" ] && sh /etc/rc.d/rc.espeakup stop
		[ "$SPEECHDUP" = "y" ] && sh /etc/rc.d/rc.speechd-up stop
		[ ! "$SPKSYNTH" = "" ] && echo none > /sys/accessibility/speakup/synth
		gettext "Do you also want a mute console at next boot? [Y/n] "
		read -r ANSWER
		if [ ! "$(to_lower "$ANSWER")" = "n" ]; then
			chmod 644 /etc/rc.d/rc.espeakup
			chmod 644 /etc/rc.d/rc.speechd-up
			[ -f /etc/rc.d/rc.fenrir ] && chmod 644 /etc/rc.d/rc.fenrir
			echo "$confnote" > /etc/speakup.conf
			echo "hard=none" >> /etc/speakup.conf
			gettext "OK"; echo
		fi
	;;
	*)
		[ "$FENRIR" = "y" ] && sh /etc/rc.d/rc.fenrir stop
		[ "$ESPEAKUP" = "y" ] && sh /etc/rc.d/rc.espeakup stop
		[ "$SPEECHDUP" = "y" ] && sh /etc/rc.d/rc.speechd-up stop
		if ! modprobe "speakup_$ARGUMENT" 2>/dev/null; then
			gettext "Unable to switch to the synthesizer "
			echo "speakup_$ARGUMENT"
			echo
			exit 1
		fi
		echo "$ARGUMENT"  > /sys/accessibility/speakup/synth
		gettext "Do you also want to use this hard synthesizer at next boot? [Y/n] "
		read -r ANSWER
		if [ ! "$(to_lower "$ANSWER")" = "n" ]; then
			chmod 644 /etc/rc.d/rc.speechd-up
			[ -f /etc/rc.d/rc.fenrir ] && chmod 644 /etc/rc.d/rc.fenrir
			chmod 755 /etc/rc.d/rc.espeakup
			echo "$confnote" > /etc/speakup.conf
			echo "hard=$ARGUMENT" >> /etc/speakup.conf
			gettext "Done."; echo
		fi
esac
