#! /usr/bin/env perl
#
# Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2004 Wind River Systems Inc.
# Copyright (C) 2004-2007 Peter Thoeny, peter@thoeny.org
# and TWiki Contributors.
#
# For licensing info read LICENSE file in the Foswiki root.
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 General Public License for more details, published at
# http://www.gnu.org/copyleft/gpl.html
#
# As per the GPL, removal of this notice is prohibited.

# Mail notification script. You must add the Foswiki bin dir to the
# search path for this script, so it can find the rest of Foswiki e.g.
# perl -I /usr/local/foswiki/bin /usr/local/foswiki/tools/mailnotify

use strict;

use File::Spec;

BEGIN {
    my ( $volume, $binDir, $action ) = File::Spec->splitpath(__FILE__);
    $binDir .= '/' if $binDir;
    my $setlib = File::Spec->catpath( $volume, "$binDir../bin", 'setlib.cfg' );
    @INC = ( '.', grep { $_ ne '.' } @INC ) unless $binDir;
    require $setlib;
}

my %options = (
    verbose => 1,
    news    => 1,
    changes => 1,
    reset   => 1,
    mail    => 1
);

my @webs   = ();
my @exwebs = ();

# Called from the command line
foreach my $arg (@ARGV) {
    if ( $arg eq "-q" ) {
        $options{verbose} = 0;
    }
    elsif ( $arg eq "-nonews" ) {
        $options{news} = 0;
    }
    elsif ( $arg eq "-nochanges" ) {
        $options{changes} = 0;
    }
    elsif ( $arg eq "-noreset" ) {
        $options{reset} = 0;
    }
    elsif ( $arg eq '-nomail' ) {
        $options{mail} = 0;
    }
    elsif ( $arg =~ m/^-(.*)/ ) {
        push( @exwebs, $1 );
    }
    else {
        push( @webs, $arg );
    }
}

use Foswiki();
use Foswiki::Contrib::MailerContrib ();

new Foswiki('admin');

Foswiki::Contrib::MailerContrib::mailNotify( \@webs, \@exwebs, %options );
