#!/usr/bin/perl -w
use strict;
use lib qw{.} ;
use SVN::Core '0.32';
use SVN::Pusher::CmdLine;
use Getopt::Long ;


# Autoflush
$| = 1;

=head1 NAME

svn-pusher - command line interface for propagating Subversion changesets. 

=head1 SYNOPSIS

    % svn-pusher push --revision=4:6 http://hosta/path http://hostb/path

=head1 DESCRIPTION

F<svn-pusher> propagates changesets from one Subversion repository
to another.

=head1 COMMANDS

=over

=item version (or --version)

Displays the version number.

=item push [options] B<srcurl> B<desturl> 

Invoke the push of B<srcurl> to B<desturl>

Possible options:

=over

=item -m --message=<text>

Use <text> for B<every> commit that is done during push

=item -r --revision=<from>:<to>

Push only changes between (including) the two given revision.
Revision can also be C<HEAD> which means the newest revision in
the repository.

=item --savedate

Save svn:date property. It requires that a pre-revprop-change exist on
the destination repository.

=item --verbatim

Do not store uuid and timestamp in log message.

=item -v --verbose

Print extra information.

=back

Example:

  svn-pusher push -r 4:6 -m 'New Release' https://svn.example.com/repos https://svn2.example.com/release

=back

=cut

# --------------------------------------------------------------------------

sub do_help {
    require Pod::Text;
    my $parser = Pod::Text->new (sentence => 0, width => 78);
    $parser->parse_from_file ($0, '-' );
}

# --------------------------------------------------------------------------

sub do_version {
    print <<"EOF";
svn-pusher using SVN::Pusher version $SVN::Pusher::VERSION.
EOF

    exit(0);
}

# --------------------------------------------------------------------------

sub opt_push {
    return qw{
	message|m:s
	revision|r:s
	savedate
	verbatim
	verbose|v
    };
}


sub do_push 
{
    if (@_ < 2)
    {
        die "Not enough arguments provided; try '$0 help' for more info\n";
    }

    my ($options, $source, $target) = @_;

    my %revs ;
    if ($options -> {revision})
    {
        if ($options->{revision} !~ /^(\d+):(\d+|HEAD)$/)
        {
            die "Incorrect revision format - there should be two revisions separated by a colon (:).";
        }
        my ($start, $end) = ($1,$2);
        %revs = (startrev => $start, endrev => $end) ;
    }

    my $m = SVN::Pusher::CmdLine->new(
        target    => $target,
        source    => $source,
        %revs,
        logmsg    => $options -> {message},
        savedate  => $options -> {savedate},
        verbatim  => $options -> {verbatim},
        verbose   => $options -> {verbose},
        );

    if ($m->init () > 0)
    {
        $m->run;
    }
}


# --------------------------------------------------------------------------

my $cmd = shift || 'help';

if ($cmd eq "--version")
{
    $cmd = "version";
}

my $cmdsub = "do_$cmd" ;
my $cmdopt = "opt_$cmd" ;

die "Command not recognized. Try $0 help\n" unless main->can($cmdsub);

my %options ;
if (my $cmdopt_ref = main -> can ($cmdopt))
{
    eval { Getopt::Long::Configure ('bundling') } ;
    $@ = "" ;
    my $ret = GetOptions (\%options, $cmdopt_ref->()) ;
}

main->can($cmdsub)->(\%options, @ARGV);


=head1 AUTHORS

Shlomi Fish E<lt>shlomif@iglu.org.ilE<gt>

(based on SVN::Push by Gerald Richter E<lt>richter@dev.ecos.deE<gt>)

=head1 CREDITS

A lot of ideas and code were taken from SVN::Mirror which was written by
Chia-liang Kao E<lt>clkao@clkao.orgE<gt>

=head1 COPYRIGHT

Copyright 2004 by Gerald Richter E<lt>richter@dev.ecos.deE<gt>

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

See L<http://www.perl.com/perl/misc/Artistic.html>

=cut

1;
