#!/usr/bin/perl #--------------------------------------------------------------------------- # htmlstamp.pl -- stamp a standard footer on the end of an html page # insert last update date, but doesn't change file mod time # insert my name, link to top level home page. # # $Id: html_template_update.pl,v 1.1 2004/03/08 14:52:59 curt Exp $ # (Log is kept at end of this file) #--------------------------------------------------------------------------- $name = shift(@ARGV); $root = shift(@ARGV); $header = shift(@ARGV); $footer = shift(@ARGV); die "Usage: $0 [ --project ] \n" if ( !defined($name) || !defined($root) || !defined($header) || !defined($footer) ); $user = $ENV{USER} || $ENV{LOGNAME} || $pw[0] || die "Cannot determine user for id $>"; if ( -l $name ) { print " $name is a symbolic link ... skipping.\n"; exit; } # check if file has header or footer $goahead = 0; open(ORIG, "<$name"); while ( ) { if ( m// ) { $goahead = 1; } if ( m// ) { $goahead = 1; } } if ( ! $goahead ) { print " $name has no header/footer information ... skipping.\n"; exit; } # determine the relative path to 'root' $tmp = $name; $tmp =~ s/^$root//; # strip off root $tmp =~ s/^\///; # strip off leading slash if it exists $count = 0; $tmp =~ s/(\/)/$count++;$1/eg; if ( $count == 0 ) { $relroot = "."; } else { $relroot = ".."; for ( $i = 1; $i < $count; $i++ ) { $relroot .= "/.."; } } # print "$count - $relroot - $tmp\n"; # Stat the original file so we can keep the original modification date ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($name); #recover the modification date from the stat ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($mtime); $year += 1900; print "Stamping ", $mon+1, "/", $mday, "/", $year, " -- $name\n"; rename($name, "$name.orig"); open(ORIG, "<$name.orig"); open(NEW, ">$name"); $state = 0; $line = 0; # transparently copy up to standard footer line while ( ) { if ( m// ) { $junk = ; while ($junk && ( $junk !~ m//) ) { $junk = ; } &do_template_header(); $_ = ; } elsif ( m// ) { $junk = ; while ($junk && ( $junk !~ m//) ) { $junk = ; } &do_template_footer(); $_ = ; } elsif ( ($comment) = $_ =~ m/\n"; } elsif ( m/\n"; } $line++; if ($state == 1) { print "Warning: $name: newline in string at $line.\n"; $state = 2; } # do ${ROOT} substitution s/\$\{ROOT\}/$relroot/; for ($i = 0; $i < length($_); $i++) { $c = substr($_, $i, 1); if ( $state == 0 ) { # outside an html command if ( $c eq "<" ) { if ( substr($_, $i, 2) eq "" ) { # probably end of string, but definitly a syntax error. # some browsers are ok with this, some not. $state = 2; print "Warning: $name: Syntax error in line $line.\n"; } } elsif ($state == 2 ) { # inside an html command if ( $c eq "\"" ) { # start of a string $state = 1; } elsif ( $c eq ">" ) { # end of html command $state = 0; } } elsif ($state == 3 ) { # inside an html comment if ( $c eq ">" ) { # end of html comment $state = 0; } } if ( $state == 2 ) { # capitalize this character $c =~ tr/a-z/A-Z/; } print NEW $c; } } close(ORIG); close(NEW); # reset the access and modification times on the file utime($atime, $mtime + 1, $name); sub do_std_header { print NEW "\n"; print NEW "\n"; } sub do_std_footer { print NEW "\n"; print NEW "\n"; print NEW "
\n"; print NEW "\n"; print NEW "
\n"; print NEW "Last modified: ", $mon+1, "/", $mday, "/", $year, "
\n"; print NEW "Curtis L. Olson
\n"; print NEW "\n"; print NEW "
\n"; print NEW "\n"; print NEW "\n"; } sub do_template_header { open( HEADER, "<$header" ) || die "Cannot open $header\n"; while (
) { s/\$\{ROOT\}/$relroot/; print NEW $_; } close( HEADER ); } sub do_template_footer { $lm = sprintf( "Last modified: %d/%02d/%2d
", $mon+1, $mday, $year ); open( FOOTER, "<$footer" ) || die "Cannot open $footer\n"; while (