#!/usr/bin/perl -wT

################################
# 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
#
#
#
#This program is written in Persistent Systems Pvt Ltd (http://www.persistent.co.in)
#Author : sopan_shewale@persistent.co.in
#Date   : 28/May/2005  : logout name is given to do_logout script. 
#       :              : the redirection of topic belonging to web different from Main happens properly now.
#
#The basic idea is taken from SessionPlugins's logon script.
#The script modifies the  ENV{REMOTE_USER} value  after authentication
######################


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

my $query = new CGI;

my $thePathInfo = $query->path_info(); 

## sopan added the code ..
$ENV{REMOTE_USER} ="guest";

###

my $theRemoteUser = $query->remote_user();


#my $theTopic = $query->param( 'topic' );
my $theUrl = $query->url;
my $redurl = $query->param('redurl');

   $redurl =~ s/\/logon\//\/view\//;
my ($trash, $webandtopic) = split /bin\/view\//, $redurl;
my ($Web, $theTopic) = split /\//, $webandtopic;


my( $topic, $webName, $scriptUrlPath, $userName ) = 
  TWiki::initialize( $thePathInfo, $theRemoteUser,
                     $theTopic, $redurl, $query );

&TWiki::Plugins::SessionPlugin::logout_session();

my $url = &TWiki::getViewUrl($webName, $topic );


TWiki::redirect($query, $url);

