#!/usr/bin/perl -wT

BEGIN {
 {
    if( $ENV{"SCRIPT_FILENAME"} && $ENV{"SCRIPT_FILENAME"} =~ /^(.+)\/[^\/]+$/ ) {
        chdir $1;
    }
    unshift @INC, '.';
    require 'setlib.cfg';
}

}
#
# TWiki Collaboration Platform, http://TWiki.org/
#
# Copyright (C) 1999-2003 Peter Thoeny, peter@thoeny.com
#
# Based on parts of Ward Cunninghams original Wiki and JosWiki.
# Copyright (C) 1998 Markus Peter - SPiN GmbH (warpi@spin.de)
# Some changes by Dave Harris (drh@bhresearch.co.uk) incorporated
# Copyright (C) 1999-2003 Peter Thoeny, peter@thoeny.com
#
# 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.ai.mit.edu/copyleft/gpl.html 

    # Set default current working directory (needed for mod_perl)
    # Set library paths in @INC at compile time

use CGI::Carp qw(fatalsToBrowser);
use CGI;
use TWiki;


sub init_config { 
	my $config = shift;

$query= new CGI;

}# END of init_config

&main();

sub main
{  
   my %configHash; 
   my $config = \%configHash;
   my $twikiUri = $ENV{"SCRIPT_FILENAME"};
   $twikiUri =~ s!/+!/!g ; # remove multiple'/' 
   my @twikiUriItem = split ( '/' , $twikiUri ) ;
   $$config{'fileConfig'} = $ENV{"SERVER_NAME"} . "Port" . $ENV{"SERVER_PORT"} . $twikiUriItem[$#twikiUriItem - 2 ];

	 &TWiki::init_config($config) ;
	 &init_config($config) ;

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

    ( $topic, $webName, $dummy1, $dummy2, $dataDir ) = 
        &TWiki::initialize($config, $thePathInfo, $theRemoteUser, $theTopic, $theUrl, $query );
    $dummy1 = "";  # to suppress warning
    $dummy2 = "";  # to suppress warning

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

    my $skin = $query->param( "skin" ) || TWiki::Prefs::getPreferencesValue($config, "SKIN" );
    my $text = TWiki::Store::readTemplate($config, "changes", $skin );
    my $changes= TWiki::Store::readFile($config, "$dataDir/$webName/.changes" );
   
    my @bar = ();
    my $foo = "";
    my %exclude = ();
    my $summary = "";
    my $time = "";
    my $frev = "";

    $text = &TWiki::handleCommonTags($config, $text, $topic );
    $text = &TWiki::getRenderedVersion($config, $text );
    $text =~ s/\%META{.*?}\%//go;  # remove %META{"parent"}%

    my $before = "";
    my $after = "";
    ( $before, $text, $after) = split( /%REPEAT%/, $text );
    &TWiki::writeHeader($config, $query );
    $before =~ s/( ?) *<\/?(nop|noautolink)\/?>\n?/$1/gois;  # remove <nop> and <noautolink> tags
    print $before;

    foreach( reverse split( /\n/, $changes ) ) {
        @bar = split( /\t/ );
        if( ( ! %exclude ) || ( ! $exclude{ $bar[0] } ) ) {
            next unless TWiki::Store::topicExists($config, $webName, $bar[0] );
            $foo = $text;
            $foo =~ s/%TOPICNAME%/$bar[0]/go;
            $wikiuser = &TWiki::userToWikiName($config, $bar[1] );
            $foo =~ s/%AUTHOR%/$wikiuser/go;
            $foo =~ s/%LOCKED%//go;
            $time = &TWiki::formatTime($config, $bar[2] );
            $frev = "";
            if( $bar[3] ) {
                if( $bar[3] > 1 ) {
                    $frev = "r1.$bar[3]";
                } else {
                    $frev = "<b>NEW</b>";
                }
            }
            $foo =~ s/%TIME%/$time/go;
            $foo =~ s/%REVISION%/$frev/go;
            $foo = &TWiki::getRenderedVersion($config, $foo );

            $summary = &TWiki::Store::readFileHead($config, "$dataDir\/$webName\/$bar[0].txt", 16 );
            $summary = &TWiki::makeTopicSummary($config, $summary, $bar[0], $webName );
            $foo =~ s/%TEXTHEAD%/$summary/go;
            $foo =~ s/( ?) *<\/?(nop|noautolink)\/?>\n?/$1/gois;   # remove <nop> and <noautolink> tags
            print $foo;
            $exclude{ $bar[0] } = "1";
        }
    }

    if( $$config{'TWiki::doLogTopicChanges'} ) {
        # write log entry
        &TWiki::Store::writeLog($config, "changes", $webName, "" );
    }

    $after =~ s/( ?) *<\/?(nop|noautolink)\/?>\n?/$1/gois;   # remove <nop> and <noautolink> tags
    print $after;
}
