%# Love cannot be much younger than the lust for murder.
%#                 -- Sigmund Freud
<%INIT>
$RT::Foundry = 1;

my $hostname = $r->hostname;
if ($hostname ne $RT::WebHost and $hostname =~ /^(\w+)\Q.$RT::Host\E$/i) {
    my $QueueObj = RT::Queue->new($session{CurrentUser});
    $QueueObj->LoadByUnixName($1);
    if ( $QueueObj->Id and not $QueueObj->Disabled ) {
	$m->comp(
	    "/Foundry/Elements/Redirect",
	    URL => $QueueObj->OriginObj->CustomFieldValue('RedirectURL') || "http://$RT::WebHost/Foundry/Project/?Queue=" . $QueueObj->Id
	);
	$m->abort;
    }

    $m->comp(
	"/Foundry/Elements/Redirect",
	URL => "http://$RT::WebHost/Foundry/"
    );
}

unless (
    $RT::Reentrant == 1 or
    $r->uri =~ m{^/(?:REST|Foundry|img|function|stylesheet)/|^/Work/Tickets/Attachment/} or
    eval { $session{CurrentUser}->UserObj->HasRight(
	Right => 'SuperUser', Object => $RT::System
    ) }
) {
    local $RT::Reentrant = 1;
    $m->subexec('/Foundry/index.html');
    $m->abort;
}
</%INIT>
