#!/bin/bash
PATH=$PATH:/usr/local/lib/inx
. inx_essentials

maybe_switch

waiting () 
{ 
    printf "  Real Media takes a while to get going... Please be patient..."
    sleep 5
    line_erase
}    


stream_data () #Parse metadata from streams... I'm learning, slowly... P.G.
{
    info-col
    setterm -cursor off
    
    # This comment is here because I'm sure to forget... ;-)
    # Tell sed not to print results until told (-n)
    # Make substitutions for each found
    # Delete all found up to ICY from 'search space'
    # Tell sed to insert a line
    # Parse ICY info, removing StreamTitle=  and ";StreamUrl -> end of line, inclusive"
    # Finally, tell sed to print all the results...
    
    sed -n \
    -e 's/ -/:\n /g' \
    -e 's/\[asfheader\].*//p' \
    -e 's/\[real\].*//p' \
    -e '/shoutcast.com/d' \
    -e 's/^Playing/  Playing/p' \
    -e 's/^Ogg/  Ogg Format, Vorbis Audio/p' \
    -e 's/Name/\n  Name/p'  \
    -e 's/Genre/  Genre/p' \
    -e 's/Bitrate/  Bitrate/p' \
    -e 's/REAL/  Real Media /p' \
    -e 's/name://p' \
    -e '/^ICY/!d' \
    -e '/^ICY/i\ ' \
    -e 's/^ICY Info: StreamTitle=/ /; s/;.*//' \
    -e 'p' 
    
    setterm -cursor on
}

clear_and_exit ()
{
    tput sgr0 ; clear
    f-plinput
}

plaitinx ()
{
# Use a hack to make plait just look, then exit :)
# I'm sure there's a more refined way, but without
# Reading Stephen's excellent docs, I came up with this
# in a moment of... umm ... inspiration ( s/inspiration/laziness ) P.G.

bblack ; clear
bold ; head-col

PLAYLIST="$HOME/.plait/playlist.pls"

keep_trying ()
    {
    plait --list --stream "$KEYS" > "$PLAYLIST" 
    # This apparently needs time to get it right :/
    TRIES=$(( $TRIES + 1 ))
    if ! [ -s "$PLAYLIST" ] && [ $TRIES -lt 3 ] ; then
        keep_trying
    fi
    }

if [ "$KEYS" = "l" ] ; then
    : # Play what's already there, if anything.
else    
    echo -e "\n\n\n\n\n\t\tSearching for \" "$KEYS" \"... Please wait...\n"
    TRIES=0
    keep_trying
fi

if ! [ -s "$PLAYLIST" ] ; then
    sleep 1
    if [ "$KEYS" = "l" ] ; then
        echo -e "\n\n\n\n\n\t\tSorry, the playlist is empty...\n"
    else
        echo -e "\v\t\tSorry, couldn't find \""$KEYS"\" on Shoutcast...\n"
    fi	
    sleep 6
    clear_and_exit
fi    


white ; clear
info-col
figlet "  plaitinx"
help_stream
# For some reason plaitinx sometimes exits without doing a thing...
if mplayer -playlist "$PLAYLIST" 2> /dev/null | stream_data ; then
   clear_and_exit
else
   keep_trying
fi

}

help_stream () # basic help for mplayer in foreground
{
    head-col
    printf "   ________________________________________________________\n" 
    printf "  |                                                        |\n"
    fig-col
    printf "    Louder      Softer      Mute      Play|Pause      Quit\n"
    bblack
    info-col
    echo
    printf "      *           /          m       p or <space>       q \n"
    head-col
    printf "  |________________________________________________________|\n"
    echo
    white
}

