#!/bin/bash

SCDIR='/mnt/data/shoutcast'
INSTALLDIR='/home/echto/scgen'
updatetime=30

# ---- DELETE OR COMMENT OUT THE FOLLOWING LINES AFTER YOU HAVE DEFINED THE TWO DIRECTORIES ABOVE.
echo " "
echo "YOU MUST EDIT THIS FILE AND DEFINE YOUR SC FILE DIR AND YOUR INSTALL DIR"
echo " "
exit 1
#------- END 


get_streams () {
	while read url bitrate; do

	wget -q `echo $url | awk ' address="shoutcast.com" substr($1,7,100) {print address}' | sed 's/"//g'`
	
        # lets swap out http for shoutcast

        SOURCE=`echo $url | awk '{print substr($0,13,100)}' | sed 's/"//g' | sed 's/?/\\?/g' | sed 's/=/\\=/g'`
        DEST=`cat $SOURCE | awk ' match($1,"Title1=") { print substr($0,1,100) }' | sed 's/Title.*) //' |\
              sed 's/\ //g' | sed 's/\///g' |sed 's/[()*,&!?:.\`]//g' | awk '{print $1}'`.$bitrate.sc
				
	cat $SOURCE | awk ' match($1,"File1=") { print $0 }' | sed 's/File1=http:/shoutcast:/' >\
	`echo $url | awk '{print substr($0,13,100)}' | sed 's/"//g' | sed 's/?/\\?/g' | sed 's/=/\\=/g'`.temp

	SOURCE=`echo $url | awk '{print substr($0,13,100)}' | sed 's/"//g' | sed 's/?/\\?/g' | sed 's/=/\\=/g'`
	DEST=$bitrate"K".`cat $SOURCE | awk ' match($1,"Title1=") { print substr($0,1,100) }' | sed 's/Title.*) //' |\
		sed 's/\ //g' | sed 's/\///g' |sed 's/[()*,&!?:.\`]//g' | awk '{print $1}'`.sc

echo $DEST

       mv -f $SOURCE.temp $DEST

	done
}

magic () {

	sed -n -f $INSTALLDIR/part1.scr index.html > part1
	sed -f $INSTALLDIR/part2.scr part1 > part2

	rm -f part1

	cat part2 | awk ' match($0,"playlist.pls") {print substr($5,1,length($5)-5)}' > url
	cat part2 | awk '!match($0,"playlist.pls") {print substr($8,17,length($8)-28) }' > bitrate

	paste url bitrate > streamlist

	# lets get the stream data
	get_streams < streamlist

}

addcron () {

	crontab -l | sed '/scgen/d' > ~/_cron.tmp

	echo "*/$updatetime * * * * $INSTALLDIR/scgen > /dev/null 2>&1" >> ~/_cron.tmp

	crontab ~/_cron.tmp
	rm ~/_cron.tmp

	echo "scgen added to cron and will be executed every 30 minutes"
	exit 1
}

delcron () {
	crontab -l | sed '/scgen/d' > ~/_cron.tmp
	crontab ~/_cron.tmp
	rm ~/_cron.tmp
	echo "scgen removed from cron."
	exit 1
}

############################################################################################

case $1 in
"help")echo " "
       echo " "
       echo "ScGen - Shoutcast .sc file generator"
       echo "------------------------------------"
       echo "Usage: `basename $0` <option>"
       echo " "
       echo "deep     Generates 100, if they exists, .sc files for each genre."
       echo "cron     Installs as a scgen to run as a cron job every 30 minutes."
       echo "nocron   Uninstall scgen from cron."
       echo "help     This screen."
       echo " "
       echo " "
       exit 1;;
"cron") addcron;;
"nocron") delcron;;
"deep") deep=1;;
     *) echo "Starting generator.";;
esac

for a in TopTen Alternative Classical Comedy Country Dance \
		Funk Jazz Metal Mixed Pop Rap RnB\
		Rock Talk Techno 80s 70s World
	    do

# make sure the directories exists
		if [ ! -d $SCDIR/$a ]; then
			echo " "
			echo "ERROR!!!!!!!!!!!!!!"
			echo " "
			echo "You are missing the $SCDIR/$a directory!"
			echo " "
			echo "You must create ALL required directories!"
			echo " "
			exit 1
		fi

# lets grab the stream list
		echo "ScGenerating $a"
		if [ $deep ]; then
			echo "......generating DEEP .sc list. THIS WILL TAKE SOME TIME!"
			echo $a $deep
			wget -q -O index.html http://shoutcast.com/directory/index.phtml?sgenre=$a\&numresult=100
		else
			wget -q -O index.html http://shoutcast.com/directory/index.phtml?sgenre=$a
		fi
		
		magic

# copy the sc file to the dest dir
		rm -f $SCDIR/$a/*.sc
		mv *.sc $SCDIR/$a

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

done
