%# $File: //depot/RG/rg/html/Elements/SetupSessionCookie $ $Author: autrijus $
%# $Revision: #8 $ $Change: 4231 $ $DateTime: 2003/02/14 06:14:29 $

<%init>
my %cookies = CGI::Cookie->fetch();
my $pm = "$session_class.pm"; $pm =~ s|::|/|g; require $pm;
my $dsn = 'dbi:mysql:database=rg';
$dsn .= ';port=8285' if ($^O eq 'MSWin32');

eval {
    tie %HTML::Mason::Commands::session, $session_class, (
	$cookies{'RG_SID'} ? $cookies{'RG_SID'}->value() : undef
    ), {
	DataSource => $dsn,
	UserName   => 'root',
	LockDataSource => $dsn,
	LockUserName   => 'root',
    }
};

if ( $@ ) {
    if ( $@ =~ /Object does not/i ) {
	tie %HTML::Mason::Commands::session, $session_class, undef, {
	    DataSource => $dsn,
	    UserName   => 'root',
	    LockDataSource => $dsn,
	    LockUserName   => 'root',
	};
	undef $cookies{'RG_SID'};
    } else {
	# This means that we got a different error or we were
	# attempting to create a new session from scratch.
	die $@;
    }
}

if ( !$cookies{'RG_SID'} ) {
    my $cookie = CGI::Cookie->new(
	-name  => 'RG_SID',
	-value => $session{_session_id},
	-path  => '/',
    );

    $r->header_out('Set-Cookie', $cookie->as_string);
} 

$r->header_out('Pragma'		=> 'no-cache');
$r->header_out('Cache-control'	=> 'no-cache');

return();
</%init>
<%args>
$session_class => 'Apache::Session::MySQL';
</%args>
