package TWiki::Plugins::CommentPlugin;
# See Plugin topic for history and plugin information

use strict;
use integer;

use TWiki::Plugins::CommentPlugin::Comment;
use TWiki::Func;

sub new { 
	my $dummy = shift;
	my $config = shift;
	unshift @_,$dummy ;

   my ( $proto ) =@_ ;
   my $class = ref($proto) || $proto ;
   my $self;

	$self->{config}=$config;
   bless ( $self );

	$self->init_config();

   return $self ;
}



sub init_config { 

   my $self = shift;
	my $config = $self->{config} ;


 {
    $config->{TWiki}->{Plugins}->{CommentPlugin}->{VERSION} = '3.003';
    $config->{TWiki}->{Plugins}->{CommentPlugin}->{firstCall} = 0;
}

}# END of init_config

sub initPlugin { 
   my $self = shift;
	my $config = $self->{config} ;


 
	 $config->{TWiki}->{Plugins}->{CommentPlugin}->init_config () ;

  #my ( $topic, $web, $user, $installWeb ) = @_;

  if( $config->{TWiki}->{Plugins}->{VERSION} < 1.010 ) {
    $config->{TWiki}->{Func}->writeWarning ( "Version mismatch between CommentPlugin and Plugins.pm $config->{TWiki}->{Plugins}->{VERSION}" );
    return 0;
  }
  if( $config->{TWiki}->{Plugins}->{VERSION} < 1.020 ) {
    $config->{TWiki}->{Func}->writeWarning ( "Version mismatch between ActionTrackerPlugin and Plugins.pm $config->{TWiki}->{Plugins}->{VERSION}. Will not work without compatability module." );
  }
  $config->{TWiki}->{Plugins}->{CommentPlugin}->{firstCall} = 1;

  return 1;
}

sub commonTagsHandler { 
   my $self = shift;
	my $config = $self->{config} ;


  ### my ( $text, $topic, $web ) = @_;

  my $query = $config->{TWiki}->{Func}->getCgiQuery ();
  return unless( defined( $query ));
  my $action = $query->param( 'comment_action' ) || "";
  if ( defined( $action ) && $action eq "save" &&
	 $query->path_info() eq "/$_[2]/$_[1]" ) {
    # $config->{TWiki}->{Plugins}->{CommentPlugin}->{firstCall} ensures we only save once, ever.
    if ( $config->{TWiki}->{Plugins}->{CommentPlugin}->{firstCall} ) {
      $config->{TWiki}->{Plugins}->{CommentPlugin}->{firstCall} = 0;
      CommentPlugin::Comment::save( $_[2], $_[1], $query );
    }
  } elsif ( $_[0] =~ m/%COMMENT({.*?})?%/o ) {
    # Nasty, tacky way to find out where we were invoked from
    my $scriptname = $ENV{'SCRIPT_NAME'} || "";
    my $previewing = ($scriptname =~ /\/preview/ ||
		      $scriptname =~ /\/gnusave/);
    CommentPlugin::Comment::prompt( $previewing, @_ );
  }
}

1;

1;
