#!/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;

$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
print "Content-type: text/html\n\n";

&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 ) = 
	&wiki::initialize( $thePathInfo, $theRemoteUser, $theTopic, $theUrl );

#FB cache
		my $cacheDir = &wiki::getCacheDir();
#/FB

#FB newTemplateScheme
		my $template = $query->param("template");
		unless($template) { #look for %TEMPLATE% in preferences
    	for( $x = 0; $x < @wiki::prefsKeys; $x++ ) {
        	if( $wiki::prefsKeys[$x] eq "TEMPLATE" ) {
						$template = $wiki::prefsValues[$x];
						last;
        	}
    	}
		}
		$template="view" unless $template; #default
 			
#/FB
    my $tmpl= "", $text="";
    my $rev= $query->param( "rev" );
    my $maxrev = 1;
    my $extra = "";

    if( ! &wiki::webExists( $webName ) ) {
		$tmpl= &wiki::readTemplate( "noweb" );
		$tmpl = &wiki::handleCommonTags( $tmpl, $topic );
		print $tmpl;
		return;
    }
#FB newTemplateScheme
#    $tmpl= &wiki::readTemplate( "view", $topic );
    $tmpl= &wiki::readTemplate( $template, $topic );
#/FB
    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, "1.$rev" );
            $extra .= "r1.$rev";
	} else {
#FB cache

# if makefile does not exists,
# write a generic makefile for the topic (forcing rebuild)
# The real list of dependences is actually made by makecache
# when rendering text
			$cached = 1;
			unless (-e "$cacheDir/$webName/$topic/Makefile") {
				undef %wiki::dependences;
				$wiki::dependences{"FORCE"}=1;
				&wiki::makemake($webName,$topic,$thePathInfo, $theTopic);
			}
			`/usr/bin/make -f $cacheDir/$webName/$topic/Makefile`;
#/FB
	    $rev= $maxrev;
	}
    } else {
	$rev = 1;
	if( &wiki::isWikiName( $topic ) ) {
	    $text= &wiki::readTemplate( "notext" );
	} else {
	    $text= &wiki::readTemplate( "notwiki" );
	}
        $extra .= " (not exist)";
    }
#FB cache
	if ($cached) {
		$text = &wiki::readFile("$cacheDir/$webName/$topic/body.txt");
	} else {
#/Fb
    $text = &wiki::handleCommonTags( $text, $topic );
    $text = &wiki::getRenderedVersion( $text );
#FB cache
	}
#/Fb	
    if( $wiki::doLogTopicView ) {
        # write log entry
#FB newTemplateScheme
#        &wiki::writeLog( "view", "$webName.$topic", $extra );
        &wiki::writeLog( $template, "$webName.$topic", $extra );
#/FB
    }

    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, "1.$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;
    print $tmpl;
}
