#!/usr/bin/perl -I/usr/local/perl

use parseHTML;

sub usage {
    my $nameFile="auditHTML";
    print "
usage:\n
$nameFile takes an HTML file and give datas about the code.
$nameFile 
\t -f <html file> OR -fr <remote html file>
\t -p [--print] prints tags
\t -po [--print_opened] prints only opened tags
\t -ps [--print_stats] prints stats on opened tags
\t --pattern <pattern> text prefix for output
\t -e '<tag1 tag2 ...> print only specified tags
\t --notext [-nt] do not print CDATAs
\t --nooption [-np] do not print tag options
\t --compact [-c] do not print End Of Line
";
}

sub retrieveRemoteFile {
    my $url=shift;
    my $nameFile=shift;
    my $success=`wget $url -O $nameFile 2>&1 1>/dev/null`;
    return 0 if (!$success) ;
    return 1;
}


#===== mieux que getopts
sub getArgs {
    my @args=@_;
    my $oldKey="";
    my %opts=();
    for $el (@args) {
		if ($el=~/^-/) {
			$el=~s/-+//g;
			$opts{$el}="";
			$oldKey=$el;
		}
		else {
			$opts{$oldKey}=$el;
		}
    }
    return %opts;
}


#==========================
# MAIN


#_____________________________
#                             |
#      Gestion des entrées :

my %opts=();
my $output;
my $eol = "\n";

%opts=getArgs(@ARGV);


if ((exists $opts{h}) || (exists $opts{help})) {
    &usage();
    exit 0;
}

if ((!exists $opts{f}) && (!exists $opts{fr})) {
    print STDERR "I need a file !\n";
    &usage();
    exit 0;
}

if (exists $opts{f}) {
    $file=$opts{f};
    if (! -e $file) {
	print STDERR "Error : file does not exist : $file\n";
	exit;
    }
}

if (exists $opts{fr}) {
    $file=$opts{fr};
    &retrieveRemoteFile($file,"/tmp/auditHTMLtmp.html");
    $file="/tmp/auditHTMLtmp.html";
    if (! -e $file) {
	print STDERR "Error : file does not exist : $file\n";
	exit;
    }
}

if ((exists $opts{p}) || (exists $opts{print})) {
	$printing=1;
} else {
	$printing=0;
}

if ((exists $opts{po}) || (exists $opts{print_opened})) {
	$printingOpened=1-$printing;
} else {
	$printingOpened=0;
}

if ((exists $opts{ps}) || (exists $opts{print_stats})) {
	$printStats=1;
} else {
	$printStats=0;
}

if ((exists $opts{e}) || (exists $opts{extract})) {
	my $r;
	$r=$opts{e} if (exists $opts{e});
	$r=$opts{extract} if (exists $opts{extract});
	@extract=split(' ',uc($r));
}

if ((exists $opts{c}) || (exists $opts{compact})) {
    $compact=1;
}

if (exists $opts{pattern}) {
	$pattern=$opts{pattern};
} else {
	$pattern="---";
}

if (exists $opts{o}) {
    $output=$opts{o};
} else {
    $output="";
}


if ($output) {
    open(OUT,">$output");
    *STDOUT=*OUT;
}

my ($notext,$nooption);
$notext=0;$nooption=0;
$notext=1 if ((exists $opts{notext}) ||(exists $opts{nt})) ;
$nooption=1 if ((exists $opts{nooption}) ||(exists $opts{no})) ;

#   Fin Gestion des entrées 
#_____________________________|


$n=parseHTML->new();
$n->setFile($file);
$n->parseFile();

my %opened;

for $r (@{$n->{listeTag}}) {
    if ($printing ) 
    {
	if (($r->{name} eq 'TD') && ($r->{type} eq 'start') && ($compact==1)) {
	    $iaminatd=1;
	    $oldpattern=$pattern;
	}
	$n->printOneBal($r,"$pattern" x $r->{depth},{notexte=>$notext,nooption=>$nooption});
	if ((! $iaminatd) && ($compact==1)) {
	    print "\n";
	} else {
	    if ($compact==1) {$pattern=""};
	}
	if (($r->{name} eq 'TD') && ($r->{type} eq 'end') && ($compact==1)) {
	    $iaminatd=0;
	    $pattern=$oldpattern;
	    print "\n" ;
	}
	if (!$compact) { print "\n";}
    }
    
    if ($r->{statut} eq 'open') {
	if ($printingOpened) 
	{
	    $n->printOneBal($r,"$pattern" x $r->{depth},{notexte=>$notext,nooption=>$nooption});
	    print "$eol";
	}
	$opened{$r->{name}}+=1;
    }
}


if ($printStats) {
	for $op (keys(%opened)) {
		print $opened{$op}." tags $op ouvert(s)\n";
	}
}


if (scalar(@extract)) {
	my %tags;
	my @lref;
	for $tag (@extract) {
		$tags{$tag}=1;
	}
	$lref=$n->extractBalBlock(\%tags);
	for $r (@{$lref}) {
		$n->printOneBal($r,"$pattern" x ($r->{depth}-1),{notexte=>$notext,nooption=>$nooption});
		print "$eol";
	}
}
#=================================
#
#         End Main
#
#=================================















