#!/bin/bash
# set -vx
# Tentatively named "playinx" ?
# Using mplayer for most streams and multimedia files...

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

heading ()
{   
    bblack
    clear
    bold
    echo
    fig-col
    figlet "  playinx"
    info-col
    sleep 1 # Better than having it flash too briefly before a video
    echo
}


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


stream_data () #Parse metadata from streams
{
    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 's/Name/  Name/p'  \
    -e 's/Genre/  Genre/p' \
    -e 's/Bitrate/  Bitrate/p' \
    -e 's/^STREAM_ASF/  Windows Media /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
}

screen_detect ()
{   
    bblack
    head-col
    printf "   ________________________________________________________\n" 
    printf "  |                                                        |\n"
    fig-col
    printf "    Playinx is running in GNU Screen. To continue play in   \n"
    printf "        the background, detach Screen with 'ctrl+a d        \n"
    head-col
    printf "  |________________________________________________________|\n"
    echo
    info-col
}

playinx_top ()
{
    heading
    help_stream
    if [ "$TERM" = "screen" ] ; then
    screen_detect    
    fi
}

play_as_playlist ()
{
    mplayer -playlist "$@" 2> /dev/null | stream_data
    outahere
}

outahere ()
{
    tput sgr0
    clear
    exit
}

# Usage :)

if [ "$1" = "something-or-other" ] ; then
    heading ; head-col
    echo -e "\n  A bit literal-minded. Let's try again... \n\n  'playinx something-or-other' \n 
  means 'playinx x , where x is the playlist or file that you wanted to play.' \n 
  Does that make it clearer? *grin* 
  Press <enter> to experiment further...\n"
    read
    outahere

elif [ "$#" -lt 1 ] ; then
    heading ; head-col    
    echo -e "\n  You didn't give me anything to play!\n  Try running 'playinx something-or-other'" 
    sleep 6
    outahere  
fi

# Stop mocp to avoid multiple streams playing. mocp retains playlists, so this isn't too drastic.
mocp -s

# Kill mplayer as well
pkill mplayer

# .pls files could be mp3 or aac, possibly some other thing, and mocp can't play aac.
# Prefer mocp over mplayer, since it can be backgrounded with "q", and shows the playlist.
# Work-around, using mplayer to diagnose the stream type.
# .pls files unfortunately are just URLs, so we can't grep for mp3 etc...
if echo "$@" | grep -i .pls > /dev/null ; then
    heading
    head-col
    echo -e "\n  Checking playlist type, please wait..."
    radiocheck=$(mktemp)
    # We don't need to hear mplayer trying to find "mpeg"...
    mplayer -ao null -playlist "$@" > "$radiocheck" 2>&1 & mplayerpid=$!
    # Give it a few moments...
    sleep 4
    kill $mplayerpid

    # See if it turned up...
    if grep mpeg $radiocheck > /dev/null ; then
        rm "$radiocheck"
        mocp "$@"
	outahere
    else
        rm "$radiocheck"
        playinx_top
        play_as_playlist "$@"
    fi    
    
elif echo "$@" | grep -i .m3u > /dev/null 2>&1 ; then
    # See if it contains mp3 - if so, use mocp, since it shows the playlist...
    # For some reason, mocp refuses to play m3u if they are .ogg - no idea why...
    # m3u are files, and contain the suffixes/extensions, so we can grep for them...
    if grep -i .mp3 "$@" > /dev/null 2>&1 ; then
        mocp "$@"
	outahere
    else
    # Well, looks like we need mplayer then...
        playinx_top
	if grep -i .ogg "$@" > /dev/null 2>&1 ; then
	    fig-col
	    echo -e "  Ogg Format, Vorbis Audio detected...\n"
	    info-col
	    play_as_playlist "$@"
	fi	
    fi
    # OK, if it's not Ogg it's probably some kind of evil proprietary thing ;)
elif echo "$@" | egrep -i "(.asx|.ram|.aspx)" > /dev/null 2>&1 ; then
    playinx_top    
    play_as_playlist "$@"
fi

# Hmm, then it looks as if it probably isn't a playlist file as such...

playinx_top

if echo "$@" | grep -i .mp3 > /dev/null 2>&1 ; then
    mocp "$@"
    outahere
elif echo "$@" | egrep -i "(.flac|.wma|.mms|.wmv|.ogg|.avi|.aac|.mov|.flv|.mpg|.mpeg|.asf|rtsp://|mms://|.rm)" > /dev/null 2>&1 ; then
    mplayer "$@" 2> /dev/null | stream_data
    outahere
elif cat "$@" 2> /dev/null | grep -i "http://" > /dev/null 2>&1 ; then # File containing URLs
    play_as_playlist "$@"
elif printf "   " && echo "$@" | grep http:// | egrep "(.m3u|.pls)" ; then # Sigh... they aren't really playlists... but...
    play_as_playlist "$@"
fi

# We tried, honest...
echo -e "\n  I don't know what to do with \""$@"\", sorry...\n"
sleep 5
outahere
