#!/bin/sh
if [ ! $(id -u) -eq 0 ]; then
	gettext -s "Please run this script as root."
	exit
fi

if [ ! -f /sys/accessibility/speakup/synth ]; then
	gettext -s  "speakup being not in use, there is no setting to restore."
	exit
elif [ "$(</sys/accessibility/speakup/synth)" = "none" ]; then
	gettext -s "speakup being not in use, there is no setting to restore."
	exit
fi

SYNTH=$(</sys/accessibility/speakup/synth)
ESPEAKUP=$(ps -C espeakup --noheaders|wc -l)
SPEECHD_UP=$(ps -C speechd-up --noheaders|wc -l)
# We saved settings separately for:
# espeakup
# each hard synthesizer
# speechd-up
# so that we restore the relevant settings for the synthesizer and
# screen reader in use.

SAVEDSYNTH="$SYNTH"
if [ "$SYNTH" = "soft" ] && [ $ESPEAKUP -ne 0 ]; then
	SAVEDSYNTH="espeakup"
fi
if [ "$SYNTH" = "soft" ] && [ $SPEECHD_UP -ne 0 ]; then
	SAVEDSYNTH="speechd-up"
fi
# If SYNTH has not been set to speechd-up or speakup a hard synth is
# in use, like e.g. SAVEDSYNTH=apollo
if [ ! -d /var/lib/speakup/$SAVEDSYNTH ]; then
	gettext -s "No speakup settings saved for $SAVEDSYNTH, so nothing to restore."
	exit
fi
cd /var/lib/speakup/$SAVEDSYNTH
ls|while read i; do
	if [ -w /sys/accessibility/speakup/$i ] && [ -f /sys/accessibility/speakup/$i ]; then
		cp $i /sys/accessibility/speakup/
	fi
done
if [ -d i18n ] && [ -d /sys/accessibility/speakup/i18n ]; then
	( cd i18n
	ls|grep -v chartab|while read i; do
		if [ -w /sys/accessibility/speakup/i18n/$i ]; then
			cp $i /sys/accessibility/speakup/i18n/
		fi
	done
	)
fi
( cd $SYNTH
ls|while read i; do
	if [ -w /sys/accessibility/speakup/$SYNTH/$i ]; then
			cp $i /sys/accessibility/speakup/$SYNTH/
		fi
	done
	)
gettext -s "speakup settings have been restored for $SAVEDSYNTH."


