###
###  Copyright 2000-2007 University of Illinois Board of Trustees
###  All rights reserved. 
###
###  Inittab.pm - /etc/inittab control module for psgconf
###
###  Campus Information Technologies and Educational Services
###  University of Illinois at Urbana-Champaign
###


package PSGConf::Control::Inittab;

use strict;

use PSGConf::Data::Boolean;
use PSGConf::Data::Table;
use PSGConf::Data::List;

use PSGConf::Action::GenerateFile::Literal;
use PSGConf::Action::RunCommand;


###############################################################################
###  Policy methods
###############################################################################

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

	return
		if ( $psgconf->data_obj('inittab_enable')->equals('false') );

	foreach $row ( @{$psgconf->data_obj('inittab')->get()} ) {
		warn "Invalid Action for inittab entry $row->[0] ($row->[2])\n"
			if ( length $row->[2] &&
				! grep /^$row->[2]$/, @{$psgconf->data_obj('inittab_actions')->get()} );
	}
}

###############################################################################
###  decide() method
###############################################################################

sub decide
{
	my ($self, $psgconf) = @_;
	my ($content, $row);

	return
		if ( $psgconf->data_obj('inittab_enable')->equals('false') );

	### Some platforms require that initdefault and sysinit be the
	### first two entries in the file.
	foreach $row ( @{$psgconf->data_obj('inittab')->get()} ) {

		$content .= join (':', @$row) . "\n";
	}

	$psgconf->register_actions (
		PSGConf::Action::GenerateFile::Literal->new(
			name			=> '/etc/inittab',
			description	=> "script for init",
			comment_str	=> $psgconf->data_obj('inittab_comment_str')->get(),
			content		=> $content
		),
		PSGConf::Action::RunCommand->new(
			name			=> 'Reread /etc/inittab',
			command		=> $psgconf->data_obj('init_refresh_cmd')->get(),
			filename		=> [ '/etc/inittab' ]
		)
	);
}


###############################################################################
###  Constructor
###############################################################################

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

	$self = {};
	bless($self, $class);

	$self->{name} = 'inittab';

	$psgconf->register_data(
		inittab_enable		=> PSGConf::Data::Boolean->new(
							value 	=> 'false'
						),
		inittab_actions	=> PSGConf::Data::List->new(),
		inittab_comment_str	=> PSGConf::Data::String->new(
							value 	=> "###"
						),
		inittab			=> PSGConf::Data::Table->new(),
		init_refresh_cmd	=> PSGConf::Data::String->new(
							'value_abspath' => 1,
							value 	=> "/sbin/telinit q"
						)
	);

	$psgconf->register_policy($self,
		inittab_check_actions	=> '_policy_check_actions',
	);

	return $self;
}


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

1;

__END__

=head1 NAME

PSGConf::Control::Inittab - psgconf control class for /etc/inittab

=head1 SYNOPSIS

In F<psgconf_modules>:

  Control PSGConf::Control::Inittab

=head1 DESCRIPTION

The B<PSGConf::Control::Inittab> module provides a B<psgconf>
control object for configuring the F</etc/inittab> file.  It
supports the following methods:

=over 4

=item new()

The constructor.  Its parameter is a reference to the B<PSGConf>
object.  It registers the following data objects:

=over 4

=item I<inittab_enable>

A B<PSGConf::Data::Boolean> flag to determine whether or not we should
administer the F</etc/inittab> file.

=item I<inittab_actions>

A B<PSGConf::Data::List> of actions that are valid for the F</etc/inittab>
file.

=item I<inittab_comment_str>

A B<PSGConf::Data::String> to use as the comment character in the
F</etc/inittab> file.  Defaults to C<###>.  Setting it to C<undef>
will remove any comments in the file.

=item I<inittab>

A B<PSGConf::Data::Table> of the entries to put into the F</etc/inittab>
file.  Order is preserved.

=item I<init_refresh_cmd>

A B<PSGConf::Data::String> for a command to run after the F</etc/inittab>
file has been updated.  Defaults to C</sbin/telinit q>.

=back

The constructor also registers the following policy methods:

=over 4

=item I<inittab_check_actions>

Compares all actions in the C<inittab> hash with the actions listed in
the <inittab_actions> list.

=back

=item decide()

Instantiates and registers action objects, as follows:

=over 4

=item *

Registers B<PSGConf::Action::GenerateFile::Literal> action objects to 
create the F</etc/initab> file from the contents of I<inittab>.

=item *

Registers B<PSGConf::Action::RunCommand> action objects to run the
I<init_refresh_cmd>.

=back

=back

=head1 SEE ALSO

L<perl>

L<PSGConf>

L<PSGConf::Action::GenerateFile::Literal>

L<PSGConf::Action::RunCommand>

L<PSGConf::Data::Boolean>

L<PSGConf::Data::Table>

L<PSGConf::Data::List>

L<psgconf-intro>

=cut



syntax highlighted by Code2HTML, v. 0.9.1