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

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


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

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

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

mpg123 -a ${ALSA_INT} -q $files

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

