#!/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/listgoers.tmp");
open(LST, "/tmp/listgoers.tmp");            
@LIST = <LST>;
close(LST);  
while(<@LIST>){
    $file=$_;
    $filenew=$file."new";
    if($file eq ""){exit;}

    @lungo="";
    $iopen=0;
    $iwait=0;
    $iclose=0;
    $ibody=0;
    $open="<table width=100%><tr><td>\n";
    $opengo="</td><td align=right>\n";
    
$goo="<script language=\"JavaScript\" type=\"text/javascript\" 
  src=\"/banner/googlecode.js\">
</script>";
$goo2="<script type=\"text/javascript\"
  src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
</script>";
$other="<script language=\"JavaScript\" type=\"text/javascript\" src=\"/banner/brotate.js\"></script>";

# sceglie il codice banner in maniera causuale, con peso diverso 
$xxx=rand(4);
if($xxx < 1){$bancod=$other;$quale="Altro";}else{$bancod=$goo2;$quale="Google";}


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


#Inserisce tabella e codice google oppure altro codice
    open(ACC,"<$file");
    while(<ACC>){
	$line=$_;
	if($line =~ "Gallery</h1>" || $line =~ "Gallery</H1>"){$iopen=1;}
        if($iopen == 1 && $iclose == 0){push(@lungo,$open);}
        push(@lungo,$_);
        if($iopen == 1 && $iclose == 0){
            if($iwait==0){
                push(@lungo,$opengo);
                push(@lungo,$goo);
                push(@lungo,$bancod);
                push(@lungo,$close);
                $iclose=1;}
	}
    }
    close(ACC);
    if($iclose==1){
    print "Cambiato $file inserendo $quale\n";
    open(OUT,">$filenew");
    print OUT @lungo;
    close(OUT);
    system("touch -r $file $filenew");
    system("mv $filenew $file");
    }
}
