#!/usr/bin/perl -w
#
# TWiki Collaboration Platform, http://TWiki.org/
#
# Copyright (C) 1999-2003 Peter Thoeny, peter@thoeny.com
# Copyright (C) 2004 ILOG S.A.
#
# 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

# Colas (http://colas.nahaboo.net)
# modifications from mailnotify script from Dec 2001 release:
# - email is now optional, is fetched from the user homepage
# - webs not beginning by a capital letter are ignored ( _default, ...)
# - no mail is sent to TWikiGuest
# - if user is a group, recurses through its members

# Nicolas Duboc <nduboc@ilog.fr>
# - used the original code to implement this mailnotify NG
# - added usage doc

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

use TWiki;
use TWiki::Net;
use TWiki::MailNotify;

&main();

sub usage {
    my $name = shift;
    print "TWiki email notification script.\n";
    print "usage: $0 [-h | --help] [-q] [-t] <webname> ...\n";
    print "    -h | --help : print this usage message\n";
    print "    -q : turn off all terminal messages (usefull for batch mode)\n";
    print "    -t : test mode, no mail sent (debug purpose)\n";
    print "    <webnames> : list of webs to run mailnotify on\n";
    print "                 (defaults to all webs of the Wiki)\n";
}

sub main
{
    my $debug=1;
    my $test=0;
    my $keepmails=0;
    my @weblist=();
    my $dataDir = &TWiki::getDataDir();

    while ( $_=shift (@ARGV) )
    {
	if (/^-h/ || /^--help/) { usage(); return;}
	if (/^-q$/) { $debug=0;      next; }
	if (/^-t$/) { $test=1;       next; }
	if (-d "$dataDir/$_")  {push ( @weblist, $_); next; }

    }

    &TWiki::basicInitialize();

    if ( $#weblist <0 ) 
    {
      opendir( DIR, "$dataDir" ) or die "could not open $dataDir";
      foreach ( grep !/^\.\.?$/, readdir DIR ) 
      {
        if ( /^_/ ) { next; }
        if (-d "$dataDir/$_")  { push ( @weblist, $_); next; }
      }
      closedir DIR;
    }

    $debug && print "TWiki mail notification for webs : ", join (" ", @weblist), "\n";
    $debug && print "- to suppress all normal output: mailnotify -q\n";
    $debug && print "- to test only (and send NO mail) : mailnotify -t\n";


    foreach $web ( @weblist ) 
    {
        # Only process webs with normal names, i.e. not starting with '_'
        if( &TWiki::isWebName($web) ) 
        {
             &TWiki::MailNotify::processWeb( $web , $debug, $test);

             # remove obsolete .lock files
             &TWiki::Store::removeObsoleteTopicLocks( $web );
        }
        else
        {
           $debug && print "Skipped non wiki web : $web\n";
        }
    }
    $debug && print "End TWiki mail notification\n";
}

