#!/usr/bin/perl

=head1 NAME

transmission-client.pl - Alternative to transmission-remote

=head1 SYNOPSIS

 transmission-client.pl list;
 transmission-client.pl session;
 transmission-client.pl session $key $value;
 transmission-client.pl stats;
 transmission-client.pl add /path/to/file.torrent
 transmission-client.pl remove 1 [,2,3...]

=head1 DESCRIPTION

This is an example application for L<Transmission::Client>

=cut

use strict;
use warnings;
use lib qw(lib);
use Transmission::Client;

my $action = shift @ARGV or _help();

my %a = ( autodie => 1 );
@a{ qw(username password) } = split(/:/, $ENV{TR_AUTH}) if $ENV{TR_AUTH};

my $tc = Transmission::Client->new( %a );

if($action eq 'list') {
    printf "%3s %-34s %4s %4s %5s %5s\n", 'id', 'name', 'lcrs', 'sdrs', 'rate', 'eta';
    print "-" x 79, "\n";
    for my $torrent ($tc->read_torrents) {
        printf "%3i %-34s %4s %4s %5s %5s\n",
            $torrent->id,
            substr($torrent->name, 0, 34),
            _peers($torrent->leechers),
            _peers($torrent->seeders),
            _rate($torrent->rate_download),
            _eta($torrent->eta),
            ;
    }
}
elsif($action eq 'session') {
    if(my $set = shift @ARGV) {
        $tc->session->$set(shift @ARGV);
        $tc->session->${ \"clear_$set" };
        printf "%s: %s\n", $set, $tc->session->$set;
        print $tc->error;
    }
    else {
        my $res = $tc->session->read_all;
        for my $key (sort keys %$res) {
            printf "%-30s %s\n", $key, $res->{$key};
        }
    }
}
elsif($action eq 'stats') {
    my $res = $tc->session->stats->read_all;
    for my $key (sort keys %$res) {
        printf "%-30s %s\n", $key, $res->{$key};
    }
}

elsif($action eq 'add') {
	$tc->add(
		filename => $ARGV[0],
		download_dir => '/rsync1/s1/torrent/download',
	);
}

elsif($action eq 'remove') {
	$tc->remove(
		ids => @ARGV
	);
}

else {
    _help();
}

print "\n";

#==============================================================================
sub _peers {
    my $n = shift;

    if($n < 0) {
        return 'na';
    }
    elsif($n < 9999) {
        return $n;
    }
    else {
        return '++';
    }
}
sub _rate {
    my $kbps = shift;

    if($kbps < 0) {
        return '0';
    }
    elsif($kbps < 1000) {
        return $kbps;
    }
    elsif($kbps < 1e6) {
        return int($kbps / 1e3) . 'k';
    }
    elsif($kbps < 1e6) {
        return int($kbps / 1e6) . 'M';
    }
    else {
        return '++';
    }
}

sub _eta {
    my $sec = shift;

    if($sec < 0) {
        return 'inf';
    }
    elsif($sec < 60) {
        return $sec . "s";
    }
    elsif($sec < 3600) {
        return int($sec / 6) / 10 . "m";
    }
    elsif($sec < 86400) {
        return int($sec / 360) / 10 . "h";
    }
    else {
        return '>1d';
    }
}

sub _help {
    exec perldoc => -tT => $0;
}

=head1 LICENSE

This library is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.

=head1 AUTHOR

Jan Henning Thorsen

=cut

exit;
