#!/usr/bin/perl
# Back 2 The Roots Radio Helper Script
# (C) 2004, Michal Januszewski <spock@gentoo.org>
#
# The guys at BTRR came up with a great idea - a mod radio. No more 
# bandwidth-sucking audio streams, no more need to download the individual
# mod files, search for them or download them in a big n-MB packages. 
#
# Unfortunately this mod-radio works nice only with WinAmp and DeliPlayer. 
# These two on the other hand, don't work very well with the OS we all use.
# As you surely know, you can play mod files with XMMS (with proper plugins
# installed). Since BTRR is a Amiga community, it makes sense to use a great 
# piece of software called UADE (Unix Amiga Deliplayer Emulator) to play the 
# tunes. The only problem with UADE is that it can't download mod files from 
# the net.
#
# This is where this little script comes in. Just set the $root variable up,
# choose a channel and let it download the files and generate a nice m3u 
# playlist for you. 
#
# Feel free to add more channels to the @radios array below. If you do so,
# please drop me a patch. Right now I'm having a good time listening to these 
# 3 stations listed below and my laziness stops me from putting more of them,
# so I'm sorry, but that's left up to you ;)

use IO::File;
use POSIX qw(tmpnam);

$root = '/home/spock/temp/b2rr/';

my @radios = (
{ 'name' => 'Games @ Back2Roots', 'type' => 'm3u', 'files' => "$root/b2-games", 'url' => 'http://www.back2roots.org/Play/Games%20%40%20Back2Roots%2C1/Infk/WinAmp.m3u' },
{ 'name' => 'Games @ Back2Roots', 'type' => 'dpl', 'files' => "$root/b2-games", 'url' => 'http://www.back2roots.org/Play/Games%20%40%20Back2Roots%2C1/Infk/DeliPlayer.dplist' },

{ 'name' => 'Chips @ Back2Roots', 'type' => 'm3u', 'files' => "$root/b2-chips", 'url' => 'http://www.back2roots.org/Play/Chips%20%40%20Back2Roots%2C1/Infk/WinAmp.m3u' },
{ 'name' => 'Chips @ Back2Roots', 'type' => 'dpl', 'files' => "$root/b2-chips", 'url' => 'http://www.back2roots.org/Play/Chips%20%40%20Back2Roots%2C1/Infk/DeliPlayer.dplist' },

{ 'name' => 'Scene @ Back2Roots', 'type' => 'm3u', 'files' => "$root/b2-scene", 'url' => 'http://www.back2roots.org/Play/Scene%20%40%20Back2Roots%2C1/Infk/WinAmp.m3u' },
{ 'name' => 'Scene @ Back2Roots', 'type' => 'dpl', 'files' => "$root/b2-scene", 'url' => 'http://www.back2roots.org/Play/Scene%20%40%20Back2Roots%2C1/Infk/DeliPlayer.dplist' }
);
    
if ($#ARGV < 1) {
	print stderr "Not enough parameters!\n";
	print stderr "Usage: ./b2rr.pl <radio_num> <out_m3u>\n";
	print stderr "\nAvailable radio channels:\n";
	$i = 1;
	foreach $radio (@radios) {
		print stderr "$i. ".$$radio{'name'}." (".$$radio{'type'}.")\n";
		$i++;
	}
	exit 1;
}

do { $tmpname = tmpnam() } until $tfh = IO::File->new($tmpname, O_RDWR|O_CREAT|O_EXCL);
END { unlink($tmpname) or die "Couldn't unlink $tmpname : $!" }

print "Downloading playlist.. ($tmpname)\n";
$num = $ARGV[0] - 1;

$url = $radios[$num]{'url'};
`wget --quiet '$url' -O $tmpname --user-agent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040707 Firefox/0.9.2"`;

if ($radios[$num]{'type'} eq "m3u") {
	parse_m3u($tmpname, $ARGV[1]);
} else {
	parse_dpl($tmpname, $ARGV[1]);
}

exit 0;

sub parse_m3u 
{
	$in = shift;
	$out = shift;

	open(FOUT, ">$out");
	open(FIN, "<$in");
	
	while ($line = <FIN>)
	{
		if ($line =~ /^#/) {
			print $line;
			next;
		}

		$line =~ m#.*/([^/]+)#;
		$fn = $1;
	
		print stderr "Downloading $fn";
		`wget --quiet -c $line -O $radios[$num]{'files'}/$fn`;
	
		print FOUT $fn
	}

	close(FIN);
	close(FOUT);
}

# Poor man's DeliPlayer playlist files parsing. This is definitely not the 
# right way to go about it, but it works (TM). If you have the specs for the 
# dplist file format, or happen to know where to find them - drop me a line, 
# maybe I'll fix this ;) 
sub parse_dpl
{
	$in = shift;
	$out = shift;

	my $fn, $title, $dotitl;
		
	@lines = `strings $in`;
	open(FOUT, ">$out");
	print FOUT "#EXTM3U\n";
	
	foreach $line (@lines) 
	{
		if ($line =~ /DELIPLAYLIST/) {
			next;
		}
		
		chop $line;
	
		if ($dotitl) {
			print FOUT "#EXTINF:1,$line\n";
			print FOUT "$fn\n";
			$dotitl = 0;
		}
	
		if ($line =~ m#http://.*[^/]$#) {
		
			$line =~ m#.*/([^/]+)#;
			$fn = $1;
	 		
			print stderr "Downloading $fn\n";
			`wget --quiet -c $line -O $radios[$num]{'files'}/$fn`;
			$dotitl = 1;
		}
	}
	
	close (FOUT);
}

