#!/usr/bin/perl
use warnings;
use strict;

use RTPG;
use Data::Dump qw(dump);

my $h = new RTPG(url=> $ENV{SOCKET} || '/srv/cloudstore/var/torrent/socket');

if ( @ARGV ) {
	print "# ",dump(@ARGV);
	my $out = $h->rpc_command(@ARGV);
	print dump($out);
	exit;
}

my $sfmt;
my @keys;
while (<DATA>) {
	chomp;
	next if /^#/;
	my ($name,$fmt) = split(/\t+/,$_,2);
	$sfmt .= $fmt . ' ';
	push @keys, $name;
}
$sfmt =~ s/\s+$/\n/;

# arrayref and error (standard version)
my ($tlist, $error)=$h->torrents_list;
warn dump( $tlist, $error ) if $ENV{DEBUG};

printf $sfmt, @keys;
foreach my $t ( @$tlist ) {
	printf $sfmt, map { $t->{$_} } @keys;
}

__DATA__
#hash			%-40s
percent			%5s
status			%-11s
size_bytes		%12d
#base_filename	%s
loaded_file		%s
