#!/usr/bin/perl
use strict;
use SVN::Client;

my $file       = $ARGV[0];
my $rangestart = $ARGV[1];
my $rangeend   = $ARGV[2];

my @logentries;

my $ctx = new SVN::Client(
        auth => [SVN::Client::get_simple_provider(),
                 SVN::Client::get_simple_prompt_provider(\&simple_prompt,2),
                 SVN::Client::get_username_provider()]
        );

$ctx->log($file, 0, "HEAD", 0,0, \&log_traverse);

foreach my $log_ref (@logentries) {
  next if (!$log_ref);
  my %log = %{$log_ref};
  next if (!$log{keep});
  print "Author:   $log{author}\n";
  print "Date:     $log{date}\n";
  print "Revision: $log{revision}\n";
  print "---------------------------------------\n";
  print "$log{message}\n";
  print "---------------------------------------\n";

}
sub log_traverse {
  my $changed_paths = shift;
  my $revision      = shift;
  my $author        = shift;
  my $date          = shift;
  my $message       = shift;
  my $pool          = shift;
  $logentries[$revision]{message}  = $message;
  $logentries[$revision]{author}   = $author;
  $logentries[$revision]{revision} = $revision;
  $logentries[$revision]{date}     = $date;
  $ctx->blame($file, "0", "HEAD",  \&blame_traverse);
}

sub blame_traverse {
  my $line_no  = shift;
  my $revision = shift;
  my $author   = shift;
  my $date     = shift;
  my $line     = shift;
  my $pool     = shift;
  return if ($logentries[$revision]{keep});
  if (($line_no > $rangestart) and ($line_no < $rangeend)) {
    $logentries[$revision]{keep} = 1;
  }
}

sub simple_prompt {
  my $cred = shift;
  my $realm = shift;
  my $default_username = shift;
  my $may_save = shift;
  my $pool = shift;
  print "Enter authentication info for realm: $realm\n";
  print "Username: ";
  my $username = <>;
  chomp($username);
  $cred->username($username);
  print "Password: ";
  my $password = <>;
  chomp($password);
  $cred->password($password);
}


