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


package PSGConf::Control::Apache::FastCGI;

use strict;

use PSGConf::Data::Boolean;
use PSGConf::Data::List;
use PSGConf::Data::String;
use PSGConf::Control::Packages qw(_add_pkgs);


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

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

	$psgconf->data_obj('www_fcgi_enable')->set('false')
		if ($psgconf->data_obj('www_enable')->equals('false'));
}


### add Apache modules
sub _policy_add_modules
{
	my ($self, $psgconf) = @_;

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

	### add modules
     $psgconf->data_obj('www_modules')->append_row(
          [ 'fastcgi' ]
     ) if (! $psgconf->data_obj('www_modules')->find_row({0 => 'fastcgi'}));
}


### update Apache config
sub _policy_add_config
{
	my ($self, $psgconf) = @_;
	my ($wrapper, $ipc_dir, $text);

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

	$wrapper = $psgconf->data_obj('www_fcgi_wrapper_path')->get();
	$ipc_dir = $psgconf->data_obj('www_fcgi_ipc_dir')->get();
	$text = "
<IfModule mod_fastcgi.c>
    AddHandler		fastcgi-script		.fcgi
    FastCgiIpcDir	$ipc_dir";
	$text .= "
    FastCgiWrapper	$wrapper"
		if (defined($wrapper));
	$text .= '
</IfModule>

';
	$psgconf->data_obj('www_main_server_config')->prepend($text);

	### Add index.fcgi to the DirectoryIndex list
	if ( !grep /index\.fcgi/,
		$psgconf->data_obj('www_main_server_options')->find('DirectoryIndex')) {
		$text = 'index.fcgi ' 
			. $psgconf->data_obj('www_main_server_options')->find('DirectoryIndex');
		$psgconf->data_obj('www_main_server_options')->insert({
			DirectoryIndex => $text
		});
	}
	
}


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

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

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

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

	$psgconf->register_data(
		www_fcgi_enable		=> PSGConf::Data::Boolean->new(
						value => 'false'
					),
		www_fcgi_packages		=> PSGConf::Data::List->new(),
		www_fcgi_ipc_dir	=> PSGConf::Data::String->new(
						value_abspath => 1,
						value => '/var/lib/httpd'
					),
		www_fcgi_wrapper_path	=> PSGConf::Data::String->new(
						value_abspath => 1,
						value => '/usr/local/sbin/suexec'
					)
	);

	$psgconf->register_policy($self,
		fcgi_check_apache	=> '_policy_check_apache',
		fcgi_add_packages	=> '_add_pkgs',
		fcgi_add_apache_modules	=> '_policy_add_modules',
		fcgi_add_apache_config	=> '_policy_add_config'
	);

	return $self;
}


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

1;

__END__

=head1 NAME

PSGConf::Control::Apache::FastCGI - psgconf control class for Apache mod_fastcgi

=head1 SYNOPSIS

In F<psgconf_modules>:

  Control PSGConf::Control::Apache::FastCGI

=head1 DESCRIPTION

The B<PSGConf::Control::Apache::FastCGI> module provides a B<psgconf>
control object for configuring the Apache F<mod_fastcgi> module.
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<www_fcgi_enable>

A B<PSGConf::Data::Boolean> value, indicating whether FastCGI should
be configured.  The default is no.

=item I<www_fcgi_packages>

A B<PSGConf::Data::List> value, indicating what packages need to be 
installed.

=item I<www_fcgi_ipc_dir>

A B<PSGConf::Data::String> object containing the absolute path to the
FastCGI IPC directory.  The default is F</var/lib/httpd>.

=item I<www_fcgi_wrapper_path>

A B<PSGConf::Data::String> object containing the absolute path to the
FastCGI wrapper binary.  The default is F</usr/local/sbin/suexec>.
If unset, the C<FastCgiWrapper> config directive is not added.

=back

The constructor also registers the following policy methods:

=over 4

=item I<fcgi_check_apache>

If the I<www_enable> data object (supplied by the
B<PSGConf::Control::Apache> module) is not set, then unsets the
I<www_fcgi_enable> object.

=item I<fcgi_add_packages>

Adds F<mod_fastcgi> and F<FCGI> to the list of requested packages.
(This uses the I<pkg_install_list> config object supplied by
B<PSGConf::Control::Packages>.)

=item I<fcgi_add_apache_modules>

Adds the F<mod_fastcgi> module to the I<www_modules> data object
(provided by the B<PSGConf::Control::Apache> module).

=item I<fcgi_add_apache_config>

Adds some reasonable defaults to the I<www_main_server_config> data object
(provided by the B<PSGConf::Control::Apache> module).

=back

=back

=head1 SEE ALSO

L<perl>

L<PSGConf>

L<PSGConf::Control::Apache>

L<PSGConf::Control::Packages>

L<PSGConf::Data::Boolean>

L<PSGConf::Data::List>

L<PSGConf::Data::String>

L<psgconf-intro>

=cut



syntax highlighted by Code2HTML, v. 0.9.1