#!/usr/bin/perl

use strict;
use FindBin qw($RealBin);
use lib "$RealBin/../lib";

use Getopt::Long;
use App::Milter::Limit;
use App::Milter::Limit::Util;
use App::Milter::Limit::PidFile;
use Pod::Usage;

my %opts = (
    debug  => 0,
    config => '/etc/mail/milter-limit.conf'
);

GetOptions(\%opts, 'debug|d', 'config|c=s', 'help|h');
pod2usage(1) if $opts{help};

main();

sub main {
    my $config = App::Milter::Limit::Config->instance($opts{config});

    if ($opts{debug}) {
        $config->global->{debug} = 1;
    }

    App::Milter::Limit::Util::daemonize() unless $opts{debug};

    die "already running" if App::Milter::Limit::PidFile->running;

    $0 = $$config{name} || 'milter-limit';

    my $milter = App::Milter::Limit->instance($config->global->{driver});

    $milter->register;
    $milter->main;
}

__END__

=head1 NAME

milter-limit - Rate limiting sendmail milter

=head1 SYNOPSIS

milter-limit [options]

 Options:
   --debug                  debug mode
   --config filename        configuration file name

=head1 OPTIONS

=over 8

=item B<--debug>

Enable debug mode.  Currently this just means the the program will not daemonize

=item B<--config>  default: /etc/mail/milter-limit.conf

Location of the milter-limit configuration file.

=back

=head1 DESCRIPTION

B<milter-limit> is a sendmail milter for limiting the rate at which messages
can be sent.  Currently the only supported mode of operation is to limit the
number of messages by SMTP envelope sender.  More limiting options may be added
later if demand exists for such features.

=head1 AUTHOR

Michael Schout <mschout@gkg.net>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2009 Michael Schout.  All rights reserved.

This program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself. The full text of this license can be found in
the LICENSE file included with this program.

=cut
