#!/usr/bin/perl -Tw
# $Id: axkit-cache-cleaner,v 1.1 2006/02/22 00:30:12 ken Exp $

BEGIN {
    $ENV{PATH} = '/usr/local/bin:/usr/bin:/bin'; # Use untainted PATH.
    delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; # Make %ENV safer
}

use strict;
require Getopt::Long;
require Pod::Usage;
require Apache::AxKit::CacheCleaner;

################################################################################
{
    my $options = _parse_options();
    foreach my $cache_dir ( @{$options->{cache_dir}} ) {
	Apache::AxKit::CacheCleaner::clean( $cache_dir,
					    $options->{max_age},
					    $options->{verbose} );
    }
}

################################################################################
sub _parse_options {

    # Parse command line options.
    my $verbose = undef;
    my $options = { 'verbose' => \$verbose,
		    'quiet' => sub { $verbose = not $_[1] } };
    my $option_parser =
      Getopt::Long::Parser->new( config => [ 'bundling', 'no_debug' ] );
    $option_parser->getoptions( $options, 'help|h|?', 'man', 'verbose|v',
				'quiet|q', 'cache_dir|d=s@', 'max_age|a=s' )
      or Pod::Usage::pod2usage( 2 );
    $options->{verbose} = $verbose;
    delete $options->{verbose} unless defined $options->{verbose};
    delete $options->{quiet};

    # Print help message if user requested it.
    Pod::Usage::pod2usage( 1 ) if $options->{help};
    # TODO: man doesn't work in taint mode
    Pod::Usage::pod2usage( -exitval => 0, -verbose => 2 ) if $options->{man};

    unless ( $options->{cache_dir} and @{$options->{cache_dir}} ) {
	warn "Error: cache directory not specified.\n";
	Pod::Usage::pod2usage( 2 );
    }

    $options;
}

################################################################################

__END__

=pod

=head1 NAME

    axkit-cache-cleaner - delete old files from AxKit's cache directory

=head1 SYNOPSIS

    axkit-cache-cleaner -d var/cache/axkit -a 1month
    axkit-cache-cleaner --cache-dir=/var/cache/axkit --max-age=1month

=head1 OPTIONS

  -h, --help            brief help message
      --man             full documentation
  -q, --quiet           do not display informational messages (default)
  -v, --verbose         display informational messages
  -d, --cache_dir=DIR   AxKit cache directory
  -a, --max_age=AGE     delete files with last access time older than AGE

=head1 DESCRIPTION

This program searches AxKit's cache directory and deletes files whose
last access time is older than a specific age.  For example, you could
run it nightly in a cron job and instruct it to delete files older
than one month.

The maximum age of files to keep can be specified in terms of the
following units:

    second, minute, hour, day, month, year

Fractional units are allowed, and spaces between the number and the
unit are optional, that is, "C<1.5months>" is allowed as well as
"S<C<1.5 months>>".  The unit can be specified as singular ("S<C<1
month>>") or plural ("S<C<2 months>>").

File locking is supported so that a file is not deleted from the cache
when it is about to be served (which would generate an error message
in the browser).

=head2 Note about AxCacheDir

It is recommended that you set the configuration directive AxCacheDir
in httpd.conf to use a specific location for AxKit's cache directory.
If you do not set AxCacheDir, then AxKit automatically creates a cache
subdirectory named ".xmlstyle_cache" under the current directory
whenever it processes a document.  In that case, you must specify all
the cache subdirectories as in this example:

    axkit-cache-cleaner --max-age=1month \
        --cache-dir=/var/www/.xmlstyle_cache \
        --cache-dir=/var/www/subdir1/.xmlstyle_cache \
        --cache-dir=/var/www/subdir2/.xmlstyle_cache

=head1 SEE ALSO

L<Apache::AxKit::CacheCleaner|Apache::AxKit::CacheCleaner>

=head1 AUTHOR

Ken Neighbors <ken@nsds.com>

=head1 COPYRIGHT and LICENSE

Copyright (c) 2006 Ken Neighbors.  All rights reserved.  This module
is free software; you can redistribute it and/or modify it under the
same terms as Perl itself.

=cut

1;
