%# One way to stop a runaway horse is to bet on him.
<& /Foundry/Elements/Header,
    Title => $QueueObj->Name,
    ($action ? (Action => $action) : ()),
    ($method ? (Method => $method) : ()),
&>
<input type="hidden" name="Queue" value="<% $ARGS{Queue} %>">

<br>
<table width="97%" border="0" cellspacing="0" cellpadding="0" align="center">
  <tr> 
    <td><& /Foundry/Elements/Tabs, Text => $m->base_comp->attr('Tab2'), Queue => $ARGS{Queue}, Skips => \%Skips &></td>
  </tr>
  <tr> 
    <td valign="top"> 
      <table border="0" cellspacing="0" cellpadding="4" class="poptable" width="100%">
        <tr> 
          <td valign="top"> 
% $m->call_next(%ARGS, QueueObj => $QueueObj, ProjectRole => $ProjectRole) unless ($r->uri =~ m{/Admin/} and !$ProjectRole) or ($r->uri =~ m{/Download/} and $DownloadRole < 0);
	  </td>
	</tr>
      </table>
<br>

<& /Foundry/Elements/Footer &>
<%INIT>
my $QueueObj = RT::Queue->new($session{'CurrentUser'}); 
$ARGS{Queue} = $1 if $r->uri =~ m{/Project/\w+/(\d+)/};
$QueueObj->Load($ARGS{Queue}||=$session{LastQueue}||=1);
$ARGS{Queue} = $session{LastQueue} = $QueueObj->Id || $session{LastQueue};
my $s = "?Queue=$ARGS{Queue}";

@RT::Tabs = (
    'Summary'		=> '/Foundry/Project/' . $s,
#   'News'		=> '/Foundry/Project/News/' . $s,
    'Tracker'		=> '/Foundry/Project/Tracker/' . $s,
    'Forums'		=> '/Foundry/Project/Forum/' . $s,
    'Wiki'              => "/Foundry/Project/Wiki/$ARGS{Queue}/",
    'Version Control'	=> '/Foundry/Project/Source/' . $s,
    'Downloads'		=> '/Foundry/Project/Download/' . $s,
    'Citations'		=> '/Foundry/Project/Citations/' . $s,
    'References'	=> '/Foundry/Project/References/' . $s,
);

my $UserObj = $session{CurrentUser}->UserObj;
my $IsAdmin = $QueueObj->HasAdminCc($UserObj)
           || $UserObj->HasRight( Right => 'SuperUser', Object => $RT::System );

my $ProjectRole = (
    $IsAdmin ? 'admin' : ($QueueObj->HasCc($UserObj) ? 'member' : '')
);
my $DownloadRole = $ProjectRole ? 2 : ($UserObj->Name ne 'guest') ? 1 : 0;

if ($QueueObj->DownloadACL('role') gt "$DownloadRole-zzz-very-unreadable") {
    # No download for you!
    splice( @RT::Tabs, -2 );
    $DownloadRole = -1;
}

my %Skips;
if ($ProjectRole) {
    push @RT::Tabs, ( 'Project Admin' => '/Foundry/Project/Admin/' . $s );
}
elsif ( $RT::SkipEmptyTabs ) {
    my $UnixName = $QueueObj->OriginObj->CustomFieldValue('UnixName') or return;
    # XXX - refactor this to somewhere else
    if (-e "$RT::VarPath/Foundry/sympa") {
	$Skips{'Forums'}++ unless glob("$RT::VarPath/Foundry/sympa/expl/$UnixName-*");
    }
    if (-e "$RT::VarPath/Foundry/kwiki") {
	require File::Compare;
	$Skips{'Wiki'}++ unless File::Compare::compare(
	    "$RT::VarPath/Foundry/kwiki/.default/database/HomePage",
	    "$RT::VarPath/Foundry/kwiki/$UnixName/database/HomePage",
	);
    }
    if (-e "$RT::VarPath/Foundry/svn") {
	if (open my $fh, "/usr/depot/rt3/var/Foundry/svn/$UnixName/db/revisions") {
	    local $/ = \32768;
	    my $rev = <$fh>;
	    close $fh;
	    $Skips{'Version Control'}++ unless index($rev, '(revision 1 2)') > -1;
	}
    }
    my $file = RT::Ticket->new($session{'CurrentUser'});
    $file->LoadByCols(
	Queue	=> $QueueObj->Id,
	Type	=> 'release',
	Status	=> 'resolved',
    );
    $Skips{'Downloads'}++ unless $file->Id;
}

my $action = $m->base_comp->attr('Action');
my $method = $m->base_comp->attr('Method');
</%INIT>
<%ATTR>
Action	=> undef
Method	=> undef
</%ATTR>
