#!/usr/local/bin/perl -w
###########################################
# rotate-client
# Mike Schilli, 2012 (m@perlmeister.com)
###########################################
use strict;
use Sysadm::Install qw(slurp blurt);
use Thrift;
use Thrift::BinaryProtocol;
use Thrift::Socket;
use Thrift::BufferedTransport;

use lib 'gen-perl';
use image_process::Rotator;

my $socket =
  Thrift::Socket->new( "localhost", 9001 );

my $transport =
  Thrift::BufferedTransport->new( $socket,
  1024, 1024 );

my $protocol =
  Thrift::BinaryProtocol->new($transport);
my $client =
  image_process::RotatorClient->new(
  $protocol);

my ($image) = @ARGV;
die "usage: $0 image" if !defined $image;

eval {
  $transport->open();

  my $image_data = slurp $image;

  my $action =
    image_process::Rotation->new();
  $action->image($image_data);
  $action->angle(90);

  my $rotated_image_data =
    $client->rotate($action);

  blurt $rotated_image_data,
    "rotated-$image";

  $transport->close();
};

if ($@ =~ m/image_process/ and 
    exists $@->{why}) {
  die $@->{why};
} elsif( $@ ) {
  die $@;
}
