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


package PSGConf::Action::ChGrp;

use strict;

use File::stat;

use PSGConf::Action;

our @ISA = qw(PSGConf::Action);


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

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

	$self->{gid} = 0
		if (!exists($self->{gid}));

	return PSGConf::Action::new($class, %$self);
}


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

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

	return if ($self->{gid} == -1);

	$self->{stat} = stat($self->{name});

     ### check group
	$self->{changed} = 1
     	if (!defined $self->{stat} || $self->{stat}->gid != $self->{gid});

	return $self->{changed};
}


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

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

	return if ($self->{gid} == -1);

	$self->{stat} = stat($self->{name});

     if (!defined $self->{stat} || $self->{stat}->gid != $self->{gid}) 
	{
		print "###############################################################################\n";
		print "### DIFF FOR $self->{name}\n";
		print "###############################################################################\n";
		print '+ GID CHANGE: '
			. ((!defined $self->{stat})? -1: $self->{stat}->gid)
			. ' -> ' . $self->{gid} . "\n\n";
	}
}


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

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

	return if ($self->{gid} == -1);

	$self->{stat} = stat($self->{name});

	### check group
     if (!defined $self->{stat} || $self->{stat}->gid != $self->{gid})
	{
		if (!chown(-1, $self->{gid}, $self->{name}))
		{
			warn "\t!!! chgrp(\"$self->{name}\"): $!\n";
			return -1;
		}
	}

	return 1;
}

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

1;

__END__

=head1 NAME

PSGConf::Action::ChGrp - chgrp file action type for psgconf

=head1 SYNOPSIS

  use PSGConf::Action::ChGrp;

  $psgconf->register_actions(
		PSGConf::Action::ChGrp->new(
			'name'		=> '/path/to/file',
			'gid'		=> 'group of the file',
			...
		),
		...
	);

=head1 DESCRIPTION

The B<PSGConf::Action::ChGrp> module provides a B<PSGConf> action
class for changing a file's group membership.  The default gid is
C<0> and C<-1> means ignore what group the file is part of.

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

=over 4

=item check()

If the file group is correct, returns 0.  Otherwise, returns 1.

=item diff()

Displays a message indicating that the file needs to be chgrp()ed.

=item do()

Actually calls chgrp().

=back

=head1 SEE ALSO

L<perl>

C<CE<lt>chgrp(1)E<gt>>

L<PSGConf>

L<PSGConf::Action>

=cut



syntax highlighted by Code2HTML, v. 0.9.1