!/bin/bash
#
#  Name : scgen v0.1a
#  Date : 3-17-2003
#  Author: echto
#
#  Usage: type scgen at your shell prompt and then do what ever you want with the .sc files.
#         Be sure to `chmod 770 scgen` and make it executable.
#
#  This script is work in progress so use at your own risk.  A few live streams don't get
#  created and I'll fix that later.  You might see the error below during execution.
# 
#  mv: when moving multiple files, last argument must be a directory
#
#  It'll get fixed later this week.
#
#  Heres a quick and dirty script to generate .sc files from the top 20 shoutcast streams
#
#  What you do with it from here is up to you.  This script could easily be modified to
#  move the .sc files to a network drive thats mapped in the xbmp config.xml file.
#
#  Add the script to your cron jobs with 30 minute intervals for a reasonably up to date list.
#
#  To do:
#
#	1. Improve .sc filenames
#	2. Include the ability to add to cron
#
# - Frankly its finals week and I don't have a lot of time to play now.
#
#
# Revision:
#
#  0.1a - fixed teh URL in the .sc file
#

wget -O index.html http://shoutcast.com
grep \.pls index.html | awk '!match($5,"Lucky") {print $5}' | sed 's/href="/shoutcast.com/g' | sed 's/"><img//g'>streamlist

get_streams () {
	while read url; do
	wget $url
	done
}

create_sc_files () {
	while read tempdata; do
	
	cat $tempdata | awk ' match($1,"File1") { print $0 }' \
	| sed 's/File1=http:/shoutcast:/' > $tempdata.temp
	
	mv -f $tempdata.temp `cat $tempdata | awk ' match($1,"Title1") { print $0 }' | sed 's/Title.*) //'\
		   | sed 's/\ //g' | sed 's/\///g' | awk '{print $1}'`.sc

	cat $tempdata | awk ' match($1,"Title1") { print $0 }' | sed 's/Title.*) //'
	
	done
}

# lets grab the latest top 20 streams
wget -O index.html http://shoutcast.com

# lets pull out the playlists
grep \.pls index.html | awk '!match($5,"Lucky") {print $5}' | sed 's/href="/shoutcast.com/g' | sed 's/"><img//g'>streamlist

# lets get the stream data
get_streams < streamlist

# lets create out .sc files
ls shoutcast-playlist.pls\?rn* | create_sc_files

# delete all the crap we don't need anymore
rm -f shoutcast-playlist.pls\?rn=*
rm -f index.html

