#!/usr/local/bin/perl # The purpose is to put in headers and footers for all the html files. # Directories are descended and all .html files are modified. # The program name (like crs or ccap) is required and is used to fill # the name of the files to include. If more arguments are given, # it is assumed you only want to do the files listed and the directories # will not be descended. # The program rmhead.pl undoes the effects of this one. # # usage: newhead.pl PROGRAM # where program is something like CRS or CCAP etc. # edit the $header and $trailer definitions if things change/move # # HTML REQUIREMENTS # The files must have and tags (capitalization doesn't # matter). These tags are used to locate where to insert the header # and footer. # # EXAMPLES # newhead.pl crs # Decends the directories and modifies all the .html files such that # the files crssub.html and crsend.html are included. # newhead.pl crs index.html sillystuff.html # Modifies only the files index.html and sillystuff.html. # # AUTHOR # Kirk Waters, TPMC @ NOAA/CSC. Sept 18, 1996 $#ARGV >= 0 || die "The program name must be supplied (ie. crs)\n"; $program=$ARGV[0]; print "Redoing for program $program\n"; $header="\"/text/${program}sub.html\""; $trailer="\"/text/${program}end.html\""; if ($#ARGV == 0) { &dodir('.'); }else{ foreach $i (1 .. $#ARGV) { print "$ARGV[$i]\n"; &swaphead($ARGV[$i]); } } sub dodir { local($dir, $nlink) = @_; local($dev, $ino, $mode, $subcount); ($dev, $ino, $mode, $nlink) = stat('.') unless $nlink; #get the list of files opendir(DIR,'.') || die "Can't open $dir"; # local(@filenames) = grep(/.html/,readdir(DIR)); local(@filenames) = readdir(DIR); closedir(DIR); if($nlink == 2) { #this dir has no subdirs for (@filenames) { next if $_ eq '.'; next if $_ eq '..'; if ( $_ =~ /.*\.html/ ) { print "$dir/$_\n"; &swaphead($_); } } } else { #has subdirs $subcount = $nlink -2; for (@filenames){ next if $_ eq '.'; next if $_ eq '..'; $name = "$dir/$_"; if ( $_ =~ /.*\.html/ ) { &swaphead($_); print $name, "\n"; } next if $subcount == 0; # seen all subdirs # get link count and check for directoriness ($dev, $ino, $mode, $nlink) = lstat($_); next unless -d $_; # it really is a directory, so do it recursively. chdir $_ || die "Can't cd to $name"; &dodir($name, $nlink); chdir '..'; --$subcount; } } } sub swaphead { local($file) = @_; $headfound=0; $tailfound=0; if( ! open(FILE,$file)) { print "Can't open $file\n"; return (0); } open(OFILE,"> ,tmp$$") || die "Can't open the temp file for $file"; while(){ if($_ =~ /^<[Bb][Oo][Dd][Yy].*/ ) { $headfound=1; print OFILE $_; print OFILE "\n"; print OFILE "
\n"; print OFILE "\n"; print OFILE "\n"; print OFILE "
\n"; print OFILE "
"; print OFILE "\n"; print OFILE "\n"; } elsif($_ =~/^<\/[Bb][Oo][Dd][Yy].*/ ) { $tailfound=1; print OFILE "\n"; print OFILE "\n"; print OFILE "\n"; print OFILE $_; } else { print OFILE $_; } } $headfound == 1 || print "WARNING! Couldn't find the tag in $file\n"; if($tailfound == 0) { print "WARNING! Couldn't find the tag in $file\n"; print "Adding it!\n"; print OFILE "\n"; print OFILE "\n"; print OFILE "\n"; print OFILE "\n"; } close OFILE; rename(",tmp$$",$file); endswap: }