#!/bin/sh
# Sonic IP script
# (c)2012 Petr Kulhavy, Barix AG
# "says" the IP address of the selected network interface
#
# TODO: volume setting

if [ $# -lt 1 ] ; then 
	echo "Barix Sonic IP. Announces IP address of a network interface via audio output."
	echo "(c)2012 Barix AG"
	echo "Usage: $0 <interface>"
	echo "Example: $0 eth0"
	exit 1
fi


DATA_DIR="/usr/share/sonic-ip"
NET_IFC=$1

ip_addr=`ifconfig $NET_IFC | grep -o "inet addr:[^ ]*"`
ip_addr=${ip_addr#inet addr:}

if [ "$ip_addr"x = "x" ] ; then 
	# no IP address
	exit 1
fi

i=0
files=""
while [ $i -lt ${#ip_addr} ]; do 
	digit=${ip_addr:$i:1};  

	if [ "X$digit" == "X." ] ; then digit="dot"; fi
	files="$files $DATA_DIR/$digit.mp3"

	i=$((i+1));
done

ALSA_INT=`uci -q get application.audio.silent_playback`
if [ "$ALSA_INT" = "" ]; then
	#add 1s silence at the beginning to allow the alsa device to wake up
	# properly since the silence "play" loop will not be started
	files="$DATA_DIR/sil.mp3 $files"
	ALSA_INT="default"
fi

mpg123 -a ${ALSA_INT} -q $files

echo "Restoring alsa state"
/usr/sbin/alsactl restore

