#!/usr/bin/perl -w
#*************************************************************************
#
#   OpenOffice.org - a multi-platform office productivity suite
#
#   $RCSfile: rlogdemo,v $
#
#   $Revision: 1.2 $
#
#   last change: $Author: vg $ $Date: 2007/08/27 13:36:12 $
#
#   The Contents of this file are made available subject to
#   the terms of GNU Lesser General Public License Version 2.1.
#
#
#     GNU Lesser General Public License Version 2.1
#     =============================================
#     Copyright 2005 by Sun Microsystems, Inc.
#     901 San Antonio Road, Palo Alto, CA 94303, USA
#
#     This library is free software; you can redistribute it and/or
#     modify it under the terms of the GNU Lesser General Public
#     License version 2.1, as published by the Free Software Foundation.
#
#     This library is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#     Lesser General Public License for more details.
#
#     You should have received a copy of the GNU Lesser General Public
#     License along with this library; if not, write to the Free Software
#     Foundation, Inc., 59 Temple Place, Suite 330, Boston,
#     MA  02111-1307  USA
#
#*************************************************************************

use lib ('../lib');

use PCVSLib;
use Getopt::Std;

getopt('-do');

if ( !$opt_d || $#ARGV < 0) {
    print STDERR "usage: rlogdemo [-o options] < -d cvsroot > < module|file >\n";
    exit(1);
}

# Create root object
my $root = PCVSLib::Root->new($opt_d);

# Read scrambled CVS password from $HOME/.cvspass.
my $credentials = PCVSLib::Credentials->new();
my $password = $credentials->get_password($root);

# Create a connection to CVS server.
my $connection = PCVSLib::Connection->new($root, $password);

# Open the connection and insert loging handle
my $log_handle = IO::File->new(">log");
my $io_handle = $connection->open();
$connection->io_handle(PCVSLib::LogHandle->new($io_handle, $log_handle));

# Create client which takes the connection
my $client = PCVSLib::Client->new($connection);

# Create event handler.
my $event_handler = PCVSLib::EventHandler->new();

# Create a listener and register it with the event handler
my $listener = CVSListener->new();
$event_handler->add_listener($listener);

# Create a command, fill in options and transfer it to client for
# execution.
my $command = PCVSLib::RLogCommand->new($event_handler);
$command->file_list([@ARGV]);
if (defined ($opt_o) ) {
    $command->options([$opt_o]);
}
$client->execute_command($command);

#  Remove listener form event handler and close connection
$event_handler->remove_listener($listener);
$connection->close();

# Query the listener if the operation was succesful
if ( $listener->is_success() ) {
    foreach ( @{$listener->get_log()} ) {
        print "$_\n";
    }
    exit(0);
}
else {
    print "rlog command failed!\n";
    exit(1);
}

# Simple minded listener for listening on message events etc. Every client listener
# should implement method 'notify()' and listen at least on 'PCVSLib::TerminatedEvent'.
package CVSListener;

sub new
{
    my $invocant = shift;
    my $class = ref($invocant) || $invocant;
    my $self = {};
    $self->{is_success_} = 0;
    $self->{log_}        = ();
    bless ($self, $class);
    return $self;
}

sub is_success
{
    my $self = shift;
    return $self->{is_success_};
}

sub get_log
{
    my $self = shift;
    return $self->{log_};
}

sub notify
{
    my $self  = shift;
    my $event = shift;

    if ( $event->isa(PCVSLib::MessageEvent) ) {
        push(@{$self->{log_}}, $event->get_message());
    }
    if ( $event->isa(PCVSLib::TerminatedEvent) ) {
        $self->{is_success_} = $event->is_success();
    }
}

# vim: set ts=4 shiftwidth=4 expandtab syntax=perl:
