#!/bin/sh
#Modifications and changes May 23 2004.
# Added more comments
# 1. Fixed bug to detect when xmms is not running and
#     exit without any error message
# 2. Added test to ensure that we don't get the IP and port number when
#     xmms is changing songs, that is waits for 3 seconds and try again..
#     In such situations
# 3. Found BUG in previous version that lead to incomplete song title
#     that is cut -f2 should be cut -f2-
#Date --
# Original version
CMD="/usr/bin/xmms-shell"
EGREP="/bin/egrep"
CUT="/bin/cut"

ARES=`$CMD -e currenttrack 2>/dev/null`
#First a test to see if xmms is running
#NULL output from xmms-shell means it is not running
[ -z "${ARES}" ] && exit 0
#If we were passed a parameter then work slowly
if [ -n "$1" ]
then
	sleep 3
	ARES=`$CMD -e currenttrack 2>/dev/null`
fi
#Now a test to check xmms did not return just the station IP address
#and port number
A=`echo ${ARES} | ${CUT} -f2- -d: | ${CUT} -c5- | ${EGREP} -i '[a-z]'`
if [ -n "${A}" ]
then
	#Okay the title is okay..
	echo ${A}
	exit 0
fi
#here we had a station ip and port only, wait 3 seconds
#and try again..
sleep 3
exec ${CMD} -e currenttrack | ${CUT} -f2- -d: | ${CUT} -c5-
