#!/usr/bin/perl					

use IO::Socket;
my @Playlist;
my @SongName;
my @SongPath;
my @Tracklist;
my $port       = $ARGV[0];
my $Band       = $ARGV[1];
my $Album      = $ARGV[2];

my $ServerAddr = "SERVERADDR";
my $list       = "DOCROOT/playlist/playlist.$port";
open PLAYLIST, "$list"; 
@Playlist = <PLAYLIST>;
chomp @Playlist;
	

##STREAM STARTS##
$socket = IO::Socket::INET->new(LocalPort => $port,
				Listen    => 10,
				proto     => 'tcp',
				Reuse     => 1) or die "Couldn't Build Socket : $! \n";	

 
$hocket = $socket->accept;
print $hocket "ICY 200 OK\n";
print $hocket "icy-notice1:<BR>This stream requires <a href=\"http://www.winamp.com\">Winamp</a><BR>";
print $hocket "icy-notice2:Perl Based Streaming Server - sheller@marlboro.edu\n";
print $hocket "icy-name:$Album by $Band\n";
print $hocket "icy-genre:GENRE\n";
print $hocket "icy-url:URL";
print $hocket "Content-Type:audio/mpeg\n";
print $hocket "icy-pub:0\n";
print $hocket "icy-br24\n\n"; 

foreach ( @Playlist )
	{
	open (MP3, $_);
	while (read MP3, $buffer, 1024)
		{
		if ( $buffer ne " " )
			{
			print $hocket $buffer;
			$buffer = " ";
			}

		}
	close MP3;
}

	unlink $list;
	unlink "DOCROOT/playlist/$port.m3u";	
	$socket->close();
