#!/usr/bin/perl

################################################################
# Log compressor
################################################################

my $dir = "/home/princeton_codeen/CoDeeN/cachefiles/";
my $next_log;
my $date;

for (;;) {
    $date = `date -u +%Y%m%d`; 
    chomp($date);

    open H_PIPE, "ls $dir | grep . |";
    while (<H_PIPE>) {
	chomp; # remove '\n'
	if (/access\.exlog\.(........)_(...)$/) {
	    $next_log = sprintf "%s%03d", "$dir/access.exlog.$1_", ($2 + 1);
	    if ((-e "$next_log") || (-e "$next_log.bz2")) {
		`bzip2 -f $dir/$_`;
	    } elsif (($date > $1) &&             # yesterday's log ?
		     ((-e "$dir/access.exlog.${date}_000") 
		      || (-e "$dir/access.exlog.${date}_000.bz2"))) {
		`bzip2 -f $dir/$_`;
	    }
	} elsif (/(dbg_req_redirect|framesets.log|debug_hbtwget|debug_getbig)\.(........)_(...)$/) { 
	    # remove zero-sized files
	    if (-z "$dir/$_") {
		unlink "$dir/$_";
	    } else {
		`bzip2 -f $dir/$_`;
	    }
	}
    }
    close H_PIPE;

    # wait 5 minites
    sleep(5*60);
}
