###
###  Copyright 2000-2007 University of Illinois Board of Trustees
###  All rights reserved. 
###
###  PSGConf::Action::RPMImportGPGKey - Import a GPG key into the RPM db action class for PSGConf
###
###  Campus Information Technologies and Educational Services
###  University of Illinois at Urbana-Champaign
###

###############################################################################
###  Init
###############################################################################

package PSGConf::Action::RPMImportGPGKey;

use strict;

use PSGConf::Action::File;

use PSGConf::Util;

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


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

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

	$self->_set_tmpfile($psgconf);

	$cmd = $self->{rpm_cmd} . ' -q --quiet gpg-pubkey-' . $self->{key};
	$res=&PSGConf::Util::RunCommand($cmd, 1);

	$self->{changed} = ($res == -1)? 1: $res;

	return $self->{changed};
}


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

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

	print "###############################################################################\n";
	print "### DIFF FOR GPG KeyID $self->{key}\n";
	print "###############################################################################\n";
	print "+ rpm --import $self->{key}\n";
	print "\n";
}


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

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

	### First fetch the key from a GPG key server
	if ( exists $self->{keyserver} ) {
		$cmd = $self->{gpg_cmd} . ' -q --recv-keys --keyserver ' 
			. $self->{keyserver} . ' '
			. $self->{key} . ' > /dev/null 2>&1';
		$res=&PSGConf::Util::RunCommand($cmd);
	} else {
		warn "\n\tNeed to set GPG keyserver\n";
		$res=-1;
	}

	### Now export it from GPG
	if ( ! $res ) {
		$cmd = $self->{gpg_cmd} . ' -q --export --armour ' 
			. $self->{key} . ' > '
			. $self->{tmpfile} . ' 2> /dev/null';
		$res=&PSGConf::Util::RunCommand($cmd);
	}

	### Finally import it into rpm
	if ( ! $res ) {
		$cmd = $self->{rpm_cmd} . ' --import '
				. $self->{tmpfile} . ' > /dev/null 2>&1';
		$res=&PSGConf::Util::RunCommand($cmd);
	}

	### remove tmpfile if applicable
	if (-e $self->{tmpfile}
	    && $psgconf->{rm_tmpfiles}
	    && !unlink($self->{tmpfile})) {
		warn "\t!!! unlink('$self->{tmpfile}'): $!\n";
	}

	return $res;
}


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

1;

__END__

=head1 NAME

PSGConf::Action::RPMImportGPGKey - Import a GPG key into the RPM db action class for PSGConf

=head1 SYNOPSIS

  use PSGConf::Action::RPMImportGPGKey;

  $psgconf->register_actions(
		PSGConf::Action::RPMImportGPGKey->new(
			'name'		=> 'Descriptive title for verbose mode',
			'key'		=> 'Key ID to import',
			'gpg_cmd'		=> 'Location of the GPG command',
			'keyserver'	=> 'GPG/PGP Keyserver to get the key from',
			'rpm_cmd'		=> 'Location of the RPM command'
		),
		...
	);

=head1 DESCRIPTION

The B<PSGConf::Action::RPMImportGPGKey> module provides a B<PSGConf> action
class for importing a GPG Key into the RPM database.

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

=over 4

=item check()

Checks the I<pkg_install_list> hash to see if I<key> is in the list.
If not, it sets the change flag to be installed.

=item diff()

Prints a message indicating that import needs to happen.

=item do()

Does the import in 3 steps.  First it downloads the key from the
GPG/PGP keyserver I<keyserver>.  Second step is to export the key
from GPG to a ascii file that I<rpm> can read.  The final step
is to run the I<rpm --import keyfile>.

=back

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

=over 4

=item I<name>

Attribute is the descriptive name for verbose mode operation.

=item I<key>

Attribute is the Key ID to download from the GPG Key servers.

=item I<gpg_cmd>

Attribute is the location of the GPG command.

=item I<keyserver>

Attribute is the Keyserver to download I<key> from.

=item I<rpm_cmd>

Attribute is the location of the rpm command.

=back

=head1 SEE ALSO

L<perl>

L<PSGConf>

L<PSGConf::Action>

L<PSGConf::Util>

=cut



syntax highlighted by Code2HTML, v. 0.9.1