#! /usr/bin/perl
#
# This script look through all log files and extract gatedb: entries.
# The files to be checked can be listed in @prefixes or @files
# The script automatically find newest files for @prefixes.

my $testgood = `modules/check_dir.pl | tail -1`;
if ( $testgood != 1 ) {
    printf("Strange directory.\n");
    printf("Please run this script only in Stand1 or Stand2 directory.\n");
    exit;
}

@prefixes = ( "ce-ni-", "stress-test-", "user-log-" );
@files    = ( );

require "modules/read_setup.pl";
( $DIR_NAME, $DATE, $ROC_SERIAL, $ROC_LAYER, $ROC_TYPE, $DCS_SERIAL, $WITH_PIPES, $INITIAL, $TRD_FEE ) = read_setup();

chdir( $DIR_NAME );

# Collect all file names
foreach( @prefixes ) {
    @files = ( @files, search_newest_file( $_ ));
}

printf("# %d files to be processed.\n", $#files + 1 );

foreach( @files ) {
    printf("# filename: %s\n", $_ );
    open( F, "$_" ) || die "file $_ could not be open. Something is strange.";
    while( <F> ) {
	if( /gatedb:/ ) {
	    print;
	}
    }
    close (F);
}





exit(0);

sub search_newest_file
{
    my ($prefix) = @_;
    $f = `ls -1 $prefix\* 2> /dev/null | tail -1 `;
    if( $f =~ /^$prefix/ ) { chop $f; return $f };
    return ( );
}

# EOF
