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


package PSGConf::Control::Printers;

use strict;

use PSGConf::Data::Hash;
use PSGConf::Data::List;
use PSGConf::Data::String;
use PSGConf::Data::Boolean;
use PSGConf::Action::GenerateFile::printcap;
use PSGConf::Action::lpadmin;
use PSGConf::Action::svcs::import;
use PSGConf::Control::Packages qw(_add_pkgs);
use PSGConf::Control::syslog qw(_add_syslog);


###############################################################################
###  policy methods
###############################################################################

### add inetd entry
sub _policy_add_inetd_entry
{
	my ($self, $psgconf) = @_;

	return
		if ( $psgconf->data_obj('print_enable')->equals('false') ||
			! $psgconf->data_obj('print_subsystem_type')->equals('SysV') ||
			$psgconf->data_obj('platform')->match('solaris10') );

	$psgconf->data_obj('inetd')->insert(
		{ 'printer/tcp' =>
			{ server => "/usr/lib/print/in.lpd" }
		}
	) if (!defined $psgconf->data_obj('inetd')->find('printer/tcp'));
}

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

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

	foreach $daemon ( @{$psgconf->data_obj('print_daemons')->get()} ) {
		$psgconf->data_obj('rc_scripts')->insert(
			{ $daemon => { 'state' => 'enable' }}
		);
	}
}


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

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

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

	if ($psgconf->data_obj('print_subsystem_type')->equals('printcap'))
	{
		$psgconf->register_actions(
			PSGConf::Action::GenerateFile::printcap->new(
				'name'		=> '/etc/printcap',
				'default_printer'
						=> $psgconf->data_obj('default_printer')->get(),
				'printers' 	=> $psgconf->data_obj('printers')->get()
			)
		);
	}
#	elsif ($psgconf->data_obj('print_subsystem_type')->equals('cups'))
#	{
#	}
	elsif ($psgconf->data_obj('print_subsystem_type')->equals('SysV'))
	{
		$psgconf->register_actions(
			PSGConf::Action::svcs::import->new(
				name           => 'Import Print Server SMF configurations',
				FMRI           => 'svc:/application/print/server:default',
				manifest       => '/var/svc/manifest/application/print/server.xml'
			),
			PSGConf::Action::svcs::import->new(
				name           => 'Import Print Daemon SMF configurations',
				FMRI           => 'svc:/application/print/rfc1179:default',
				manifest       => '/var/svc/manifest/application/print/rfc1179.xml'
			)
		) if ($psgconf->data_obj('platform')->match('solaris10'));

		my ($printers) = $psgconf->data_obj('printers')->get();
		$psgconf->register_actions(
			map {
				PSGConf::Action::lpadmin->new(
					name				=> $_,
					default_printer	=> $psgconf->data_obj('default_printer')->get(),
					remotehost => $printers->{$_}->{remotehost},
					device => $printers->{$_}->{device},
					model => $printers->{$_}->{model},
					protocol => $printers->{$_}->{protocol},
					options => $printers->{$_}->{options},
					content_type => $printers->{$_}->{content_type}
				)
			} sort keys %{$printers}
		);
	}
}


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

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

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

	### So that _add_pkgs knows which directives to look at
	$self->{name} = 'print';
	$self->{enable} = $self->{name} . '_enable';
	$self->{packages} = $self->{name} . '_packages';
	$self->{syslog} = 'print';
	$self->{facility} = 'lpr.info';

	$psgconf->register_data(
		print_enable		=> PSGConf::Data::Boolean->new(
						value => 'false'
					),
		print_subsystem_type => PSGConf::Data::String->new(
					value => 'printcap'
				   ),
		default_printer	=> PSGConf::Data::String->new(),
		print_packages		=> PSGConf::Data::List->new(),
		print_daemons		=> PSGConf::Data::List->new(),
		printers			=> PSGConf::Data::Hash->new(
					value_type => 'HASH'
				   ),
	);

	$psgconf->register_policy($self,
		print_enable_rc_scripts => '_enable_rc_scripts',
		print_add_packages	=> '_add_pkgs',
		print_add_syslog	=> '_add_syslog',
		print_add_inetd_entry => '_policy_add_inetd_entry'

	);

	return $self;
}


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

1;

__END__

=head1 NAME

PSGConf::Control::Printers - psgconf control class for printer configuration

=head1 SYNOPSIS

In F<psgconf_modules>:

  Control PSGConf::Control::Printers

=head1 DESCRIPTION

The B<PSGConf::Control::Printers> module provides a B<psgconf> control object
for configuring printer files, including B</etc/printcap>.  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<print_enable>

A B<PSGConf::Data::Boolean> object that indicates whether the printers
should be configured.  The default is off.

=item I<print_packages>

A B<PSGConf::Data::List> object that lists all packages to install.

=item I<print_subsystem_type>

A B<PSGConf::Data::String> object that indicates what type of printing
subsystem is being configured.  Currently, the only supported value is
C<printcap> and C<SysV>; other values will be added in the future.

=item I<default_printer>

A B<PSGConf::Data::String> object that indicates the name of the default
printer.

=item I<printers>

A B<PSGConf::Data::Hash> object that contains the entries for each
printer.  The hash key is the name of the print queue, and the hash
value is an anonymous hash containing the settings for that print queue.
The following settings are supported: 

=over 4

=item I<remotehost>

The name of the host the printer is attached to.

=item I<remotequeue>

The name of the queue on the remote host.

=item I<description>

A description of the printer.

=item I<spooldir>

The location to store the intermediate print files before sending to the 
I<remotehost>.

=item I<logfile>

The location to log print request and results.

=item I<options>

Any other options that are needed for the queue setup.

=back

=back

In addition, the constructor registers the following policy method:

=over 4

=item I<print_enable_rc_scripts>

Updates the I<rc_scripts> data object (provided by the
B<PSGConf::Control::InitScripts> module) to enable the
printer daemon, based on whether the I<print_enable>
data object is true.

=back

=item decide()

If I<print_enable> is true and I<print_subsystem_type> is set to
C<printcap>, instantiates a B<PSGConf::Control::GenerateFile::printcap>
object to generate the F</etc/printcap> file.

=back

=head1 BUGS

This module only implements the client side of printing.

Printing subsystems other than C<printcap> should be supported.

=head1 SEE ALSO

L<perl>

printcap(5)

L<PSGConf>

L<PSGConf::Control::InitScripts>

L<PSGConf::Control::Packages>

L<PSGConf::Data::Boolean>

L<PSGConf::Data::Hash>

L<PSGConf::Data::List>

L<PSGConf::Action::GenerateFile::printcap>

L<PSGConf::Action::lpadmin>

L<PSGConf::Action::svcs::import>

L<psgconf-intro>

=cut



syntax highlighted by Code2HTML, v. 0.9.1