#!/bin/bash

# Tentatively named "playinx" ?
# Using mplayer to play m3u ( magnatunes.com etc.) 
# A "front end" should probably control stop/start/pause/track choice etc.
# Yet another project... at least this seems to play better than cplay for streams

PATH="$PATH:/usr/local/lib/inx"
. f-colours

clear
bold
green
figlet inx"  "player
white
unbold
echo
sleep 1 # short splash :)
COUNTER=0

# This is not necessary until I work out how to implement choosing and control...

#if ! [ $PLAYLIST = "" ] ; then
#PLAYLIST=$[PLAYLIST + 1]
#else
#PLAYLIST=1
#fi

# Check for non m3u formats and play without "playlist" "ram" files seem OK with playlist
# Consider saving the actual list files? ( i.e. .asx etc) for later use?

echo "$@" | egrep "(.wma|.mms|.wma|.ogg|.avi|.mp3|.aac|.mov|.flv|.mpg|.mpeg|.asf)" > /dev/null 2>&1
if [ $? = 0 ] ; then
mplayer "$@" 2> /dev/null | egrep -i "(Name|Genre|Website|Bitrate|ICY)"
exit
fi

echo "$@" | egrep "(.pls|.asx)" > /dev/null 2>&1
if [ $? = 0 ] ; then
mplayer -playlist "$@" 2> /dev/null | egrep -i "(Name|Genre|Website|Bitrate|ICY)"
exit
fi

# This works OK from here down for m3u

for each in $(cat "$@") 
do 
COUNTER=$[COUNTER + 1]
tmpdir="$(mktemp -d)"
echo "$each" > $tmpdir/TRACK.$COUNTER
done

bold
yellow
echo "> Move forwards in playlist 	< Move backwards in playlist"
echo "/ Lower volume			* Higher volume"
echo "p Pause		m Mute		q Quit"
echo
unbold
white

for name in $(cat $tmpdir/TRACK.* | sort)
do
basename "$name" \
| sed -s 's/\%20/ /g'\
| tr '_' ' '
done
echo
yellow 
unbold

# Do we need the "Playing" output ? Might remove the grep here... scrolling issues
/usr/bin/mplayer -really-quiet -playlist "$@" > /dev/null 2>&1 #| grep Playing

echo


# Clean up - might change this since some people could use the files...
rm -rf $tmpdir
white
unbold
clear

