package PLP::CGI;

use strict;

our $VERSION = '1.00';

# CGI initializer: parses PATH_TRANSLATED
sub init {
	$PLP::print = 'print';
	
	my $path = $ENV{PATH_TRANSLATED};
	$ENV{PLP_NAME} = $ENV{PATH_INFO};
	my $path_info;
	while (not -f $path) {
		if (not $path =~ s/(\/+[^\/]*)$//) {
			print STDERR "PLP: Not found: $ENV{PATH_TRANSLATED} ($ENV{REQUEST_URI})\n";
			PLP::error(undef, 404);
			exit;
		}
		my $pi = $1;
		$ENV{PLP_NAME} =~ s/\Q$pi\E$//;
		$path_info = $pi . $path_info;
	}
	
	if (not -r $path) {
		print STDERR "PLP: Can't read: $ENV{PATH_TRANSLATED} ($ENV{REQUEST_URI})\n";
		PLP::error(undef, 403);
		exit;
	}

	delete @ENV{
		qw(PATH_TRANSLATED SCRIPT_NAME SCRIPT_FILENAME PATH_INFO),
		grep /^REDIRECT_/, keys %ENV
	};

	$ENV{PATH_INFO} = $path_info if defined $path_info;
	$ENV{PLP_FILENAME} = $path;
	my ($file, $dir) = File::Basename::fileparse($path);
	chdir $dir;

	$PLP::code = PLP::source($file, 0, undef, $path);
}

# This is run by the CGI script. (#!perl \n use PLP; PLP::everything;)
#sub everything {
#	clean();
#	cgi_init();
#	start();
#}

1;

