#!/usr/bin/perl
#
# iceplay
# - MP3 File Streamer
#
# Copyright (c) 1999 Jack Moffitt, Barath Raghavan
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

use strict;
use IO::Socket;
use Time::HiRes qw( sleep gettimeofday tv_interval );

my ($server, $port, $password, $playlist, $webfile, $bitrate, $public);
my ($name, $genre, $url);

$port = 8001;
$public = 0;
$bitrate = 128;
$playlist = "playlist";
$webfile = "webfile";

while (scalar(@ARGV) > 0) {
	$_ = shift @ARGV;
	if (/^-([A-Za-z])$/) {
		SWITCH: {
		$1 =~ /s/	&& do {
					$server = shift @ARGV;
					last SWITCH;
				};
		$1 =~ /P/	&& do {
					$port = shift @ARGV;
					last SWITCH;
				};
		$1 =~ /p/	&& do {
					$password = shift @ARGV;
					last SWITCH;
				};
		$1 =~ /l/	&& do {
					$playlist = shift @ARGV;
					last SWITCH;
				};
		$1 =~ /w/	&& do {
					$webfile = shift @ARGV;
					last SWITCH;
				};
		$1 =~ /n/	&& do {
					$name = shift @ARGV;
					last SWITCH;
				};
		$1 =~ /g/	&& do {
					$genre = shift @ARGV;
					last SWITCH;
				};
		$1 =~ /u/	&& do {
					$url = shift @ARGV;
					last SWITCH;
				};
		$1 =~ /d/	&& do {
					$public = shift @ARGV;
					last SWITCH;
				};
		$1 =~ /b/	&& do {
					$bitrate = shift @ARGV;
					last SWITCH;
				};
		$1 =~ /h/	&& do {
					&usage;
					exit(0);
				};

		&usage;
		}
	} else {
		&usage;
	}
}

unless ($server && $port && $password && $name && $genre && $url && defined($public) && $bitrate) {
	&usage;
	exit(1);
}

my $sock = IO::Socket::INET->new(Proto => "tcp",
				 PeerAddr => $server,
				 PeerPort => $port);
$sock->autoflush(1);

print $sock "$password\n";
my $val = <$sock>;
if (!($val =~ /OK/)) {
	die "Wrong password\n";
}

print $sock "icy-name: $name\n";
print $sock "icy-genre: $genre\n";
print $sock "icy-url: $url\n";
print $sock "icy-pub: $public\n";
print $sock "icy-br: $bitrate\n";
print $sock "\n";

print "Connected to server...\n";

open(PLAYLIST, $playlist);
my @songs = <PLAYLIST>;
close(PLAYLIST);

my $num_songs = @songs;

print "Playlist loaded...$num_songs total songs...\n";

# Figure out the sleep time and data size
my $buffsize = 4000;
my $timetosleep = $buffsize / ($bitrate * 1000 / 8);


while (1) {
  my $song = $songs[rand $num_songs];
  open(SONG, $song);
  $song =~ /.*\/(.*) - (.*)\.mp3/;
  my $artist = $1;
  my $title = $2;
  open(WEBFILE, ">$webfile");
  print WEBFILE "$title by $artist";
  close(WEBFILE);

  my ($read, $buff, $sleeptime);
  do {
    $read = read(SONG, $buff, $buffsize);
    print $sock $buff;
    $sleeptime = $timetosleep * $read / $buffsize;
    sleep($sleeptime);
  } while ($read == $buffsize);
  close(SONG);
}

close($sock);

exit(0);

sub usage {
print<<"END";
i c e p l a y - Version 0.5
 ` ` ` ` ` `
Usage:
iceplay -s <server> -p <password> -n <name> -g <genre> -u <url> [-P <port>] [-b <bitrate>] [-d <0/1>] [-l <playlist>] [-w <webfile>]

        Options explained (default in parenthesis):
	-s: the server to connect to
        -p: the password to access the server
	-n: the name of the stream
	-g: the genre of the stream
	-u: the homepage url of the stream
	-P: the port to connect on (8001)
	-b: the bitrate of the stream (128)
	-d: is the server public? (0)
	-l: playlist file (playlist)
	-w: web data file (webfile)

END
}
