#!/usr/bin/perl -wTI.
#
# TWiki WikiClone (see wiki.pm for $wikiversion and other info)
#
# 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.com
#
# 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.org/copyleft/gpl.html

use CGI;
use wiki;

$query= new CGI;

##### for debug only: Remove next 2 comments (but redirect does not work)
#open(STDERR,'>&STDOUT'); # redirect error to browser
#$| = 1;                  # no buffering

&main();

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

    ( $topic, $webName, $scriptUrlPath, $userName ) = 
	&wiki::initialize( $thePathInfo, $theRemoteUser, $theTopic, $theUrl );

    my $tmpl = "";
    my $text = "";
    my $rev = $query->param( "rev" );
    my $maxrev = 1;
    my $extra = "";
    my $wikiUserName = &wiki::userToWikiName( $userName );

    if( ! &wiki::webExists( $webName ) ) {
	$tmpl= &wiki::readTemplate( "noweb" );
	$tmpl = &wiki::handleCommonTags( $tmpl, $topic );
        print "Content-type: text/html\n\n";
	print $tmpl;
	return;
    }

    my $unlock = $query->param( "unlock" ) || "";
    if( $unlock eq "on" ) {
        # unlock topic, user cancelled out of edit
        &wiki::lockTopic( $topic, "on" );
    }

    $tmpl= &wiki::readTemplate( "view", $topic );
    if( ! $tmpl ) {
        print "Content-type: text/html\n\n";
        print "<html><body>";
        print "<h1>TWiki Installation Error</h1>";
        print "Template directory $wiki::templateDir not found.<p>";
        print "Check the \$templateDir variable in wikicfg.pm.";
        print "</body></html>";
	return;
    }

    my $topicExists = &wiki::topicExists( $webName, $topic );
    if( $topicExists ) {
	$maxrev = &wiki::getRevisionNumber( $topic );
        #$maxrev =~ s/1\.//go;  # cut major
	if( $rev ) {
            #$rev =~ s/1\.//go;  # cut major
	    if( $rev < 1 )       { $rev = 1; }
            if( $rev > $maxrev ) { $rev = $maxrev; }
	    $text= &wiki::readVersion( $topic, $rev );
            $extra .= "r1.$rev";
	} else {
	    $text= &wiki::readTopic( $topic );
	    $rev= $maxrev;
	}
    } else {
	$rev = 1;
	if( &wiki::isWikiName( $topic ) ) {
	    $text= &wiki::readTemplate( "notext" );
	} else {
	    $text= &wiki::readTemplate( "notwiki" );
	}
        $extra .= " (not exist)";
    }

# commented out because not tested and not finished (protect also %INCLUDE% and search)
#    # check access permission
#    if( ! &wiki::checkAccessPermission( "view", $wikiUserName, $text, $topic, $webName ) ) {
#        my $url = &wiki::getOopsUrl( $webName, $topic, "oopsaccessview" );
#        print $query->redirect( $url );
#        return;
#    }

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

    if( $wiki::doLogTopicView ) {
        # write log entry
        &wiki::writeLog( "view", "$webName.$topic", $extra );
    }

    if( $rev < $maxrev ) {
        # disable edit of previous revisions
        $tmpl =~ s/%EDITTOPIC%/Edit/go;
        $tmpl =~ s/%REVTITLE%/\(r1.$rev\)/go;
    } else {
        if( $topicExists ) {
            $tmpl =~ s/%EDITTOPIC%/<A href=\"$scriptUrlPath\/edit%SCRIPTSUFFIX%\/%WEB%\/%TOPIC%\"><B>Edit<\/B><\/A>/go; 
            # remove the NOINDEX meta tag
            $tmpl =~ s/<META NAME="ROBOTS"[^>]*>//goi;
        } else {
            $tmpl =~ s/%EDITTOPIC%/<A href=\"$scriptUrlPath\/edit%SCRIPTSUFFIX%\/%WEB%\/%TOPIC%\"><B>Create<\/B><\/A>/go; 
        }
        $tmpl =~ s/%REVTITLE%//go;
    }

    my $i = $maxrev;
    my $j = $maxrev;
    my $revisions = "";
    my $breakRev = 0;
    if( ( $wiki::numberOfRevisions > 0 ) && ( $wiki::numberOfRevisions < $maxrev ) ) {
        $breakRev = $maxrev - $wiki::numberOfRevisions + 1;
    }
    while( $i > 0 ) {
        if( $i eq $rev) {
            $revisions = "$revisions | r1.$i";
        } else {
            $revisions = "$revisions | <A href=\"$scriptUrlPath/view%SCRIPTSUFFIX%/%WEB%/%TOPIC%?rev=1.$i\">r1.$i</A>";
        }
        if( $i != 1 ) {
            if( $i == $breakRev ) {
                $revisions = "$revisions | <A href=\"$scriptUrlPath/oops%SCRIPTSUFFIX%/%WEB%/%TOPIC%?template=oopsrev&amp;param1=1.$maxrev\">&gt;...</A>";
                $i = 1;
            } else {
                $j = $i - 1;
                $revisions = "$revisions | <A href=\"$scriptUrlPath/rdiff%SCRIPTSUFFIX%/%WEB%/%TOPIC%?rev1=1.$i&amp;rev2=1.$j\">&gt;</A>";
            }
        }
        $i = $i - 1;
    }
    $tmpl =~ s/%REVISIONS%/$revisions/go;

    if( $topicExists ) {
        my( $date, $user ) = &wiki::getRevisionInfo( $topic, $rev, 1 );
        $user = &wiki::userToWikiName( $user );
        my $temp = &wiki::getRenderedVersion( "r1.$rev - $date by $user" );
        $tmpl =~ s/%REVINFO%/$temp/go;
    } else {
        $tmpl =~ s/%REVINFO%//go;
    }

    $tmpl = &wiki::handleCommonTags( $tmpl, $topic );
    $tmpl =~ s/%TEXT%/$text/go;
    $tmpl =~ s|</*nop/*>||goi;   # remove <nop> tags (PTh 06 Nov 2000)
    print "Content-type: text/html\n\n";
    print $tmpl;
}
