#!/bin/sh
# 
# talkwith -- switches speakup synthesizers on the fly
# 
# Bug reports, suggestions, etc. to chuckh@ftml.net
# 
# Copyright (c) 2008 by Charles Hallenbeck, chuckh@.ftml.net
# This script is offered under the terms of the GNU General Public License,
# version 2or higher, shown in the file "COPYING" included with the script.
# Please read the terms and conditions of GPL before using this script.
# 
# Requires Linux kernel 2.6.24 or higher and must be run as root.
# Talkwith requires espeak and espeakup for software speech
# To install talkwith, simply copy this script to /usr/local/sbin
# On my system, the /etc/rc.local file contains statements like these:
# 
# start talking
# export PATH=/usr/local/bin:/usr/local/sbin:$PATH
# talkwith soft
# 
if [ "$1" = "" ] || [ "$2" != "" ] || \
[ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "Usage: `basename $0` <synth>"
echo
echo "You may use one of the following synthesizer names if the driver"
echo "has been selected and the corresponding device is available:"
echo "acntsa, acntpc, apollo, audptr, bns, dectlk, decext, decpc, dtlk,"
echo "keypc, ltlk, soft, spkout, txprt, dummy, or none."
echo "Use speakupconf to save speech settings to load at startup"
exit
fi
# 
if [ "$1" != "none" ] && \
# To disallow a particular device, comment out its line below:
[ "$1" != "acntsa" ] && \
[ "$1" != "acntpc" ] && \
[ "$1" != "apollo" ] && \
[ "$1" != "audptr" ] && \
[ "$1" != "bns" ] && \
[ "$1" != "dectlk" ] && \
[ "$1" != "decext" ] && \
[ "$1" != "decpc" ] && \
[ "$1" != "dtlk" ] && \
[ "$1" != "keypc" ] && \
[ "$1" != "ltlk" ] && \
[ "$1" != "spkout" ] && \
[ "$1" != "txprt" ] && \
[ "$1" != "dummy" ] && \
# do not comment out the next line!
[ "$1" != "soft" ]; then
echo "Unrecognized synthesizer type: \"$1\""
exit 1
fi
# 
test $UID -eq "0" || exit 0
#
test -d /sys/module/speakup/parameters || exit 0
#
F1="`cat /sys/module/speakup/parameters/synth`"
if [ "$F1" != "none" ]; then
echo "none" > /sys/module/speakup/parameters/synth
if [ "$F1" = "soft" ]; then
kill `cat /var/run/speechd-up.pid 2>/dev/null` 2>/dev/null
kill `cat /var/run/espeakup.pid 2>/dev/null` 2>/dev/null
fi
sleep 1
fi
# 
test "$1" != "none" || exit 0
#
echo "$1" >/sys/module/speakup/parameters/synth
if [ "$1" = "soft" ]; then
espeakup >/dev/null 2>&1
fi
#
# now load synth specific parameters
#
test -d /etc/speakup/ || exit 0
cd /etc/speakup/$1
for i in * ; do
    if [ -w /sys/module/speakup/parameters/$i ]; then
      cat $i >/sys/module/speakup/parameters/$i
fi
done
#
# end of talkwith
