#!/bin/sh

. /etc/control.d/functions

SPEECHDCONF="/etc/speech-dispatcher/speechd.conf"

new_help alsa   "Use alsa system"
new_help pulse   "Use pulse system"
new_help oss	"Use oss system"
new_help pipewire	"Use pipewire system"
new_help nas		"Use nas system"
new_help libao	"Use libao system"

new_summary "Select speech-dispatche audio output method"

show_mode() {
	(grep -e "^AudioOutputMethod" $SPEECHDCONF || echo "0 undefined") | awk -c '{print $2}' | sed 's/\"//g'
}

set_mode() {
	if grep -e "^AudioOutputMethod" $SPEECHDCONF > /dev/null; then 
		line=`grep -e "^AudioOutputMethod" $SPEECHDCONF`
		sed -i "s/$line/AudioOutputMethod \"$REQUEST\"/g" $SPEECHDCONF
	else
		echo AudioOutputMethod \"$REQUEST\" >> $SPEECHDCONF
	fi
}

set -u
REQUEST="$*"
case "$REQUEST" in
	help|'help '*) control_help "${REQUEST#help}" ;;
	list) control_list ;;
	summary) control_summary ;;
	status) show_mode ;;
	alsa|pulse|oss|pipewire|nas|libao) set_mode "$REQUEST" ;;
	*) echo >&2 "Invalid mode: $*"; exit 1 ;;
esac


