#!/usr/bin/perl

if (@ARGV) {	# The first arg is a pattern
	$pat = shift @ARGV;
} else {
	print STDERR "Usage: $0 pat [file]..\n";
	exit 1;
}
if (@ARGV) {	# The rest are files
	@files = @ARGV;
} else {
	@files = glob("/var/log/httpd-access.log*");
}

for $f (@files) {
	$f =~ s/[\r\s]+$//;
	next unless $f;
	print "File: $f\n";
	if ($f =~ /(.*)\.gz$/) {
		$cmd = "zcat $f |";
	} else {
		$cmd = "cat $f |";
	}
	$cmd .= "grep -i '$pat'";
	for $line (`$cmd`) {
		if ($line =~ m"$pat") {
			print $line;
		}
	}
}
