#!/usr/bin/perl -w
# (C) 2003-2007 Willem Jan Hengeveld <itsme@xs4all.nl>
# Web: http://www.xs4all.nl/~itsme/
#      http://wiki.xda-developers.com/
#
# $Id$
#

use strict;

# this script outputs all apache access or error logs, in chronological order.
# you can specify how many logfiles to start in the past.

use POSIX;
use Getopt::Long;
if (getuid()) {
	exec "sudo $0 @ARGV";
}

my $list_errors=0;
my $list_all=0;

GetOptions(
	"e"=>\$list_errors,
	"a"=>\$list_all,
);

my $filename= $list_errors ? "error" : "access";

my @files= sort { my ($na) = ($a=~/(\d+)/); my ($nb) = ($b=~/(\d+)/); $nb <=> $na } glob("/var/log/apache/$filename.log.*.gz");
push @files, "/var/log/apache/$filename.log.1";
push @files, "/var/log/apache/$filename.log";

my $n= shift || ($list_all?$#files+1 : 2);

for (my $i=$#files-$n+1 ; $i <= $#files ; $i++) {
	if ($files[$i] =~ /\.gz$/) {
		system "zcat $files[$i]";
	}
	else {
		system "cat $files[$i]";
	}
}
