#!/usr/bin/perl -wl
#
#	metalog-gzip
#
#	Copyright (c) 2006 Krzysztof Krzyzaniak
#
#	You may distribute under the terms of either the GNU General Public
#	License[1] or the Artistic License[2].
#
#	[1] http://www.gnu.org/licenses/gpl.html
#	[2] http://www.perl.com/pub/a/language/misc/Artistic.html

use strict;

my $metalog_conf = "/etc/metalog.conf";
my $gzip = "/bin/gzip";
my @dirs = ();

#--- first take directories from configuration file
open(FD, "<", $metalog_conf) or die qq{Cannot open $metalog_conf}; 
my @lines = <FD>;
 
for my $line (@lines)
{
	my @tmp = $line =~ /logdir[^\=]*\=[^\"]"(.+)\"/;
	push @dirs, $tmp[0] if defined $tmp[0];	
}

#--- then compress files in these dirs (skip current and already gzipped files)
for my $dir (@dirs)
{
	opendir( DIR, $dir );
	my @files = grep { $_ =~ /^log(.+)[^\.gz]$/ } readdir DIR;
	closedir DIR;
	
	for my $file (@files)
	{				
		my @args = ( $gzip, qq{$dir/$file} );
		system(@args);
	}
}
