######################################################################
#
# EPrints::Session
#
######################################################################
#
#  This file is part of GNU EPrints 2.
#  
#  Copyright (c) 2000-2004 University of Southampton, UK. SO17 1BJ.
#  
#  EPrints 2 is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  EPrints 2 is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with EPrints 2; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
######################################################################

package EPrints::TempDir;

use strict;
use warnings;

use File::Temp;
use File::Path qw/ rmtree /;

our @ISA = qw( File::Temp );

=pod

=head1 NAME

EPrints::TempDir - Create temporary directories that can automatically be removed

=head1 SYNOPSIS

	use EPrints::TempDir;

	my $dir = EPrints::TempDir->new(
		TEMPLATE => 'tempXXXXX',
		DIR => 'mydir',
		UNLINK => 1);

=head1 DESCRIPTION

This module is basically a clone of File::Temp, but provides an object-interface to directory creation.

=head1 METHODS

This module does not provide any additional methods.

=cut

use overload '""' => sub { return shift->{'dir'} };

sub new {
	my $class = shift;
	my $templ = 'eprintsXXXXX';
	if( 1 == @_ % 2 ) {
		$templ = shift;
	}
	my %args = (TEMPLATE=>$templ,@_);
	$args{dir} = File::Temp::tempdir(%args);
	return bless \%args, ref($class) || $class;
}

sub DESTROY
{
	my $self = shift;
	if( $self->{UNLINK} ) {
		rmtree($self->{dir},0,0);
	}
}

1;
