#!/usr/bin/perl -w

# TWiki extension : automatic mailnotify subscription
#
# Copyright (C) 2004 ILOG S.A.
#
# 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.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

# Author: Nicolas Duboc <nduboc@ilog.fr>
# Creation date : 2004-01-02
# $Id: monitor,v 1.5 2004/05/05 15:36:34 nduboc Exp $
#

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 CGI;
use CGI::Carp qw(fatalsToBrowser);

use TWiki;
use TWiki::NotifSettings;

use strict;

&main();

sub main
{
    my $query = new CGI;

    my $thePathInfo = $query->path_info(); 
    my $theRemoteUser = $query->remote_user();
    my $theTopic = $query->param( 'topic' ) || "";
    my $theUrl = $query->url;

    my( $topic, $webName, $dummy, $userName ) = 
	&TWiki::initialize( $thePathInfo, $theRemoteUser, $theTopic, $theUrl, $query );
    $dummy = "";  # to suppress warning


    my $text = "";
    my $meta = "";
    my $extra = "";
    my $wikiUserName = &TWiki::userToWikiName( $userName );
    my $mainWebPattern = qr/(?:$TWiki::mainWebname|%MAINWEB%)/;
    my $simpleUserName = $wikiUserName;
    $simpleUserName =~ s/$mainWebPattern.//;
    my $notifyTopic = $TWiki::notifyTopicname;

    if( ! &TWiki::Store::webExists( $webName ) ) {
        my $url = &TWiki::getOopsUrl( $webName, $topic, "oopsnoweb" );
        TWiki::redirect( $query, $url );
        return;
    }


    if (TWiki::NotifSettings::isMonitoringTopic($simpleUserName,
						$webName, $topic)) 
    {
	my $url = &TWiki::getOopsUrl( $webName, $topic, "oopsnotifyngnotdone",
				      $notifyTopic);
	TWiki::redirect( $query, $url );
	return;
    } else {
	if ( ! &TWiki::Access::checkAccessPermission("view", $simpleUserName,
						     0, $topic, $webName) )
	{
	    my $url = &TWiki::getOopsUrl( $webName, $topic,
					  "oopsnotifyngdenied",
					  $notifyTopic);
	    TWiki::redirect( $query, $url );
	    return;
	}

	my $status = TWiki::NotifSettings::monitorTopic($simpleUserName,
						       $webName, $topic);
	if ( $status eq "1" ) {
	    # OK ! Done.
	    my $url = &TWiki::getOopsUrl( $webName, $topic, "oopsnotifyngok",
					  $notifyTopic);
	    TWiki::redirect( $query, $url );
	    return;
	} elsif ( $status eq "0" ) {
	    # we should not get here, but ...
	    my $url = &TWiki::getOopsUrl( $webName, $topic,
					  "oopsnotifyngnotdone",
					  $notifyTopic);
	    
	    TWiki::redirect( $query, $url );
	    return;
	} else {
	    # error on save
	    my $url = &TWiki::getOopsUrl( $webName, $notifyTopic,
					  "oopssaveerr", $status );
	    TWiki::redirect( $query, $url );
	    return;
	}
    }


}
