#!/usr/bin/perl -w

# Version 3.0 last update on 22/11/2001 by <alex.klassmann@mpi.nl> 

# make sure that all output is flushed.
$| = 1;

$startTime = time;

my $usage='
Usage: cgn-stat.pl anchorpattern tracklabel [list of files] [arguments]
  -q				 quiet mode. No information of search status
  -s				 subset mode. File names are expected to follow query-information;
					 reads from <queryfile> if specified, otherwise from STDIN
					 In the latter case last file name must be followed by a line 
					 beginning with the word READY to terminate input
  -cp <corpuspath>	 expects the CGN-Files in <corpuspath>, 
					 if not set, built-in default is used

';
	
prepro();

$quiet || print "STARTING SEARCH\n";

foreach $fileName (@preFileNames) {
	# print $fileName."\n";
	open(INFILE, $fileName);
	$files++;
	
	while ($line = <INFILE>) {
		# check if we have the right track
			if ($line =~ /^$trackLabel/o) {
			# look for the anchor pattern
			while ($line =~ /$anchorPattern/go) {
			$hits++;
				$quiet || print "HIT $hits\n"; 
			}
		}
	}
	
	close(INFILE);
	
	# report progress
	$quiet || print "PROGRESS ".$files / ($#preFileNames + 1)."\n";

}

$duration = time - $startTime;

print "found $hits hits in $files files in $duration seconds\n";

$quiet || print "READY\n"; 


sub prepro {
	$corpusPath = "Corpora/cgn/annot";

	$anchorPattern = $ARGV[0];
	$trackLabel	   = $ARGV[1];

	$anchorPattern || die "No Anchor-Pattern! \n";
	$trackLabel || die "No Tracklabel! \n";

	#looking for arguments
	for($i=2;$i<=$#ARGV;$i++){
		if($ARGV[$i] !~ /^\-/) { $qfile=$ARGV[$i]; next; }
		if($ARGV[$i] eq "-q")  { $quiet=1;  next; }
		if($ARGV[$i] eq "-s")  { $subset=1; next; }
		if($ARGV[$i] eq "-cp") { $corpusPath=$ARGV[++$i]; next; }
		die $usage;
	}	

	@preFileNames = ();
	
	if($subset){
		if(defined($qfile)){
			# read subset files from $qfile
			open(QFILE, $qfile);
			@qfile = <QFILE>;
			close(QFILE);
		}
		else{
			# read subset files from STDIN
			@qfile = ();
			do {
				$input = <STDIN>;
				push @qfile, $input;
			}
			while ($input !~ /^READY/) 
			}
		
		# process qfile until EOF or key-word "READY"
		foreach(@qfile){
			# ensure to have full file-name
			chomp();						
			if(/^READY/){ last; }			   # leave loop
			if(!$_) { next; }				   # skip empty lines
			$_=~s/^file:\/\///;				   # chop "file://"
			if(!/\.sea$/) { $_.=".sea"; }	   # append, if necessary ".sea"
			if(!/^\/|\w:/) { $_=~s/^/$corpusPath\// } # prepend, if necessary corpusPath
			push @preFileNames, ("$_"); 
		}
	}
	else{
		@preFileNames = fileNames($corpusPath, "sea");
	}

	$hits = 0;
	$files = 0;
}


sub fileNames {
	my ($path, $extension) = @_;

	opendir(DIR, $path);
	my @entries = readdir(DIR);
	close(DIR);
	
	my @files = ();
	
	my $entry;
	foreach $entry (@entries) {
		if ($entry =~ /^\./) {
			next;
		}
		
		my $newPath = $path."/".$entry;
		
		if (-d $newPath) {
			push @files, fileNames($newPath, $extension);
		}
		
		if ($entry =~ /.*\.$extension$/) {
			push @files, ("$newPath");
		}
	}
	
	return @files;
}








