#!/usr/bin/perl -w

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell

# $Id: svnnotify 720 2004-10-07 06:17:22Z theory $

use strict;
use Getopt::Long;

##############################################################################
# Process the command-line arguments.
##############################################################################

# Enable bundling and, at the same time, case-sensitive matching of single
# character options.
Getopt::Long::Configure ("bundling");

# Get options.
my %opts;
GetOptions(
    "repos-path|p=s"     => \$opts{repos_path},
    "revision|r=s"       => \$opts{revision},
    "to|t=s"             => \$opts{to},
    "to-regex-map|x=s%"  => \$opts{to_regex_map},
    "from|f=s"           => \$opts{from},
    "user-domain|D=s"    => \$opts{user_domain},
    "svnlook|l=s"        => \$opts{svnlook},
    "sendmail|s=s"       => \$opts{sendmail},
    "charset|c=s"        => \$opts{charset},
    "with-diff|d"        => \$opts{with_diff},
    "attach-diff|a"      => \$opts{attach_diff},
    "reply-to|R=s"       => \$opts{reply_to},
    "subject-prefix|P=s" => \$opts{subject_prefix},
    "subject-cx|C"       => \$opts{subject_cx},
    "max-sub-length|i=i" => \$opts{max_sub_length},
    "format|F=s"         => \$opts{format},
    "handler|H=s"        => \$opts{handler},
    "viewcvs-url|U=s"    => \$opts{viewcvs_url},
    "verbose|V+"         => \$opts{verbose},
    "help|h"             => \$opts{help},
    "man|m"              => \$opts{man},
    "version|v"          => \$opts{version},
) or require Pod::Usage && Pod::Usage::pod2usage(2);

# Handle version, help, man.
if (delete $opts{version}) {
    require SVN::Notify;
    print "svnnotify ", SVN::Notify->VERSION, $/;
    exit;
}
require Pod::Usage && Pod::Usage::pod2usage(1) if delete $opts{help};
require Pod::Usage && Pod::Usage::pod2usage(-exitstatus => 0, -verbose => 2)
  if delete$opts{man};

# Handle required options.
die qq{Missing required "--repos-path" parameter\n}
  unless $opts{repos_path};
die qq{Missing required "--revision" parameter\n}
  unless $opts{revision};
die qq{Missing required "--to" or "--to_regex_map" parameter\n}
  unless $opts{to} || $opts{to_regex_map};

# Backwards compatability to 2.00. What was I thinking?
if (my $format = delete $opts{format}) {
    warn "the --format option is deprecated. Use --handler instead.\n";
    $opts{handler} = 'HTML' unless $opts{handler} || $format ne 'html';
}

require SVN::Notify;
my $notifier = SVN::Notify->new(%opts);
$notifier->prepare;
$notifier->execute;

1;
__END__

=head1 Name

svnnotify - Subversion activity notification

=head1 Synopsis

=begin comment

Fake-out Pod::Usage

=head1 SYNOPSIS

=end comment

  svnnotify --repos-path "$1" --revision "$2" [options]

=head1 Options

  -p --repos-path PATH         Path to the Subversion repository. Required.
  -r --revision REVISION       Commit revision number. Required.
  -t --to ADDRESS              The notification destination email address.
                               Required unless --to-regex-map.
  -x --to-regex-map TO=REGEX   A mapping between a destination email address
                               and a regular expresion to match against the
                               directories affected by the commit. Required
                               unless --to.
  -f --from ADDRESS            Email address to use in the From header.
  -D --user-domain DOMAIN      Domain name to append to the username to
                               complete the email address in the From header.
  -l --svnlook SVNLOOK         Location of the svnlook executable. Defaults
                               to "/usr/local/bin/svnlook".
  -s --sendmail SENDMAIL       Location of the sendmail executable. Defaults
                               to "/usr/sbin/sendmail".
  -c --charset CHARSET         The character set of the log message and the
                               repository files.
  -d --with-diff               Include the diff in the message.
  -a --attach-diff             Attach the diff to the message.
  -R --reply-to ADDRESS        Address for use in the Reply-To header.
  -P --subject-prefix PREFIX   String to prepend to the subject.
  -C --subject-cx              Include the context of the commit in the
                               subject.
  -i --max-sub-length LENGTH   Maximum size of the subject line.
  -H --handler HANDLER         The notification handler, such as "HTML".
  -U --viewcvs-url URL         Include links to specified ViewCVS URL.
  -V --verbose                 Incremental verbose mode.
  -h --help                    Print this usage statement and exit.
  -m --man                     Print the complete documentation and exit.
  -v --version                 Print the version number and exit.

=head1 See Also

See L<SVN::Notify|SVN::Notify> for the complete documentation.

=head1 Author

David Wheeler <david@kineticode.com>

=head1 Copyright and License

Copyright (c) 2004 Kineticode, Inc. All Rights Reserved.

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

=cut
