#!/usr/bin/perl -wT -I.
#
# TWiki WikiClone (see $wikiversion in wiki.pm for version)
#
# 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 Peter Thoeny, peter.thoeny@takefive.com , 
# TakeFive Software Inc.
#
# 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 

use CGI;
use wiki;


print "Content-type: text/html\n\n";

$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 ) = 
        &wiki::initialize( $thePathInfo, $theRemoteUser, $theTopic, $theUrl );
    $dummy1 = "";  # to suppress warning
    $dummy2 = "";  # to suppress warning

    if( ! &wiki::webExists( $webName ) ) {
        my $tmpl= &wiki::readTemplate( "noweb" );
        $tmpl = &wiki::handleCommonTags( $tmpl, $topic );
        print $tmpl;
        return;
    }

    my $text = &wiki::readTemplate( "changes" );
    my $changes= &wiki::readFile( "$dataDir/$webName/.changes" );
   
    my %exclude;
    my $head;

    $text = &wiki::handleCommonTags( $text, $topic );

    ( $before, $text, $after) = split( /%REPEAT%/, $text);
    print $before;

    foreach( reverse split( /\n/, $changes ) ) {
        @bar = split( /\t/ );
        if( ( !defined( %exclude ) ) || ( ! $exclude{ $bar[0] } ) ) {
            $foo = $text;
            $foo =~ s/%TOPICNAME%/$bar[0]/go;
            $wikiuser = &wiki::userToWikiName( $bar[1] );
            $foo =~ s/%AUTHOR%/$wikiuser/go;
            $time = &wiki::formatGmTime( $bar[2] );
            $foo =~ s/%TIME%/$time/go;
            $foo = &wiki::getRenderedVersion( $foo );

            $head = &wiki::readFileHead( "$dataDir\/$webName\/$bar[0].txt", 12 );
            $head =~ s/<[^>]*>//go;         # remove all HTML tags
            $head =~ s/[\*\|=_]/ /go;       # remove all Wiki formatting
            $head =~ s/%INCLUDE[^%]*%/ /go; # remove server side includes
            $head =~ s/%SEARCH[^%]*%/ /go;  # remove inline search
            $head =~ s/\n/ /go;
            $head = &wiki::handleCommonTags( $head, $bar[0] );
            $head =~ s/(.{162})([a-zA-Z0-9]*)(.*?)$/$1$2 \.\.\./go;
            $foo =~ s/%TEXTHEAD%/$head/go;

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

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

    print $after;
}
