Xchat::register("Cyth's Amarok Now Playing Script","1.0","","");
Xchat::hook_command("nowplay","getinfo");
Xchat::hook_command("sendnow","send");

sub getinfo {
		my $gotitle = gettitle();
		my $goartist = getartist();
		my $gonet = getnet();
		my $gotime = gettime();
		my $goalbum = getalbum();
		my $govers = getversion();
		if ($goartist == -1){
			Xchat::command("say \00310$govers not playing");
		} else {
			Xchat::command("say \00310Now Playing on \00305$gonet \00307[:.\00305 $goartist \00307- \00305$gotitle \00307.:] $goalbum $gotime \00310with $govers!");
		}
		return Xchat::EAT_NONE;
}

sub getnet {
	my $net;
	open(NET, "uname -n|");
	$net = <NET>;
	chomp $net;
	close NET;
	return $net;	
}

sub gettitle {
	my $title;
	open(TITLE, "dcop amarok player title|");
	$title = <TITLE>;
	chomp $title;
	if ($title eq ""){
		$title = -1;
	}
	close TITLE;
	return $title;
}

sub getartist {
	my $artist;
	open(ARTIST, "dcop amarok player artist|");
	$artist = <ARTIST>;
	chomp $artist;
	if ($artist eq ""){
		$artist = -1;
	}
	close ARTIST;
	return $artist;
}

sub getalbum {
	my $album;
	open(ALBUM, "dcop amarok player album|");
	$album = <ALBUM>;
	chomp $album;
	close ALBUM;
	if ($album eq ""){
		return "";
	} else {
		return "\00307[\00310Album: \00305".$album."\00307]";
	}
}

sub gettime {
	my $tottime;
	my $curtime;
	my $bitrate;
	open(TOTTIME, "dcop amarok player totalTime|");
	open(CURTIME, "dcop amarok player currentTime|");
	open(BITRATE, "dcop amarok player bitrate|");
	$bitrate = <BITRATE>;
	$tottime = <TOTTIME>;
	$curtime = <CURTIME>;
	chomp $tottime;
	chomp $curtime;
	chomp $bitrate;
	close BITRATE;
	close TOTTIME;
	close CURTIME;
	if($tottime eq ""){
		return "";
	} else {
		return "\00307[\00305".$curtime."\00307/\00305".$tottime." \00307@ \00305".$bitrate."\00307]";
	}
}

sub getversion {
	my $version;
	my $check;
	open(VERSION, "amarok --version|");
	while($check = <VERSION>){
		$check =~ /^(amaroK:.*)$/i;
		$version = $1;
		chomp $version
	}
	close VERSION;
	return $version;
}
sub send {
	my $text = $_[1][1];
	open(FILE, "dcop amarok player path|");
	my $path = <FILE>;
	chomp $path;
	close FILE;
	if($path eq ""){
		Xchat::print("no file to send");
	} else {
		Xchat::command("DCC SEND $text \"$path\"");
	}
	return Xchat::EAT_NONE;
}