#!/bin/sh
#this main script created by rmdijkman@wl500g.info at http://www.wl500g.info/showthread.php?t=1670
PLAYLISTFILE=/tmp/playlist
PIDFILE=/tmp/madscript.pid
NRFILE=/tmp/madnr

if [ -f $PIDFILE ]; then
	exit 0
fi

echo $$ > $PIDFILE

while [ true ] ; do
	i=1
	PLAYNR=`cat $NRFILE`
	NRPLAYED=0
	while read f ; do
		if [ $i -eq $PLAYNR ] ; then
			if [ "${f##*\.}" = "url" ]
			then
			##play_stream by Neonicus@gmail.com
				./play_stream "$f" 
#				stime=`/bin/date '+%b %e %H:%M:%S'`
#				echo "${stime}: play_stream $f" >> /opt/var/log/messages
			else
			##play_mp3 by Neonicus@gmail.com
				./play_mp3 "$f" 
#				stime=`/bin/date '+%b %e %H:%M:%S'`
#				echo "${stime}: play_mp3 $f" >> /opt/var/log/messages
			fi
			nice -n 20 madplay
			wait `pidof madplay`
			PLAYNR=`cat $NRFILE`
			PLAYNR=$(expr $PLAYNR + 1)
			echo $PLAYNR > $NRFILE
			NRPLAYED=1
			break
		fi
		i=$(expr $i + 1)
	done < $PLAYLISTFILE
	if [ $NRPLAYED -eq 0 ] ; then
		break
	fi
done

rm $PIDFILE
