#!/usr/bin/perl -w
#
# TWiki Collaboration Platform, http://TWiki.org/
#
# Copyright (C) 2004 Wind River Systems Inc.
#
# For licensing info read license.txt file in the TWiki 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

# Must be run from twiki/bin, as it uses setlib.cfg

BEGIN {
    # Set default current working directory (needed for mod_perl)
    if( $ENV{"SCRIPT_FILENAME"} &&
		$ENV{"SCRIPT_FILENAME"} =~ /^(.+)\/[^\/]+$/ ) {
	  chdir $1;
	} elsif ( $0 =~ /^(.*)\/.*?$/ ) {
	  chdir $1;
	}
    # Set library paths in @INC, at compile time
    unshift @INC, '.';
    require 'setlib.cfg';
}

my $sendmail = 0;
my $verbose = 1;

my @webs = ();

if( $ENV{'DOCUMENT_ROOT'} ) {
    # Called from browser.
    # CODE_SMELL: Is this really a reliable way of detecting
    # a browser environment?
    use CGI;
    $query = new CGI;
    $sendmail = $query->param( 'sendmail' ) || 0;
    $verbose = $query->param( 'verbose' ) || 1;
    my $webz = $query->param( 'webs' ) || "";
    @webs = split( /[, ]+/, $webz);

    print "Content-type: text/plain\n\n";
} else {
    # Called from the command line
    foreach $arg ( @ARGV ) {
        if( $arg eq "-m" ) {
            $sendmail = 1;
        } elsif ( $arg eq "-q" ) {
            $verbose = 0;
        } else {
            push( @webs, $arg );
        }
    }
}

use TWiki::Contrib::Mailer;
#TWiki::Contrib::Mailer::mailNotify( $sendmail, $verbose, \@webs );
my $report = TWiki::Contrib::Mailer::mailNotify( \@webs, undef, $verbose, $sendmail );
print $report."\n";