function f-radiomenu ()
{
foo=

clear
fig-col
figlet "  inx Radio"
printf "\n"
head-col
echo "  Type the number or letter for your choice"
info-col
cat << EORADIO

  1: BBC3 - mostly Classical (U.K)
  2: Virgin Rock (U.K)
  3: Otto's Baroque Music (USA)
  4: Jazz Atlanta (USA)
  5: Rock - JJJ (Australia)
  6: Classic FM (Australia)
  7: ABC News Service (Australia)

EORADIO

head-col
echo "  s: Search Shoutcast for more music."
fig-col

cat << EOOPTS

  h: Help with Radio inx.
  f: Flip colours.
  w: Net and Web menu.
  m: INX main menu.
  x: Give me a command prompt.

EOOPTS

head-col
echo "  You can stop playing streams by pressing q "
printf "\n"

fig-col
f-ttynum
read -s -n 1 foo

case $foo in 
 
	1)
	white
	clear
	info-col
	figlet "  bbc 3"
	help_stream	
	fig-col ; sleep 1
	printf "  Please wait..." ; sleep 2 ; printf "  Connecting to BBC3..."
	sleep 2
	line_erase ; waiting
	bbc3-classical | stream_data
	f-radiomenu
	;;
	2)
	white
	clear
	info-col
	figlet "  Virgin Rock"
	help_stream
	fig-col ; sleep 1 
	printf "\n  Rock on! Suffer while we buffer..."
	sleep 1.5 
	printf "  Virgin Classic Rock!"
	sleep 3 ; line_erase
	virgin-classic-rock | stream_data
	f-radiomenu
	;;
	3)
	white
	clear
	info-col
	figlet "  Otto's Baroque"
	help_stream
	fig-col ; sleep 1
	printf "  Contacting ..."
	sleep 1
	printf "  Otto's Baroque Music "
	sleep 3 ; line_erase 
	printf "  Windows Media Stream\n\n"
	baroque | stream_data
	f-radiomenu
	;;
	4)
	white
	clear
	info-col
	figlet "  Jazz Colours"
	help_stream
	fig-col ; sleep 1
	printf "  Contacting ... "
	sleep 1
	printf "  Jazz from Atlanta " ; sleep 3
	line_erase 
	printf "  Windows Media Stream\n\n"	
	jazz | stream_data
	f-radiomenu
	;;
	5)
	white
	clear
	info-col
	figlet "  triple jjj"
	help_stream
	fig-col ; sleep 1
	printf "  Contacting ... "
	sleep 1
	printf "  ABC JJJ rocking now! " ; sleep 3
	line_erase
	printf "  Windows Media Stream\n\n"	
	rock-jjj | stream_data
	f-radiomenu
	;;
	6)
	white
	clear
	info-col
	figlet "  Classic  FM"
	help_stream
	fig-col ; sleep 1
	printf "  Please wait - contacting ..." 
	sleep 2
	printf "  ABC Classic FM... " ; sleep 3
	line_erase 
	printf "  Windows Media Stream\n\n"	
	classicfm-abc | stream_data
	f-radiomenu
	;;
	7)
	white
	clear
	info-col
	figlet "  Oz  News"
	help_stream
	fig-col ; sleep 1
	printf "  Please wait - contacting ..." ; sleep 2.5
	printf "  ABC News Service... " ; sleep 1
	line_erase ; waiting
	printf "\n"
	news-abc | stream_data
	f-radiomenu
	;;
	8)
	white
	clear
	info-col
	figlet "  Klassiek Baroque"
	help_stream
	fig-col ; sleep 1
	printf "  Please wait - contacting..." ; sleep 2
	printf "  AVRO Klassiek Baroque..." ; sleep 2
	line_erase ; printf "\n"
	mplayer http://145.58.33.31:8068/ 2> /dev/null | stream_data
	f-radiomenu
	;;
	h)
	clear
        help_stream
	head-col
	echo "  HIt any key to return to the radio menu."
	read -s -n 1
	white
	f-radiomenu 
	;;
	s)
	exec random
	;;
	w)
	exec netinx
	;;
	x)
	clear
	f-prompt
	;;
	m)
	clear
	exec menu
	;;
	f)
	switch
	f-radiomenu
	;;
	*)
	echo
	echo "  I didn't understand that command - please try again."
	sleep 2
	f-radiomenu
	;;
	
esac
}

f-radiomenu
