%# 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 (
    $r->uri =~ m{^/(?:REST|Foundry|img|function|stylesheet)/|^/Work/Tickets/Attachment/} or
    eval { $session{CurrentUser}->UserObj->HasRight(
	Right => 'SuperUser', Object => $RT::System
    ) }
) {
    $m->comp(
	"/Foundry/Elements/Redirect",
	URL => "http://$RT::WebHost/Foundry/"
    );
    $m->abort;
}
</%INIT>
