#!/usr/local/bin/perl -w
#	addpointer.pl.pl SOURCEDIR INDEX HELP
#	Add Dynamic HTML to ordinary pages
#
use English;
#
my($line,$source,$pptstem,$pptfile,@messages,@afterhead,@page,@page);
$source= $ARGV[0];
opendir(ORG,$source) || die("\nSorry Could not open ",$source," \n");
open(AFTERHEAD,'<afterhead') || die("\nSorry Could not open AFTERHEAD\n");
@afterhead = <AFTERHEAD>;
close(AFTERHEAD);
open(INDEX,"<".$source.'/'.$ARGV[1]) || die("\nSorry Could not open INDEX\n");
@beforemessages = <INDEX>;
close(INDEX);
$help = 'generichelp';
if(defined($ARGV[2]))	{
	$help = $source.'/'.$ARGV[2]; }
open(HELP,"<$help") || die("\nSorry Could not open HELP\n");
@helpdiv = <HELP>;
close(HELP);
open(MESSAGES,'<messages') || die("\nSorry Could not open MESSAGES\n");
@messages = <MESSAGES>;
close(MESSAGES);
#
#	Loop over HTML Pages
foreach $pptstem (sort readdir(ORG))	{
	if( $pptstem =~ m%(htm|html)$%i )	{
		if( $pptstem =~ m%wisdom% )	{
			next; }
		$pptfile = $source .'/'. $pptstem;
		open(OLDPAGE,'<'.$pptfile) || die("\nSorry Could not open ",$pptfile," \n");
		@page = <OLDPAGE>;
		close(OLDPAGE);
		$lastno = @page;
		$head = -1;
		$headend = -1;
		$body = -1;
		$bodyend = -1;
#
#	Loop over lines in current OLDPAGE
		for( $i=0; $i<$lastno; $i++)	{
			$line = $page[$i];
			chomp($line);
			$line =~ s%\015%%g;
			$line =~ s%\013% %g;
			$line =~ s%\325%'%g;
			$line =~ s%\205%...%g;
			$line =~ s%\227%--%g;
			$line =~ s%\221%`%g;
			$line =~ s%\222%'%g;
       			$line =~ s%\322%"%g;
       			$line =~ s%\323%"%g;
       			$line =~ s%\223%"%g;
       			$line =~ s%\224%"%g;
       			$line =~ s%\225%%g;
			$page[$i] = $line;
			if( $line =~ m%<head%i) {
				$head = $i; }
			if( $line =~ m%</head%i) {
				$headend = $i; }
			if( $line =~ m%<body%i) {
				$body = $i; }
			if( $line =~ m%</body%i) {
				$bodyend = $i; }
		}
		$bodyadd = -1;
		if($body < 0)	{
			$bodyadd = 0;
			if( $headend > -1)	{
				$bodyadd = $headend+1;}
			elsif( $head > -1)	{
				$bodyadd = $head+1; }
		}
		$pointerhead=0;
		$pointerend= $lastno;
		if($head > -1)	{
			$pointerhead = $head+1; }
		if($bodyend > -1)	{
			$pointerend = $bodyend;
		}
		open(NEWPAGE,'>'.$pptfile) || die("\nSorry Could not open ",$pptfile," \n");
		for( $i=0; $i<$lastno; $i++)    {
			$line = $page[$i];
			if( $pointerhead == $i) {
				print NEWPAGE @afterhead; }
			if( ($bodyadd > -1) && ($i==$bodyadd) ) {
				print NEWPAGE ('<body onload="WW_pageisloaded();" >',"\n"); }
			if( ($body > -1) && ($i==$body) ) {
				$line =~ s%<body%<body onload="WW_pageisloaded();" %i; }
			if( $pointerend == $i)	{
				print NEWPAGE "<div id='WWindexpage' >\n";
				print NEWPAGE @beforemessages;
				print NEWPAGE "</div>\n";
				print NEWPAGE "<div id='WWhelppage' >\n";
				print NEWPAGE @helpdiv;
				print NEWPAGE "</div>\n";
				print NEWPAGE @messages; }
			print NEWPAGE $line,"\n";
		}
		if($pointerend == $lastno)	{
				print NEWPAGE @messages; }
		close(NEWPAGE);
				
	}	# End loop over relevant files
}	# End loop over files
#
1;
