#/usr/bin/perl


############################################
use strict;

use POSIX qw(strftime);
use Time::Local;
use Getopt::Long;
use Pod::Usage;
use constant {
############# Other constants are located on Line 65 ###########
	# DO NOT include the trailing slashes at the end of the urls
	ES_SOURCE_BASEDIR => "/lampdev/digital/ciaonet",
	#ES_SOURCE_BASEDIR => "/www/data/dlc/ciao-test",
	ES_TEST_DESTINATION_BASEDIR => "/lampdev/digital/ciaonet/admin/perlpublishing/test", #"/lampdev/digital/ciaonet/admin/perlpublishing/testsite", 
	#ES_TEST_DESTINATION_BASEDIR => "/lampdev/digital/ciaonet/admin/perlpublishing/testsite", 
	ES_LIVE_DESTINATION_BASEDIR => "/lampdev/digital/ciaonet/admin/perlpublishing/test", #/lampdev/digital/ciaonet/admin/perlpublishing/livesite
	ES_ESCAPE_UNIX_GID => 15757, # escape is 5404; dkvserv is 15757; dkvprod is 6824; staff (barnacle) is 10
	ES_NEWFILE_PERMISSIONS => 0775, # must have the leading zero.
	ES_NEWDIRECTORY_PERMISSIONS => 0775, #must have leading zero
	PERL_GENERATOR_SCRIPTS_DIR =>"/lampdev/digital/ciaonet/admin/perlpublishing",
};



############ GET COMMAND LINE OPTIONS ############
my $filename = $ARGV[0];

if ($filename =~ /html$/is  ){
			
	print "\n Found html file: " .   $filename . "\n";

	my $filename = shift;
	open FH, "+<", $filename  or die $!;
	my $text = do { local( $/ ) ; <FH> } ;# slurp
	$text =~ s|<!-- begin auto-2003-03-16-RRAADDEEPPIICC -->||gs;
	$text =~ s|<!-- end auto-2003-03-16-RRAADDEEPPIICC -->||gs;
	if (!($text =~ /\#include/x)  ){
		print "$filename does not match the term #include \n";
	$text =~ s|<hr ([^>]*)>||ig;
	# remove empty paragraph tags
$text =~ s|<p>([\s]*)</p>||igs;
	  $text =~ s|<center>([\s]*)</center>||igs;
	$text =~ s|<a href="http://www.ciaonet.org([\/]?)"([^>]*)>||ig;
	$text =~ s|<a href="http://ciaonet.org([/]?)"([^>]*)>||ig;
	$text =~ s|<img([^>]*)src="([^>]*)return.gif"([^>]*)>([^<]*)</a>||ixg;
		$text =~ s|<body([^>]*)>|<body><!--#include virtual="/incs/masthead.html" --><!--#include virtual="/incs/leftnav.html" --><!--#include virtual="/incs/wrapperOpen.html" -->|io;
		$text =~ s|</body>|<!--#include virtual="/incs/wrapperClose.html" --><!--#include virtual="/incs/footer.html" --></body>|io;
		close FH;
		open FH, ">", $filename  or die $!;
		print FH $text;
		#print $text;
	} elsif (($text =~ /footer.html/is) &&  (!($text =~ /wrapperClose.html/is)) )  {
		print "$filename does  match the term footer.html \n";
		##########
			$text =~ s|<hr ([^>]*)>||ig;
		$text =~ s|<p>([\s]*)</p>||igs;
	 $text =~ s|<center>([\s]*)</center>||igs;
	$text =~ s|<a href="http://www.ciaonet.org([\/]?)"([^>]*)>||ig;
	$text =~ s|<a href="http://ciaonet.org([/]?)"([^>]*)>||ig;
	$text =~ s|<img([^>]*)src="([^>]*)return.gif"([^>]*)>([^<]*)</a>||ixg;
	############
	#########
		$text =~ s|<body(.)*>|<body><!--#include virtual="/incs/masthead.html" --><!--#include virtual="/incs/leftnav.html" --><!--#include virtual="/incs/wrapperOpen.html" -->|io;
		$text =~ s|<!--#include virtual="/incs/footer.html" -->([\s]*)</body>|<!--#include virtual="/incs/wrapperClose.html" --><!--#include virtual="/incs/footer.html" --></body>|io;
		print "Check out:  $filename \n";
		close FH;
		open FH, ">", $filename  or die $!;
		print FH $text;
		#print $text;
	} else {
		# remove empty paragraph tags
		$text =~ s|<p>([\s]*)</p>||igs;
		 $text =~ s|<center>([\s]*)</center>||igs;
		$text =~ s|<a href="http://www.ciaonet.org([\/]?)"([^>]*)>||ig;
		$text =~ s|<a href="http://ciaonet.org([/]?)"([^>]*)>||ig;
		print "includes already added, may have removed link:  $filename \n";
		close FH;
		open FH, ">", $filename  or die $!;
		print FH $text;
		
	}
	close FH;
	
}