#!/usr/bin/perl

# data
($sec,$min,$ora,$giorno,$mese,$anno,$gsett)=(localtime(time))[0,1,2,3,4,5,6];
$year="1900"+$year;

# scansiona tutti i file .html nella dir corrente
system("find . -name \"*html\" > /tmp/listgo.tmp");
open(LST, "/tmp/listgo.tmp");            
@LIST = <LST>;
close(LST);  
while(<@LIST>){
    $file=$_;
    if($file eq ""){exit;}
    $filenew=$file."new";
    print "Changing $file\n";

    @lungo="";
    $iopen=0;
    $iclose=0;
    $ibody=0;
    $open="<table width=100%><tr><td align=left>\n";
    $opengo="</td><td valign=top align=right>\n";
    
$goo="<script type=\"text/javascript\"><!--
google_ad_client = \"pub-8511590679376945\";
google_ad_width = 234;
google_ad_height = 60;
google_ad_format = \"234x60_as\";
google_ad_channel =\"6183772390\";
google_color_border = \"2D5893\";
google_color_bg = \"99AACC\";
google_color_link = \"000000\";
google_color_url = \"000099\";
google_color_text = \"003366\";
//--></script>
<script type=\"text/javascript\"
  src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
</script>";

    $close="</td></tr></table>\n";


#Inserisce tabella e codice google
    open(ACC,"<$file");
    while(<ACC>){
	$line=$_;
	if($line =~ "<body" || $line =~ "<BODY" ){$iopen=1;}
	if(($line =~ "ry<\h1>" || $line =~ "ry</H1>") && $iclose == 0){$iclose=1;}
	if($iopen == 2){push(@lungo,$open);$iopen=3;}
	if($iclose == 2){
	    push(@lungo,$opengo);
	    push(@lungo,$goo);
	    push(@lungo,$close);
	    $iclose=3;} 
	push(@lungo,$_);
	if($iopen == 1){$iopen=2;}
	if($iclose == 1){$iclose=2;}
    }
    close(ACC);
    
    open(OUT,">$filenew");
    print OUT "@lungo";
    close(OUT);
    system("touch -r $file $filenew");
    system("mv $filenew $file");
}
