#!/bin/sh
#
# Starts Sonic IP announcement

# Use Barix utilities.
source /lib/config/functions.sh
source /usr/local/sbin/barix-sound-ctrl.sh

# Read config file if it is present.
if [ -r /etc/sonic-ip.conf ]
then
	. /etc/sonic-ip.conf
else
	echo "SonicIP configuration file /etc/sonic-ip.conf not found!"
	SONICIP_VOLUME="50"
fi

IFC=eth0

SOUND_CARD_CONF=/etc/barix/soundcard.conf

start() {
	if cfg_bool_is_true network.sonic_ip.enabled ; then
		sound_set_amplifier 1
		echo -n "Starting sonic IP announcement with volume $SONICIP_VOLUME :"
		if [ -f $SOUND_CARD_CONF ] ; then
			MASTER_CONTROL=$(cat $SOUND_CARD_CONF | grep "master.control" | sed -e 's/master.control\s*=\s*//')
			/usr/bin/amixer -M -q set "$MASTER_CONTROL" $SONICIP_VOLUME%
		else
			/usr/bin/amixer -M -q sset DAC $SONICIP_VOLUME%
		fi
		/usr/bin/sonic-ip ${IFC} &
	else
		echo "Sonic IP is disabled, skipping"
	fi
}
restart() {
	start
}

case "$1" in
  start)
	start
	;;
  stop)
	# no action
	;;
  restart|reload)
	restart
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?

