file = src/sidebars
------------------------------------------------------------------------------

#!/usr/pkg/bin/perl
#
# SIDEBARS - Provides a pseudo-navbar for people who don't wish to use
# frames for navigation purposes.  Supports subfolders!
$version = "v1.0 Baxil, 8-22-2K";

$filelist = "/u0/baxil/pagesWSidebar";
$sc = ","; # split character -- separates filename & subfolder & textstring
$sidebarsrc = "/u0/baxil/sidebardefault";

print STDOUT "Sidebars $version;\n File list (${filelist}), source (${sidebarsrc})\n";

$srcdir = "/u0/web/baxil/";
$begmatch = '<!--sidebar-->';
$endmatch = '<!--main-->';

$imgmarker = '\[]'; # may need to change if sidebar text contains it. 
# Since $imgmarker is used in a pattern match, THIS EQUALS THE STRING '[]'.
$hitimage = '<IMG SRC="Images/point.gif" ALT="@ " WIDTH="11" HEIGHT="18">' . "\n";
$missimage = '<IMG SRC="Images/no.gif" ALT="  " WIDTH="11" HEIGHT="18">' . "\n";

$Errors = "";

############################ Main program ##############################

(open(FILELIST, "< $filelist") && open(SIDEBARSRC, "< $sidebarsrc")) 
  or die("Can't open one of the two default files!\n");

@sidebar = <SIDEBARSRC>;
close SIDEBARSRC; # that was quick. ;)

# ----- Process the filelist, build a hash table w/ the data ----------
%fnord = ();
while ($holder=<FILELIST>) { 
   next if (m/^\w\w/); # skips lines not starting w/ 2 letter/numbers.
   @temp = split /$sc/o, $holder;
   $tempkey = shift @temp;
   $fnord{$tempkey} = join $sc, @temp; 
}

# ----- Go through the hash table and change each file in turn --------
$ctr = 0;
foreach $ffile (keys %fnord) {

   # ---- build this particular file's sidebar ------
   ($matchnum, $matchstr) = split /$sc/o, $fnord{$ffile};

   chop $matchstr; # this removes the \n from the end of it.

   $j = -1;
   @mysidebar = ();
   for ($c = 0; $c < @sidebar; $c++) {
      if ( $sidebar[$c] =~ /^\{(.)\}/ ) { # a new SB entry: has {num}
         $j++;
         # now determine whether this particular one goes in our bar or not
         if ($1 == "0" || $1 == $matchnum) {
            $mysidebar[$j] = $sidebar[$c];
         } else {
            do { $c++ } until ( $sidebar[$c] =~ /^\{(.)\}/ );
            $c--; # back up one so for will increment to next "good" line
            next;
         }
      } else {
         next if ($j == -1); # throw away stuff at beginning of file ;)
         $mysidebar[$j] .= $sidebar[$c]; 
      }
   }
   # And one more time through to insert appropriate images! :)
   for ($j = 0; $j < @mysidebar; $j++) {
      if ( $mysidebar[$j] =~ /$matchstr/s ) {
         $mysidebar[$j] =~ s/$imgmarker/$hitimage/s;
      }
      $mysidebar[$j] =~ s/$imgmarker/$missimage/sg; # get the rest of 'em, if any.
      $mysidebar[$j] =~ s/^\{.\}//s; # and remove that annoying {X}.
   }

   # --------------- Now prepare file to be written to -------------------
   
   $srcfile = $srcdir . $ffile;
   
   if (! (-e $srcfile) ) {
     $Errors .= "Apparently $srcfile doesn't exist.  Please fix $filelist.\n";
     next;
   }
   if (! `grep \'$begmatch\' $srcfile`) {
     $Errors .= "No match of $begmatch in $srcfile!  Didn't process.\n";
     next;
   }
   if (! `grep \'$endmatch\' $srcfile`) {
     $Errors .= "No match of $endmatch in $srcfile!  Didn't process.\n";
     next;
   }

   if (! open(SRCFILE, "< $srcfile") ) {
      print STDOUT "Couldn't open file $srcfile!  Quitting.\n";
      print STDOUT $Errors;
      die;
   }
   
   # Retrieve static (not-modified-here) text from the given file 

   $partone = "";
   $partthree = ""; # re-zero variables ;P
   $stoplooping = "";
   do {  # Get first part of the homepage file
     $holder=<SRCFILE>;
     $partone .= $holder;
     $stoplooping = "yes" if ($holder =~ /$begmatch/);
   } until ($stoplooping);
   until ($HellFreezesOver) { # Now deal with (i.e. ignore) what to replace
     $holder=<SRCFILE>; 
     last if (($temp = $holder) =~ /$endmatch/); # exit loop if matchy.
   }
   
   $partthree = $holder; # since it wasn't added to part two
   while ($holder=<SRCFILE>) { $partthree .= $holder; } # Get rest of the file 
   close SRCFILE;
   
   # make baxup of source file here.
   if (`mv $srcfile ${srcfile}.bak`) { # 0 = successful execution.
      print STDOUT $Errors . "\n";
      die("Couldn't back up $srcfile -- ABORTED W/O CHANGING IT!");
   }

   # ------ And finally, write the new file with happy updated sidebar -------
   
   if (! open(HOMEOUT, "> $srcfile") ) {
     print STDOUT $Errors . "\n";
     die("couldn't open $srcfile for write, FILES HAVE BEEN MOVED!!");
   }
   
   print HOMEOUT $partone . "\n";
   
   for ($j = 0; $j < @mysidebar; $j++) {
      print HOMEOUT $mysidebar[$j] . "\n";
   }
   print HOMEOUT $partthree;
   close HOMEOUT;

   print $ffile;
   $ctr++;
   if ($ctr % 3 == 0) { print "\n"; } else { print " ... "; }

} # ... and proceed to next file in the list.


# --------- End-of-program cleanup --------------------------



if ($Errors != "") {
   print $Errors;
} else {
   print "\nProgram completed successfully. Remember backup files.\n";
}
exit;

