#!/bin/bash

PATH=$PATH:/usr/local/lib/inx
. inx_essentials
maybe_switch


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-plinput ()
{
clear

fig-col
figlet "  Random "    " Radio !"
KEYS=
info-col
unbold
echo "  For information about the "Plait" player this uses,"
echo "  read the documents in $HOME/plait. "Plait" is written by Stephen Jungels "
bold
echo
head-col
echo "  Random Radio searches for Shoutcast streams, and plays them."
echo
echo "  Type the kind of music you would like to hear."
echo
echo "  For example: '"jazz"' or '"hard rock "' or '"flamenco"'"
printf "\n\n"
info-col 
echo "  PLAYER CONTROLS"
echo
echo "  l: Play whatever happens to be previously in the list. "
echo 
echo "  m: INX main menu. "
echo
echo "  w: Net and Web menu."
echo
echo "  r: INX radio menu."
echo
echo "  x: Give me a command prompt."
echo
printf "\n"
head-col
echo "  In each case hit <enter> to activate your search or command."
printf "\n"
f-ttynum

white
printf "  " ; read "KEYS"


    case "$KEYS" in

	l)
	export KEYS="$KEYS"
        plaitinx
	;;

	m)
	exec menu 
	;;

	r)	
	exec radio
	;;
	
	w)
	exec netinx
	;;
	
	x)
	clear
	f-prompt
	;;
	
	*)
	# Check for more than one character to avoid nonsense one-letter calls
	# Of course, plait will happily search for rubbish like "*%_@lgjdj84*&$)#" , but too bad :-)
	if [ "$(echo "$KEYS" | cut -c 2)" = "" ] ; then
		echo -e "\n  I didn't understand that request - try a new search."
		sleep 3
		f-plinput
	else
	    export KEYS="$KEYS"
		plaitinx 
	fi
	;;

    esac


}

f-plinput