#! /usr/bin/perl

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.

# Tue Jan 13 01:47:57 MET 1998: Martin Schulze <joey@infodrom.north.de>
#	Fixed typo.
#
#	Modified logfile detection routine to take care of double
#	listed logfiles.  It does this by using a hash instead of
#	immediate printout.  Thanks to Roman Hodek <roman@debian.org>
#	for providing an appropriate patch.

$conf = "/etc/syslog.conf";
$opt_daily = 1;
$opt_all = 0;
$opt_auth = 0;
$opt_ign_size = 0;
$opt_news = 0;

sub usage
{
    print STDERR
"
Debian GNU/Linux syslogd-listfiles =VER=.  Copyright (C) 1997
Martin Schulze.  This is free software; see the GNU General Public Licence
version 2 or later for copying conditions.  There is NO warranty.

Usage: syslogd-listfiles <options>
Options: -f file	specifies another syslog.conf file
         -a | --all	list all files (including news)
         --auth		list all files containing auth.<some prio>
	 --ignore-size  don't rotate files which got too large (default: 2MB)
         --news		include news logfiles, too
         -w | --weekly	use weekly pattern instead of daily
";
}

while (@ARGV) {
    $_=shift(@ARGV);
    if (m/^-f$/) {
	$conf = shift(@ARGV);
    } elsif (m/^(--weekly|-w)$/) {
	$opt_daily = 0;
    } elsif (m/^(-a|--all)$/) {
	$opt_all = 1;
    } elsif (m/^--auth$/) {
	$opt_auth = 1;
    } elsif (m/^--ignore-size/) {
	$opt_ign_size = 1;
    } elsif (m/^--news$/) {
	$opt_news = 1;
    } else {
	&usage();exit (0);
    }
}

open (C, $conf) || die "Can't open $conf, $!";
while (<C>) {
    next if (/^(\#|$)/);
    chop;

    s/\s*(\S.*)$/$1/ if ($line);

    $line .= $_;
    chop ($line) if (/\\$/);
    if (!/\\$/) {
	$line =~ s/\s+/\t/;
	$line =~ s/\t-/\t/;
	push (@lines, $line) if ($line =~ /\t\/(?!dev\/)/);
	$line = "";
    }
}
close (C);

foreach $line (@lines) {
    ($pat,$file) = split (/\t/,$line);

    # handled by news.daily from INN
    next if (!$opt_news && ($pat =~ /news\.(crit|err|notice)/));

    if ($opt_all) {
	$output{$file} = 1;
    } elsif ($opt_auth) {
	$output{$file} = 1 if ($pat =~ /auth[^\.]*\.(?!none).*/);
    } else {
	$i = ($pat =~ /\*\.\*/);
	$output{$file} = 1 if (($i && $opt_daily) || (!$i && !$opt_daily)
			       || (!$opt_ign_size && (stat $file)[7] >= 2097152 && $opt_daily)); # 2MB
    }
}

foreach $file (keys (%output)) {
    print $file . "\n";
}
