@rem = '
@goto endofperl
';

local(@HTMFiles, %ErrCodes, $OutputFile, $Arg, $Redirect, $InputFile, $SortBy);
local(@ErrorsReport, %ErrorsFound);

	@HTMFiles	= ();
	%ErrCodes	= ();
	@ErrorsReport	= ();
	%ErrorsFound = ();
	undef($OutputFile);
	undef($SortBy);

	if (@ARGV==0)
	{
		die "Usage: $0 <input_file_list> [<error_list>/E] [<output_file>/O] [/C | /F]\n\n".
			"       <input_file_list> = file_name | [directory_name]/ALL\n".
			"       <error_list> = [Range1[:Range2][:Range3]...]\n".
			"             RangeX = Ennnn | Wnnnn | Ennnn-[E]mmmm | Wnnnn-[W]mmmm\n".
			"       /C | /F      = Sorting criteria: by error code (C) or by file name(F)\n";
	}

	foreach $Arg (@ARGV)
	{
		local (@Files, $dir, $file, @Codes, $CodeStr);

		if ( $Arg =~ /(.*)\/ALL$/i )
		{	$dir = (($1 =~ /^$/) ? "." : $1);
			if ( !opendir(LOCALDIR,  $dir) )
			{	print "Cannot open directory $dir \n";
				next;
			}
			@Files = readdir LOCALDIR;
			foreach $file (@Files)
			{	push(@HTMFiles, (($dir =~ /^\.$/) ? "" : $dir."\\") .$file) 
						if ((uc $file) =~ /\.HTM$/ && (uc $file) !~ /^SUMMARY\.HTM$/); }

			@HTMFiles	= sort @HTMFiles;
			closedir LOCALDIR;
		}
		elsif ( $Arg =~ /(.*)\/E$/i )
		{
			@Codes = split(/:/,$1);
			foreach $CodeStr (@Codes)
			{
				$CodeStr =~ s/[\s\t]//g;
				next if ($CodeStr =~ /^$/);
				local (@range, $first, $last, $ErrType, $i);
				@range = split(/\-/, $CodeStr);
				if (($count = @range) != 2)
				{	$ErrCodes{uc $CodeStr} = 1;		#PlaceHolder
				}
				else
				{
					$range[0] =~ /^([E|W])([0-9]*)$/i;
					$ErrType = $1;
					$first	 = $2;
					$range[1] =~ /([0-9]*)$/;
					$last	 = $1;
					for ($i=$first; $i <= $last; $i++)
					{
						$CodeStr = sprintf("%04d",$i);
						$CodeStr = $ErrType.$CodeStr;
						$ErrCodes{uc $CodeStr} = 1;		#PlaceHolder
					}
				}
			}
		}
		elsif ( $Arg =~ /(.*)\/O$/i )
		{
			if (defined($OutputFile))
			{	print "Output file redefined. Keep first definition $OutputFile.\n"; }
			else
			{	$OutputFile = $1; }
		}
		elsif ( $Arg =~ /^\/C$/i )
		{
			if (defined($SortBy) && $SortBy != /^F$/)
			{	print "Sort order redefined, keep first definition\n";	}
			else
			{	$SortBy = "C";	}
		}
		elsif ( $Arg =~ /^\/F$/i )
		{
			if (defined($SortBy) && $SortBy != /^C$/)
			{	print "Sort order redefined, keep first definition\n";	}
			else
			{	$SortBy = "F";	}
		}
		else
		{	push(@HTMFiles, $Arg); }
	}

	if (!defined($SortBy))
	{	$SortBy = "C";	}

	$Redirect = 0;
	if (defined($OutputFile))
	{
	local($Overwrite);

		# check for the output file
		if (-e $OutputFile)
		{	print "Output file $OutputFile already exists. Overwrite (Yes/No/All) [Y] ? ";

			$Overwrite = <STDIN>;
			chop $Overwrite;

			$Redirect = ($Overwrite =~ /^[nN]/) ? 0 : 1;
		}
		else
		{	$Redirect = 1; }
	}

	if ($Redirect == 1)
	{
		open(OUTPUTFILE, ">$OutputFile") || die "Cannot open $OutputFile: $!\n";
	}
	else
	{
		open(OUTPUTFILE, ">&STDOUT") || die "Cannot duplicate STDOUT: $!\n";
	}


	foreach $InputFile (@HTMFiles)
	{
		unless (open (HTMFILE, $InputFile))
		{
			print "Error opening $InputFile: $!\n";
			next;
		}
		print "[$InputFile]\n";

		&FileErrorsReport($InputFile);
		close(HTMFILE);
	}

	&MakeReport();

	close(OUTPUTFILE);

	die "Done.\n";


