#!/bin/bash
# playphc.sh - play the latest Prairie Home Companion Show

# prepare
mkdir phctemp
cd phctemp

# download index file
wget http://www.prairiehome.org/

# extract link to show's page

LINKLINE=`grep Listen\ to\ the index.html`
FIELD=1

extract_playlist_linkname(){
 CANDIDATE=`echo $LINKLINE | cut -d\" -f $FIELD`
 echo $CANDIDATE | grep performances
  if [ "$?" = "1" ]; then
   let FIELD=$FIELD+1 && extract_playlist_linkname
  fi 
}

extract_ramfile_url(){
 rm ~/phctemp/*
 wget http://www.prairiehome.org$CANDIDATE
 wget -O playlist.html http://www.prairiehome.org`grep Listen\ to\ the\ whole\ show index.html | cut -d\" -f 2`
 RAMFILE_URL=`grep audio\ begin playlist.html | cut -d\" -f4`
}

extract_playlist_linkname
extract_ramfile_url
exec realplay $RAMFILE_URL &

# cleanup
cd - && rm -rf phctemp
