#!/usr/bin/perl

use XML::Parser;
use RPC::XML::Client;
use WWW::Curl::easy;
use Ogg::Vorbis;
use HTTP::Date;


sub parsetag {
	local $xmltext = shift;
	local $identifier = shift;  
	local $extent = shift;  
	local $encoding = shift;

	local @xmlarr = split(/\n/,$xmltext);
	local $p = XML::Parser->new(Style => 'Stream');
	local $doc = $p->parse($xmltext);

	local $ctype="";
	local $cattr="";
	local $ctag="";
	local %visit;



    sub StartDocument {}
    sub EndTag { 
        my $exp = shift;
        my $type = shift;

		if($visit{$type}==0) {
			if($type eq "dc:format:medium") {
				$xmlarr[$exp->current_line-1] =~ s/></>reboot</;
			}
			if($type eq "dc:format:extent") {
				$xmlarr[$exp->current_line-1] =~ s/></>$extent</;
			}
			if($type eq "dc:format:encoding") {
				$xmlarr[$exp->current_line-1] =~ s/></>$encoding</;
			}
			if($type eq "dc:identifier") {
				$xmlarr[$exp->current_line-1] =~ s/></>$identifier</;
			}
			if($type eq "dc:publisher") {
				$xmlarr[$exp->current_line-1] =~ s/></>100tage.reboot.fm</;
			}
		}
		$visit{$type}=0;
		$ctype = $cattr = $ctag = "";
	}
    sub StartTag {
        my $exp = shift;
        my $type = shift;
        my %attr = %_;
        my $tag = $_;

        $ctype = $type;
        $cattr = $attr{'role'};
        $ctag = $tag;
		$visit{$ctype} = 0;
    }

    sub Text {
        my $exp = shift;
        my $type = shift;
        my %attr = %_;
        my $tag = $_;
        local $otag = $tag;

		if($ctype ne "") {
			$visit{$ctype}++;
		}
		if($ctype eq "dc:date:issued") {
#			$xmlarr[$exp->current_line-1] =~ s/$tag/$issued/;
			$ntag = $tag;
			$ntag =~ s/\ /T/;
			$ntag .= "Z";
			$issued = str2time($ntag);
			$xmlarr[$exp->current_line-1] =~ s/$tag/$ntag/;
		}
		if($ctype eq "dc:type") {
			$tag = $s =~ s/\*/\\\*/g;
			$xmlarr[$exp->current_line-1] =~ s/$tag/\/$tag/;
			$xmlarr[$exp->current_line-1] =~ s/\\\*/\*/g;
		}
	}
    $xmltext="";
    for($i=0;$i<=$#xmlarr;$i++) {
        $xmltext .= "$xmlarr[$i]\n";
    }
    return($xmltext,$issued);
}



sub chunk {
	my ($data,$pointer)=@_;
	${$pointer}.=$data;
	return length($data)
}

#opendir(D,"/tmp/test");
opendir(D,"/data/recording/shows2archive/");
while($_=readdir(D)) {
	if($_ =~ /\.ogg$/) {
		my $fn = $_;
#		open(F, "< /tmp/test/$fn");

		open(F, "< /data/recording/shows2archive/$fn");
		$ogg = Ogg::Vorbis->new;
		$err = $ogg->open(F);
		if(!$err) {
			$info = $ogg->info;
			%comments = %{$ogg->comment};
			$encoding = "ogg/vorbis ". $info->bitrate_nominal ." bps, ";
			$encoding .= $info->channels ." channels, ";
			$encoding .= $info->rate ." frequency";
			$extent = sprintf("%d",$ogg->time_total(-1));
		} else {
			undef(%comments);
		}
		close(F);
		@n = split(/-/,$fn);
		$comments{uid} = $n[$#n];
		$comments{uid} =~ s/\.ogg$//;

		if($comments{uid} > 0) {
			my $body="";
			my $headers="";
#			print "$comments{title}\n$comments{uid}\n$comments{date}\n";
#			$uid = (split(/-/,$comments{uid}))[1];

			$uid = $comments{uid};

			my $curl = WWW::Curl::easy->new();
			$curl->setopt(CURLOPT_URL,"http://www.reboot.fm/programs/rdf/emission?emission_id=$uid");
			$curl->setopt(CURLOPT_HEADERFUNCTION, \&chunk );
			$curl->setopt(CURLOPT_WRITEFUNCTION, \&chunk );
			$curl->setopt(CURLOPT_FILE, \$body);
			$curl->perform();
			($ne,$issued) = parsetag($body,$_,$extent,$encoding);
print "---------------------------------------------------------\n";
			print $ne;
			my $client = new RPC::XML::Client('http://localhost:9501/');
print "------ ('oma.receive_dc_set',TA,\"playout system\",$fn,$issued) -----\n";
			my $req = RPC::XML::request->new('oma.receive_dc_set',$ne,"playout system",$fn,$issued);
			$client->send_request($req);
		}
	}
}
