use Irssi;
use strict;
use vars qw/$VERSION %IRSSI/;

$VERSION = '1.0.2';
%IRSSI = {
    authors     => 'Torbjörn \'Azoff\' Svensson',
    contact     => 'azoff@se.linux.org',
    name        => 'xmms.pl',
    description => 'Display XMMS/InfoPipe data using /np [arg]',
    license     => 'Public domain',
    changed     => '09-01-2004'
};

Irssi::settings_add_str('misc', 'xmms_infopipe_fifo', '/tmp/xmms-info');
Irssi::settings_add_str('misc', 'xmms_host', 'burk');
Irssi::command_bind('np', 'cmd_get_xmms_playing');

sub cmd_get_xmms_playing {
	my ($args, undef) = @_;
	my ($fifo) = Irssi::settings_get_str('xmms_infopipe_fifo');
	my ($host) = Irssi::settings_get_str('xmms_host');
	#open F, "ssh $host cat $fifo|" or Irssi::print('error connecting');  # recive info.
	open F, "ssh $host ~/bin/mp3info.sh|" or Irssi::print('error connecting');  # recive info.
	my $data = {};

	while(<F>){
		chomp;
		my ($key,$val) = split /: /, $_;
		$key =~ s/ /_/g;
		$data->{$key} = $val;
	}
	close F;
	
	my $outstr="";
	if ($data->{"Currently_playing"} > 0) {
		my ($tmin,$tsec) = (split /:/, $data->{"Time"});
		my $time = $tsec + $tmin * 60;
	#	my $th = int($tmin/60);
	#	$data->{"Position"} = "$th:$tmin:$tsec";
		my ($pmin,$psec) = (split /:/, $data->{"Position"});
		my $pos = $psec + $pmin * 60;
		my $proc = ( $time == 0) ? 100 : int $pos * 100 / $time;
		my $mode = ( $data->{"Channels"} eq "2") ? "stereo" : "mono";

		$outstr = (($args ne "") and not $args=~/^\W/) ? "$args " : "is listening to ";

	#	$outstr .= $data->{"Currently_playing"} ."/";
	#	$outstr .= $data->{"Tunes_in_playlist"} .". ";

		$outstr .= $data->{"Title"} ." [";
		$outstr .= $data->{"Position"}."/";
		$outstr .= (($time == 0) ? $data->{"Position"} : $data->{"Time"} ) ." (";
		$outstr .= $proc."\%)] (";
		$outstr .= $data->{"Current_bitrate"} / 1000 ."kbps ";
		$outstr .= $mode.")";
#Irssi::print($outstr);
	} else {
		$outstr = "is not playing anything right now.";
	}
	
	#Irssi::print('Unable to delete file..') unless unlink $fifo;

	Irssi::active_win()->command("/me $outstr");
}


