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


package PSGConf::Action::svcs::setprop;

use strict;

use PSGConf::Action;

use PSGConf::Util;

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


###############################################################################
###  check method
###############################################################################
sub check
{
     my ($self, $psgconf) = @_;
     my ($cmd, $value, $output);

	$value = $self->{value};

	$cmd = '/usr/sbin/svccfg';

	$cmd .= ' -s ' . $self->{FMRI}
		if ( $self->{FMRI} );

	$cmd .= ' listprop ' . $self->{property};
          
     if ( ! open (SVCCFG, "$cmd 2>&1|" )) {
          warn "\n\t!!! cannot run ($cmd): $!\n";
          return -1
     }
                    
     $output = <SVCCFG>;
            
     close SVCCFG;

	### Massage ${value} to remove the TYPE: from the
	### 'TYPE: value' format so our test works better.
	${value} =~ s/[^ ]*: //;
 
     if ( $output =~ /${value}$/ ) {
          return 0;
     } else {
		$self->{changed} = 1;
          return 1;
     }
}


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

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

	$cmd = '/usr/sbin/svccfg';
	$cmd .= ' -s ' . $self->{FMRI}
		if ( $self->{FMRI} );

	$cmd .= ' setprop ' . $self->{property};
	$cmd .= ' = ' . $self->{value};
          

	print "###############################################################################\n";
	print "### DIFF FOR $self->{name}\n";
	print "###############################################################################\n";
	print "+ setprop: ${cmd}\n";
	print "\n";
}


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

sub do
{
	my ($self) = @_;
	my ($cmd, $val, $res);

	$cmd = '/usr/sbin/svccfg';
	$cmd .= ' -s ' . $self->{FMRI}
          if ( $self->{FMRI} );

        $cmd .= ' setprop ' . $self->{property};

	### Escape any " or ' in value
	$val = $self->{value};
	$val =~ s/(['"])/\\$1/g;

	$cmd .= ' = ' . $val;

	$res=!(&PSGConf::Util::RunCommand(${cmd}));

	### If we failed, then return right away
	return $res if ( ! $res); 


        if ($self->{refresh})
        {
	    ### Otherwise we need to refresh the service
	    $cmd = '/usr/sbin/svcadm refresh ' .  $self->{FMRI};

	    return (!&PSGConf::Util::RunCommand(${cmd}));
        }

        return 1;
}


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

1;

__END__

=head1 NAME

PSGConf::Action::svcs::setprop - setting svcs property action type for psgconf

=head1 SYNOPSIS

  use PSGConf::Action::svcs::setprop;

  $psgconf->register_actions(
		PSGConf::Action::svcs::setprop->new(
			'name'		=> 'message about property being set',
			'FMRI'		=> 'The FMRI to work on ...',
			'property'	=> 'property to set ...',
			'value'		=> 'value to set the property to ...',
		),
		...
	);

=head1 DESCRIPTION

The B<PSGConf::Action::svcs::setprop> module provides a B<PSGConf> action
class for configuring the System Management Facility .

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

=over 4

=item check()

Checks to see what value the I<property> is set to, and compares
it to I<value>.  If they are different, it sets the changed flag
so that later on, the C<svccfg setprop> and C<svcadm refresh> 
will get run.

=item diff()

Prints a message indicating that C<svccfg setprop> will be executed.

=item do()

Actually executes the C<svccfg setprop> and C<svcadm refresh> commands.

=back

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

=over 4

=item I<FMRI>

The Fault Management Resource Identifier where the property resides.

=item I<property>

The property to be changed

=item I<value>

The value to change the property to.

=item I<refresh>

true if the service should be refreshed via svcadm

=back

Note that the I<name> attribute will only be used to print the
"checking" message when the check() method is called.

=head1 SEE ALSO

C<CE<lt>smf(5)E<gt>>

C<CE<lt>svccfg(1m)E<gt>>

L<perl>

L<PSGConf>

L<PSGConf::Action>

=cut



syntax highlighted by Code2HTML, v. 0.9.1