#!/bin/sh -e

. /usr/share/debconf/confmodule

db_get espeakup/voice
default_voice=$RET
[ -n "$default_voice" ] || exit 0
db_get espeakup/card
ALSA_CARD=$RET

apt-install espeakup alsa-utils || true

case $default_voice in
	mb-*)
		mbrola_voice=${default_voice#mb-}
		apt-install espeak-ng mbrola-$mbrola_voice || true
		;;
esac

mkdir -p /var/lock
mkdir -p /var/lib/alsa
if alsactl store
then
	cp /var/lib/alsa/asound.state /target/var/lib/alsa/ || true
fi

case $default_voice in
	en | en-*) ;;
	*)
		cat > /target/etc/modprobe.d/speakup_soft.conf << EOF
options speakup_soft direct=1
EOF
		;;
esac

cp /usr/share/espeakup-udeb/espeakup.default /target/etc/default/espeakup
sed -i -e "s/^default_voice=.*/default_voice=$default_voice/" /target/etc/default/espeakup
[ -z "$ALSA_CARD" ] || sed -i -e "s/^# ALSA_CARD=.*/ALSA_CARD=\"$ALSA_CARD\"/" /target/etc/default/espeakup
