# $Id: openlogfile.pl,v 1.2 2004/11/21 02:20:11 root Exp $
#
# subroutine for opening a log file regardless of how it is compressed
#
# (C) Copyright Craig Sanders <cas@taz.net.au>, 2001
#
# this routine is licensed under the terms of the GNU General Public 
# License (GPL)
#
# the latest version can always be found at http://taz.net.au/postfix/scripts

sub open_log_file {
    # open log files whether they're compressed or not

    my $zcat = "/bin/zcat" ;
    my $gzcat = "/bin/zcat" ;
    my $bzcat = "/usr/bin/bzcat" ;

    my ($file) = shift ;

    if ($file eq "-") {
        open(FILE, "<&=STDIN");
    } else {
        my $mm = new File::MMagic;
        my $res = $mm->checktype_filename($file);

        #print "$file: $res\n" ;

        if ($res eq "application/x-compress") {
                open(FILE,"$zcat $file|") || die "couldn't run '$zcat $file': $!" ;
        } elsif ($res eq "application/x-gzip") {
                open(FILE,"$gzcat $file|") || die "couldn't run '$gzcat $file': $!" ;
        } elsif ($res eq "application/x-bzip2") {
                open(FILE,"$bzcat $file|") || die "couldn't run '$bzcat $file': $!" ;
        } else {
                open(FILE,$file) || die "couldn't open $file: $!" ;
        }  ;
    } ;

    return *FILE ;
} ;

return 1;
