###
###  Copyright 2000-2007 University of Illinois Board of Trustees
###  All rights reserved. 
###
###  PSGConf::Action::MkDir - directory action type for psgconf
###
###  Campus Information Technologies and Educational Services
###  University of Illinois at Urbana-Champaign
###


package PSGConf::Action::MkDir;

use strict;

use File::Path;
use File::stat;

use PSGConf::Action::ChMod;
use PSGConf::Action::ChOwn;
use PSGConf::Action::ChGrp;

our @ISA = qw(PSGConf::Action::ChMod
			PSGConf::Action::ChOwn
			PSGConf::Action::ChGrp);



###############################################################################
###  constructor
###############################################################################

sub new
{
	my ($class, %opts) = @_;
	my ($self) = \%opts;

	$self->{mode} = 0755
		if (!exists($self->{mode}));

	### Call the ChMod action as well.
	$self = PSGConf::Action::ChMod::new($class, %$self);

	### Call the ChOwn action as well.
	$self = PSGConf::Action::ChOwn::new($class, %$self);

	### Call the ChGrp action as well.
	return PSGConf::Action::ChGrp::new($class, %$self);
}


###############################################################################
###  check method
###############################################################################

sub check
{
	my ($self, $psgconf) = @_;

	$self->{changed} = 1
		if (! -d $self->{name});

	### check for permission differences
	$self->PSGConf::Action::ChMod::check($psgconf);

	### check for ownership differences
	$self->PSGConf::Action::ChOwn::check($psgconf);

	### check for group differences
	$self->PSGConf::Action::ChGrp::check($psgconf);

	return $self->{changed};
}


###############################################################################
###  diff method
###############################################################################

sub diff
{
	my ($self, $psgconf) = @_;

	if (! -d $self->{name})
	{
		print "###############################################################################\n";
		print "### DIFF FOR $self->{name}\n";
		print "###          (original directory does not exist)\n";
		print "###############################################################################\n";
		print "+ MKDIR: $self->{name}\n\n";
	}

	### check for permission differences
	$self->PSGConf::Action::ChMod::diff($psgconf);

	### check for ownership differences
	$self->PSGConf::Action::ChOwn::diff($psgconf);

	### check for group differences
	$self->PSGConf::Action::ChGrp::diff($psgconf);
}


###############################################################################
###  do() method
###############################################################################

sub do
{
	my ($self, $psgconf) = @_;

	if (! -d $self->{name})
	{
		if (-l $self->{name}
		    || -e $self->{name})
		{
			if (!$self->{backup})
			{
				if (!unlink($self->{name}))
				{
					warn "\t!!! unlink('"
						     . $self->{name}
						     . "'): $!\n";
					return -1;
				}
			}
			elsif (! $psgconf->save_file($self->{name}, $self->{backup}))
			{
				return -1;
			}
		}

		mkpath($self->{name});
	}

	### check for permission differences
	$self->PSGConf::Action::ChMod::do($psgconf);

	### check for ownership differences
	$self->PSGConf::Action::ChOwn::do($psgconf);

	### check for group differences
	$self->PSGConf::Action::ChGrp::do($psgconf);

	return 1;
}


###############################################################################
###  documentation
###############################################################################

1;

__END__

=head1 NAME

PSGConf::Action::MkDir - Directory creation action class for PSGConf

=head1 SYNOPSIS

  use PSGConf::Action::MkDir;

  $psgconf->register_actions(
		PSGConf::Action::MkDir->new(
			'name'		=> '/path/to/dir',
			'mode'		=> 01777,
			...
		),
		...
	);

=head1 DESCRIPTION

The B<PSGConf::Action::MkDir> module provides a B<PSGConf> action class
for creating a directory.

The B<PSGConf::Action::MkDir> class is derived from the
B<PSGConf::Action::ChMod>, B<PSGConf::Action::ChOwn> and
B<PSGConf::Action::ChGrp> classes, but it defines/overrides
the following methods:

=over 4

=item check()

Checks to see whether the directory exists.

=item diff()

Prints a message indicating whether the directory needs to be created.

=item do()

Creates the directory, optionally saving pre-existing directory.

=back

In addition to the attributes supported by the B<PSGConf::Action>
class, the B<PSGConf::Action::MkDir> class supports the following
attributes:

=over 4

=item I<mode>

The octal mode of the directory.  Defaults to 0755.

=item I<uid>

The numeric user id of the directory.  Defaults to 0.

=item I<gid>

The numeric group id of the directory.  Defaults to 0.

=item I<backup>

A boolean flag to indicate whether a backup copy of the existing directory
will be saved.  If enabled, existing directories will be saved under their
original name but with the prefix ".<backupext>".  Default is no
backup.

=back

=head1 SEE ALSO

L<perl>

L<PSGConf>

L<PSGConf::Action::ChGrp>

L<PSGConf::Action::ChMod>

L<PSGConf::Action::ChOwn>

=cut



syntax highlighted by Code2HTML, v. 0.9.1