#!/usr/bin/perl -wT
#
# 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 library paths in @INC, at compile time
BEGIN { unshift @INC, '.'; require 'setlib.cfg'; }

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

$query= new CGI;

&main();

sub main
{
    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( $thePathInfo, $theRemoteUser, $theTopic, $theUrl, $query );
    $dummy1 = "";  # to suppress warning
    $dummy2 = "";  # to suppress warning

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

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

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

    my $before = "";
    my $after = "";
    ( $before, $text, $after) = split( /%REPEAT%/, $text );
    &TWiki::writeHeader( $query );
    print $before;

    foreach( reverse split( /\n/, $changes ) ) {
        @bar = split( /\t/ );
        if( ( ! %exclude ) || ( ! $exclude{ $bar[0] } ) ) {
            next unless TWiki::Store::topicExists( $webName, $bar[0] );
            $foo = $text;
            $foo =~ s/%TOPICNAME%/$bar[0]/go;
            $wikiuser = &TWiki::userToWikiName( $bar[1] );
            $foo =~ s/%AUTHOR%/$wikiuser/go;
            $foo =~ s/%LOCKED%//go;
            $time = &TWiki::formatGmTime( $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( $foo );

            $summary = &TWiki::Store::readFileHead( "$dataDir\/$webName\/$bar[0].txt", 16 );
            $summary = &TWiki::makeTopicSummary( $summary, $bar[0], $webName );
            $foo =~ s/%TEXTHEAD%/$summary/go;

            print $foo;
            $exclude{ $bar[0] } = "1";
        }
    }

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

    print $after;
}
