#/usr/local/bin/perl 
$|=1;

#update the HTML bar on the side of HTML pages using generatebar.pl

$path = "https://pds-imaging.jpl.nasa.gov/planetary_datasets";

@get_lines = (`perl -w generatebar.pl`);
@htmlfiles = <$path/*.html>; 
@htmlfiles = glob("$path/*.html"); 

foreach $one (@htmlfiles) {

  $file = $one;
  print "file is $file\n";
  @line = ();
  @array = ();
  open(FH, "+< $file");
  
  @array=<FH>;
  $pos = 0;
  $found = 'f'; #flag to tell if the page had the MARKERS 
  foreach $item (@array){
    if ($found eq 't') { last; }
    push(@line, $item) unless ($item =~ /^<!--start_bar-->/);

    if($item =~ /^<!--start_bar-->/) {
    
      push(@line, "<!--start_bar-->\n");
      for($x=$pos; $x<$#array; $x++)
      { 
     
        if($array[$x] =~ /^<!--end_bar-->/)
        {
          $found = 't';
          push(@line, @get_lines);
          push(@line, "\n");
          for($i=$x; $i<$#array + 1; $i++){
            push(@line, $array[$i]);}
            last;
        }#endif
       }#endfor
     }
     else { $pos++; }  
  
}#endforeach

if($found eq 't'){
seek(FH, 0, 0);  
print FH @line;
truncate(FH, tell(FH));
close(FH);
}#endif
}#endforeach


