#
# TWiki WikiClone ($wikiversion has version info)
#
# Copyright (C) 2000-2001 Andrea Sterbini, a.sterbini@flashnet.it
# Copyright (C) 2001 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
#
# =========================
#
# This is the default TWiki plugin. Use EmptyPlugin.pm as a template
# for your own plugins; see TWiki.TWikiPlugins for details.
#
# Each plugin is a package that contains the subs:
#
#   initPlugin           ( $topic, $web, $user, $installWeb )
#   commonTagsHandler    ( $text, $topic, $web )
#   startRenderingHandler( $text, $web )
#   outsidePREHandler    ( $text )
#   insidePREHandler     ( $text )
#   endRenderingHandler  ( $text )
#
# initPlugin is required, all other are optional. 
# For increased performance, DISABLE handlers you don't need.
#
# NOTE: To interact with TWiki use the official TWiki functions 
# in the &TWiki::Func module. Do not reference any functions or
# variables elsewhere in TWiki!!


# =========================
package TWiki::Plugins::LastModifiedPlugin;

# =========================
use vars qw(
        $web $topic $user $installWeb $VERSION $debug
        $doOldInclude $renderingWeb $dataDir $lastMod $theText
    );

$VERSION = '1.000';

# =========================
sub initPlugin
{
    ( $topic, $web, $user, $installWeb ) = @_;


    # check for Plugins.pm versions
    if( $TWiki::Plugins::VERSION < 1 ) {
        &TWiki::Func::writeWarning( "Version mismatch between DefaultPlugin and Plugins.pm" );
        return 0;
    }

    $dataDir = &TWiki::Func::getDataDir();
    my @fileStat=stat("$dataDir/$web/$topic.txt");
    $lastMod = $fileStat[9];
    $theText = $topic; # Keep track of current text...

    # Plugin correctly initialized
    &TWiki::Func::writeDebug( "- TWiki::Plugins::DefaultPlugin::initPlugin( $web.$topic ) is OK" ) if $debug;
    return 1;
}

sub writeHeaderHandler
{
###  my ( $query ) = @_;    # do not uncomment, use $_[0], $_[1]... instead
    my @fileStat=stat("$dataDir/$web/$topic.txt");
    print "X-TWiki-Date: ".localtime($fileStat[9])."\n";
    print "Last-Modified: ".localtime($lastMod)."\n";
    &TWiki::Func::writeDebug( "- TWiki::Plugins::DefaultPlugin::writeHeaderHandler : X-TWiki".($fileStat[9])." $lastMod\n") if $debug;

    print $_[0]->header();
}

sub preIncludeHandler
{
### my ( $topic, $web, $includeTopic, $includeWeb) =@_;
    &TWiki::Func::writeDebug( "- TWiki::Plugins::DefaultPlugin::preIncludeHandler PRE : $lastMod, $_[0], $_[1]\n" ) if $debug;
    my @fileStat=stat("$dataDir/$_[3]/$_[2].txt");
    $lastMod = $fileStat[9] if ($fileStat[9] > $lastMod);
    &TWiki::Func::writeDebug( "- TWiki::Plugins::DefaultPlugin::preIncludeHandler POST: $lastMod, $_[2], $_[3]\n" ) if $debug;
}

# =========================
sub DISABLE_commonTagsHandler
{
### my ( $text, $topic, $web ) = @_;   # do not uncomment, use $_[0], $_[1]... instead

    &TWiki::Func::writeDebug( "- DefaultPlugin::commonTagsHandler( $_[2].$_[1] )" ) if $debug;

    # This is the place to define customized tags and variables
    # Called by sub handleCommonTags, after %INCLUDE:"..."%
    # for compatibility for earlier TWiki versions:

    # do custom extension rule, like for example:
    # $_[0] =~ s/%WIKIWEB%/$TWiki::wikiToolName.$web/go;
}

# =========================
sub DISABLE_startRenderingHandler
{
### my ( $text, $web ) = @_;   # do not uncomment, use $_[0], $_[1] instead

    &TWiki::Func::writeDebug( "- DefaultPlugin::startRenderingHandler( $_[1] )" ) if $debug;

    # This handler is called by getRenderedVersion just before the line loop
}

# =========================
sub DISABLE_outsidePREHandler
{
### my ( $text ) = @_;   # do not uncomment, use $_[0] instead

    &TWiki::Func::writeDebug( "- DefaultPlugin::outsidePREHandler( $renderingWeb.$topic )" ) if $debug;

    # This handler is called by getRenderedVersion, in loop outside of <PRE> tag
    # This is the place to define customized rendering rules

    # do custom extension rule, like for example:
    # $_[0] =~ s/old/new/go;

}

# =========================
sub DISABLE_insidePREHandler
{
### my ( $text ) = @_;   # do not uncomment, use $_[0] instead

    &TWiki::Func::writeDebug( "- DefaultPlugin::insidePREHandler( $web.$topic )" ) if $debug;

    # This handler is called by getRenderedVersion, in loop inside of <PRE> tag
    # This is the place to define customized rendering rules

    # do custom extension rule, like for example:
    # $_[0] =~ s/old/new/go;
}

# =========================
sub DISABLE_endRenderingHandler
{
### my ( $text ) = @_;   # do not uncomment, use $_[0] instead

    &TWiki::Func::writeDebug( "- DefaultPlugin::endRenderingHandler( $web.$topic )" ) if $debug;

    # This handler is called by getRenderedVersion just after the line loop

}

# =========================

1;


