#!/usr/bin/perl -w

# Set library paths in @INC, at compile time
BEGIN { unshift @INC, '.'; require 'setlib.cfg' }

use CGI;
use TWiki;

use strict;

use vars qw( $topic $webName $scriptUrlPath $userName );

&main();

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

  ( $topic, $webName, $scriptUrlPath, $userName ) = &TWiki::initialize( $thePathInfo, $theRemoteUser, $theTopic, $theUrl, $query );
  
  TWiki::writeHeader( $query );
  
  if ( opendir( DIR, $TWiki::dataDir ) ) {
    my @dirs = grep( /^[A-Z]/, readdir( DIR ) );
    foreach my $tmpDir ( @dirs ) {
      print "<h2>$tmpDir</h2>";
      opendir( TMP, "$TWiki::dataDir/$tmpDir" );
      my @files = grep( /^[A-Z].*\.txt$/, readdir( TMP ) );
      foreach my $file ( @files ) {
        my ( $tmpTopic ) = ( $file =~ /(.*?)\..*/ );
        my ( $meta, $text ) = &TWiki::Store::readTopic( $tmpDir, $tmpTopic );
        if ( $text =~ /%ACTION{/ ) {
          print "Resaving $tmpDir.$tmpTopic ... ";
          my $error = &TWiki::Store::saveTopic( $tmpDir, $tmpTopic, $text, $meta, "", "on", "on" );
          if ($error) {
            print "ERROR<br>";
          } else {
            print "OK<br>";
          }
        }
      }
      closedir( TMP );
    }
    closedir( DIR );
  }
}