#! /usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2008 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

# @(#)76   1.3   src/rsct/trace/chkspool.perl, trace, rsct_rfos, rfos0838a 7/2/08 15:49:26
# Search on "sub help" and/or "sub usage" for script description.

use Getopt::Long;
use Fcntl ':mode';
use Time::Local;

my $spool_dir, $days_limit, $megabytes_limit, $help;

if (!GetOptions("spool_dir=s" => \$spool_dir, "days_limit=i" => \$days_limit, "megabytes_limit=i" => \$megabytes_limit, "help" => \$help, "h" =>\$help))
{
	usage(1);
}

if (undef ne $help)
{
	help();
}

if (undef eq $spool_dir)
{
	usage(1);
}

if (undef eq $days_limit)
{
	if (undef eq $megabytes_limit)
	{
		usage(1);
	}

	# handle megabytes_limit

	my $total_spool_dir_file_bytes = total_file_bytes($spool_dir);

	my @spool_files = `lstrsp --spool_dir $spool_dir --show_file_bytes`;

	foreach $line (@spool_files)
	{
		chomp $line;
		my ($file_bytes, $spool_file) = split " ", $line, 2;

		$total_spool_dir_file_bytes -= $file_bytes;

		if ($total_spool_dir_file_bytes < $megabytes_limit*1024*1024)
		{
			exit 0;
		}

		printf "removing %d %s\n", $file_bytes, $spool_file;
		system("rm -f $spool_file");
	}

	# TODO: remove empty directories?
}
else
{
	if (undef ne $megabytes_limit)
	{
		usage(1);
	}

	my ($second, $minute, $hour, $day, $month, $year, $wday, $yday, $isdst) = localtime(todays_midnight_epoch_seconds() - $days_limit*24*60*60);

	my $lstrsp_command = sprintf("lstrsp --spool_dir %s -to %s", $spool_dir, sprintf("%.2u-%.2u-%.4u\n", $month + 1, $day, $year + 1900));

	open LSTRSP, "$lstrsp_command |" or die sprintf("%s: %d: cannot pipe lstrsp command\n", $0, __LINE__);

	while (<LSTRSP>)
	{
		chomp;
		printf "removing %s\n", $_;
		system("rm -f $_");
	}
}

exit 0;

# functions

sub help
{
	usage();

	print STDERR <<HELP;

The chkspool utility manages a trace spool directory by permitting
pruning of spool files created before a number of days, or per a
maximum spool directory size quota in megabytes.

Examples:

# prune spool files older than 20 days
> chkspool --spool_dir /tmp/TraceSpool --days_limit 20

# prune spool files until the spool_dir is 100 megabytes
> chkspool --spool_dir /tmp/TraceSpool --megabytes_limit 100
HELP

	exit 0;
}

sub usage
{
	print STDERR <<USAGE;

Usage:
chkspool --spool_dir <path>
         --days_limit <number> | --megabytes_limit <number>
         [--help]
USAGE

	if (undef ne $_[0])
	{
		exit $_[0];
	}
}

sub todays_midnight_epoch_seconds
{
	my ($current_second, $current_minute, $current_hour, $current_day, $current_month, $current_year, $wday, $yday, $isdst) = localtime(time);
	return timelocal(0, 0, 0, $current_day, $current_month, $current_year);
}

# arguments: month (1-12), day (1-31), year
sub midnight_epoch_seconds
{
	# note that we subtract 1 from the 1-12 month here to fit
	# timelocal()'s argument requirements
	return timelocal(0, 0, 0, $_[1], $_[0]-1, $_[2]);
}

sub total_file_bytes
{
	my $cumulative_bytes = 0;

#	open FIND, "find $_[0] -type f -ls|" or die sprintf "%d: ooops\n", __LINE__;
	open FIND, "find $_[0] -type f -ls|" or return 0;

	while (<FIND>)
	{
		my @files = split;

		$cumulative_bytes += $files[6];
	}

	close FIND;

	return $cumulative_bytes;
}
