#!/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 ] <html_file> <header_template> <footer_template>\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 ( <ORIG> ) {
    if ( m/<!-- Standard Header Begin -->/ ) {
	$goahead = 1;
    }
    if ( m/<!-- Standard Footer Begin -->/ ) {
	$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 ( <ORIG> ) {
    if ( m/<!-- Standard Header Begin -->/ ) {
	$junk = <ORIG>;
	while ($junk && ( $junk !~ m/<!-- Standard Header End -->/) ) {
	    $junk = <ORIG>;
	}

        &do_template_header();

	$_ = <ORIG>;
    } elsif ( m/<!-- Standard Footer Begin -->/ ) {
	$junk = <ORIG>;
	while ($junk && ( $junk !~ m/<!-- Standard Footer End -->/) ) {
	    $junk = <ORIG>;
	}

        &do_template_footer();

	$_ = <ORIG>;
    } elsif ( ($comment) = $_ =~ m/<! (.*)$/ ) {
        print "Bad comment: $_";
	print "Bad comment: $comment\n";
	$comment = sprintf("%-70s", $comment);
	$_ = "<!-- $comment -->\n";
    } elsif ( m/<!\s*$/ ) {
        print "Bad comment: $_";
	$comment = sprintf("%-70s", "");
	$_ = "<!-- $comment -->\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 "<!" ) {
		    # this is an html comment
		    $state = 3;
		} else {
		    # found the start of an html command ... start capitalizing
		    $state = 2;
		}
	    }
	} elsif ($state == 1 ) {
	    # inside a string inside an html command
	    if ( $c eq "\"" ) {
		# end of string
		$state = 2;
	    } elsif ( $c 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 "<!-- Standard Header Begin -->\n";
    print NEW "<!-- Standard Header End -->\n";
}


sub do_std_footer {
    print NEW "<!-- Standard Footer Begin -->\n";
    print NEW "\n";
    print NEW "<HR>\n";
    print NEW "\n";
    print NEW "<ADDRESS>\n";
    print NEW "Last modified:  ", $mon+1, "/", $mday, "/", $year, " <BR>\n";
    print NEW "<A HREF=\"/~curt/\">Curtis L. Olson</A> <BR>\n";
    print NEW "<IMG HREF=\"/~curt/email.png\">\n";
    print NEW "</ADDRESS>\n";
    print NEW "\n";
    print NEW "<!-- Standard Footer End -->\n";
}


sub do_template_header {
    open( HEADER, "<$header" ) || die "Cannot open $header\n";
    while ( <HEADER> ) {
        s/\$\{ROOT\}/$relroot/;
        print NEW $_;
    }
    close( HEADER );
}

sub do_template_footer {
    $lm = sprintf( "Last modified:  %d/%02d/%2d <BR>", $mon+1, $mday, $year );

    open( FOOTER, "<$footer" ) || die "Cannot open $footer\n";
    while ( <FOOTER> ) {
        s/\%\%INSERT_LAST_MODIFIED_DATE\%\%/$lm/;
        s/\$\{ROOT\}/$relroot/;
        print NEW $_;
    }
    close( FOOTER );
}


#---------------------------------------------------------------------------
# $Log: html_template_update.pl,v $
# Revision 1.1  2004/03/08 14:52:59  curt
# Updates.
#
# Revision 1.1.1.1  2002/09/10 01:14:09  curt
# Initial revision of FlightGear-0.9.0
#
# Revision 1.2  2002/08/30 15:04:50  curt
# Various tweaks.
#
# Revision 1.1  2001/07/13 15:11:59  curt
# Initial revision.
#
# Revision 1.3  1996/07/09 21:03:36  curt
# Switched to an footer section (begin & end) rather than forcing the
# footer to be the last thing in the file.
#
# Revision 1.2  1996/05/07 17:17:05  curt
# Added an output message, skip symbolic links.
#
# Revision 1.1  1996/04/18 21:39:21  curt
# Initial revision.
#

