#!/home/rebelsky/perl/bin/perl

##########NEW BLOCKIFY
#########VERSION 0.05, pre-vivek-blockify


#%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#############   BUG LIST ######################
#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

### Jared Seaman             Last Updated: 7/12/99
### A program for doing some serious blockification of
###  files to be edited... 
### Begun 7/9/99

##############################################
#=pod
#  =head1 blokify.pl
#  This should \do some quick-n-dirty blockification of to-be-edited
#  files; i.e. it will \read stuff in, BLOK at a \time [which is to say, up to
#  an HTML tag], & should import \some hardcoded rule-predicessors
#  to apply to BLOKs....
#  =cut

#This gives us some facilities for slashifying strings & things, so that
#  Perl's reg.exps will be ok with them.
#use searchslash;

#This gives us facilities for reading files into strings (read_in_file)
# or arrays (file_into_array).
use filereaders;

#This contains the siteweave routine that applies the local Rules.
use siteweave; 

local ($default_rules) = 'sample/rules.swr'; 
#This variable is here to indicate the default file we are to be editing 
local ($default_file) = 'sample/nuts.html';
#This variable indicates the default location for the updated file
local ($default_update) = 'sample/nnuts.html';

#If outy is not locally defined up here, they will be globally
#defined by open_files, below.

#the (current) definitions of what = a block
local (@block_defs) = (2, "^(<.*?>)","^([^<]*)");
local (*outy); #the output File-Handle
local (@rules); #the array that contains all rules we use
local ($oldfile); #the string that contains the contents of the old file
&main;              #The MAIN is what we want to use.

##############################################

###Main; contains the NEW version of the BLOK-readin' in thing
sub main () 
  {#main
    &open_files();
    my $block; #the thing we use to process a block of text
    #Now, @rules & $oldfile will have their contents filled.
    #print $oldfile;
    #Let's blockify the $oldfile, & write out the modified blocks:
    my $i = 0;
    my $j = @rules;
    my $dmzstart, $dmzstop;
    #We check to see if there is a DMZ tag.  If there is more than 1, we
    #take the first, so there.
    while ($i < $j)
      {#check rules for DMZ
        if ($rules[$i] =~ /(.*?)\s+DMZ=(-|)(\d*)\s+(.*)/)
          {#we've found a DMZ rule...
            $rules[$i] = ""; #blank that variable, cuz we're implementing it here

            $dmzstart = $1;
            $dmzstop = $4;
            #there are now 3 rules, including the DMZ tag...
            $block_defs[0] = 3;
            #The "string of stuff" can not contain a DMZ start..
            #The new tag is the "DMZ--stuff not to change--/DMZ"\
            #  & just to be nice, if they don't close the DMZ tag, it keeps
            #  going until it gets to the end of the file.
            $block_defs[3] = '^\s*'.$dmzstart.'((?:.|\n)*)'.$dmzstop;
            last;
          }#close-- found-DMZ-rule
        $i++;
      }#close while-loop-for-checking-rules-for-DMZ
    while ($oldfile)
      {#while stuff in $oldfile, keep processing the file
        #HERE'S WHERE IT ACTUALLY DOES BLOKIFYING!
        #There CAN be N ways to define a blok:
        for ($i=$block_defs[0]; $i >0; $i--)
          {#checking to cut out a blok.
            if ($oldfile =~ s/$block_defs[$i]//)
              {#get that blok
                $block = $1;
                #If we are in an HTML tag..
                if ($i == 1) 
                  {#we pass in an IN-HTML value as the 2nd value.      
                    $block = &siteweave($block,1);
                  }
                else
                  {#if not In HTML

                    #if we're in a DMZ tag..
                    if ($i == 3)
                      {
                        #print it out unmodified!
                        &outprint ($block);
                        next;
                      }
                    #if we're NOT in a DMZ tag:
                    else 
                      {
                        #we modify the not-HTML, non-DMZ section
                        $block = &siteweave($block);
                      }
                  }#close If not In HTML

                #in all cases but DMZ (where we already printed it out
                # unmodified & went through the loop again), we print out
                #print $block."\n";
                &outprint ($block);
              }#end of get-that-blok
          }#close checking to cut out a block
      }#close $oldfile while loop
  }#close main


#%%%%%%%%%%%%%%%%%%%%%%%%%%
###########NON-MAIN SUBPROGRAMS#########
#%%%%%%%%%%%%%%%%%%%%%%%%%%

##########################OUTPRINT
#This is a subprogram that prints the string it is
#given to outy.
sub outprint ()
  {
    my $block = $_[0];
    print outy $block."\n";
  }
    
##########################OPEN_FILES
#Here I'll use some code from earlier programs, that do
#the file-opening.
sub open_files ()
  {#open files
    #local (*rulz); #the rules File-Handle
    my ($rulefile); #file name for Rules file
    #if the user gave us a 3rd input in command-line,
    #that was the name of their rules file
    if ($ARGV[0]) {$rulefile = $ARGV[0];}
    #if the user did not, we ask for it here
    else
      {
        print "What rules file would you like to use?";
        chomp($rulefile = <STDIN>);
        if (!$rulefile)
          {$rulefile = $default_rules;}
      }
    my $fil;#file name for stuff to be read IN
    #if the user gave us a 2nd input in command-line,
    #that was the name of the file they wanted to modify..
    if ($ARGV[1]) {$fil = $ARGV[1];}
    #if the user did not, we ask for it here.
    else
      {
        print "What file would you like to convert?";
        chomp($fil = <STDIN>);
        if (!$fil) 
          {$fil = $default_file;} #my testing file default
      }#close get-where-to-write-input-file-to
    print $fil." will be blockified & sitewoven!\n";
    my $new; #where we're writing out the Newly-Edited thing
    #If they input a 3rd value on the command line, 
    #that's where it is to be written to
    if ($ARGV[2]) {$new = $ARGV[2];}
    #if the user did not, we ask for it here
    else
      {
        print "What file would you like the converted version written to?";
        chomp ($new = <STDIN>);
        if (!$new)
          #in case they wanted to write & save to the same place, or the default, is: 
          {$new = $default_update;} #my testing output default
      }#close get-where-to-write-output-file-to
    #reggy is the regular expression we'll use to sort the rules in file_into_sorted...
    #$2 is the one we want, as reflected below.
    #   What reggy matches is "Chunk, followed by =, then priority, then * or \s
    my $reggy = '(.*?)=(\d+|-\d+)?(\*|\s)';
    #Fill the @rules array with the rules, sorted by Priority, then rules-file order
    @rules = &file_into_sorted_array($rulefile,$reggy); 

    $oldfile = &read_in_file($fil); #fill the $oldfile with the contents of oldfile
    #If outy is not locally defined at the beginning, it will be globally
    #defined here.
    open (outy, ">$new") or die "ZAM!  Bad Write-to attempt"; #& the to-file
    return; #all is well in open_file-vil
  }#close open_files

