#!/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" = nb ]
then
	lang=no
fi

if [ -f /usr/lib/*/espeak-data/voices/$lang-$country ] || \
   [ -f /usr/lib/*/espeak-data/voices/$lang/$lang-$country ]
then
	VOICE="$lang-$country"
elif [ -f /usr/lib/*/espeak-data/voices/$lang ] || \
     [ -f /usr/lib/*/espeak-data/voices/$lang/$lang ]
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
