#!c:/cygwin/bin/perl

################################
# 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   : 8/Feb/2005
#
#The basic idea is taken from SessionPlugins's logon script.
#The script modifies the  ENV{REMOTE_USER} value  after authentication
######################

sub BEGIN { $ENV{PTKDB_STOP_TAG_COLOR} = "yellow" } 
use CGI::Carp qw(fatalsToBrowser);
use CGI;


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::Plugins::SessionPlugin;
$query = new CGI();


main();

sub main
{
    my $thePathInfo = $query->path_info(); 
    
 
    $ENV{REMOTE_USER} ="guest";
    my $theRemoteUser = $query->remote_user();

    my $theTopic = $query->param( 'redurl' );

    my @topiclisting = split/logon/, $theTopic;## The logon word is part of
                                               ## %SESSIONLOGONURL% variable

    foreach (@topiclisting) { $theTopic = $_;}
    $theTopic =~ s/\/+//; ## Remove all "/" from begining of string.
    
    my $theUrl = $query->url;
   $query->delete_all(); 
 ( $topic, $web ) = 
	&TWiki::initialize( $thePathInfo, $theRemoteUser, $theTopic, $theUrl, $query );

   $TWiki::Plugins::SessionPlugin::session->param
                  ( 'VALIDATION', '' );

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

    $url .= ( '?' . $query->query_string() ) if $query->query_string();
    &TWiki::redirect( $query, $url );
}