sub
FileErrorsReport
{
local($InputFile)=@_;
local($FileDateStamp, $FileTimeStamp, @Status, @Time);
local(%FileErrors, $AllErrors, $ErrorCode, $ErrorCount);

	@Status = stat($InputFile);
	@Time = localtime($Status[9]);
	$FileDateStamp = sprintf "%02d/%02d/%02d", $Time[4]+1, $Time[3], $Time[5];
	$FileTimeStamp = sprintf "%02d:%02d:%02d", $Time[2], $Time[1], $Time[0];

	$AllErrors = %ErrCodes;
	%FileErrors = ();

	while(<HTMFILE>)
	{
		if ($_ =~ /;\(E(\d\d\d\d)\)/)
		{
			$ErrorCode = "E$1";
			if ($AllErrors == 0 || defined($ErrCodes{$ErrorCode}) )
			{
				if (defined($FileErrors{$ErrorCode}))
				{	$FileErrors{$ErrorCode}++;	}
				else 
				{	$FileErrors{$ErrorCode} = 1;}
			}
		}
		if ($_ =~ /;\(W(\d\d\d\d)\)/)
		{
			$ErrorCode = "W$1";
			if ($AllErrors == 0 || defined($ErrCodes{$ErrorCode}) )
			{
				if (defined($FileErrors{$ErrorCode}))
				{	$FileErrors{$ErrorCode}++;	}
				else 
				{	$FileErrors{$ErrorCode} = 1;}
			}
		}
	}

	while (($ErrorCode,$ErrorCount) = each %FileErrors)
	{
		push(@ErrorsReport, [$ErrorCode, $InputFile, $ErrorCount, $FileDateStamp, $FileTimeStamp]);
		if (!defined($ErrorsFound{$ErrorCode}))
		{
			$ErrorsFound{$ErrorCode} = 0;
		}

		$ErrorsFound{$ErrorCode} += $ErrorCount;
	}
}

sub
MakeReport
{
local(@SortedArray, $RefArray);
local($ErrorCode, $InputFile, $ErrorCount, $FileDateStamp, $FileTimeStamp);
local($TotalErrors, $count);

	return if (($count = @ErrorsReport) == 0);

	if ($SortBy =~ /^C$/)
	{	@SortedArray = sort ByErrorCode @ErrorsReport; }
	else
	{	@SortedArray = sort ByFileName @ErrorsReport; }

	printf OUTPUTFILE "Error   File Name                      Err. Count     Date       Time  \n";
#	printf OUTPUTFILE "------- ------------------------------ -----------  --------   --------\n";
	printf OUTPUTFILE "-----------------------------------------------------------------------\n";
	foreach $RefArray (@SortedArray)
	{
		($ErrorCode, $InputFile, $ErrorCount, $FileDateStamp, $FileTimeStamp) = @$RefArray;
		printf OUTPUTFILE "%-7s %-30s %8d     %8s   %8s\n",
			$ErrorCode, substr($InputFile,0,25) , $ErrorCount, $FileDateStamp, $FileTimeStamp;
	}

	printf OUTPUTFILE "\n\n";
	printf OUTPUTFILE "Error   Err. Count\n";
#	printf OUTPUTFILE "------- -----------\n";
	printf OUTPUTFILE "-------------------\n";
	$TotalErrors = 0;
	foreach $ErrorCode (sort keys %ErrorsFound)
	{
		$ErrorCount = $ErrorsFound{$ErrorCode};
		printf OUTPUTFILE "%-7s %8d\n",	$ErrorCode, $ErrorCount;
		$TotalErrors += $ErrorCount;
	}
	printf OUTPUTFILE "-------------------\n";
#	printf OUTPUTFILE "------- -----------\n";
	printf OUTPUTFILE "Total   %8d\n", $TotalErrors;
	printf OUTPUTFILE "-------------------\n";
}


sub
ByErrorCode
{
	$$a[0] cmp $$b[0];
}

sub
ByFileName
{
	$$a[1] cmp $$b[1];
}

__END__

:endofperl
@echo off
rem @perl -d -IC:\nt\private\developr\costelr\cinf\perl\lib %0.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
@perl %0.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
