#!/bin/sh
PIDFILE=/var/run/espeakup.pid

. /usr/share/debconf/confmodule

set -e

locale="$1"

[ -f $PIDFILE ] || exit 0

lang=`echo $locale | sed 's/_.*//'`
country=`echo $locale | sed 's/.*_//;s/\..*//;s/@.*//' | tr A-Z a-z`

if [ "$lang" = gl ]
then
        # Not supported by espeak yet, but pt should be fine enough, better than
        # english anyway
	lang=pt
fi

if grep -r "^language $lang-$country$" /usr/lib/*/espeak-data/voices
then
	VOICE="$lang-$country"
elif grep -r "^language $lang$" /usr/lib/*/espeak-data/voices
then
	VOICE="$lang"
else
	VOICE=en
fi

db_set espeakup/voice $VOICE

[ $lang = en ] || echo 1 > /sys/accessibility/speakup/soft/direct

kill `cat $PIDFILE`
while [ -f $PIDFILE ]; do sleep 1 ; done
log-output -t espeakup espeakup -V $VOICE > /dev/null 2>&1 </dev/null &
while [ ! -f $PIDFILE ]; do sleep 1 ; done
