#!/usr/bin/perl
# $Header: /global/src//etc/cron.daily/RCS/cl-anacron-cleanbigtmp,v 1.41 2017/04/12 14:39:26 pb22 Exp $

# Called by cron from /etc/cron.daily/ on sec=sys server to clear bigtmp; installed as cl-anacron-cleanbigtmp package

@cmd = ("rm");
shift, unshift (@cmd, "echo") if (@ARGV) && ($ARGV[0] eq "-v");

$time= $ENV{'DAYS'} || 1;
$size= $ENV{'SIZE'} || 512;

@dirs = @ARGV ? @ARGV : "/anfs/bigtmp";

chop($hostname = `hostname`);

$exclude='\.|\.\.|\.snapshot|hardwarelab-volatile';


foreach $dir (@dirs) {
	chdir($dir) || next;
	opendir(TMP, ".") || next;
	foreach $file (grep(!/^($exclude)$/, readdir(TMP))) {
		lstat($file);
		if (-d _) {
			if (-C _ > $time) { system @cmd, "-rf", "./$file"; }
		}
		elsif ((-s _ > $size && -A _ > $time) || (-A _ > 10*$time)) { system @cmd, "-f", "./$file"; }
	}
	closedir(TMP);
}
