<h1 class="pagehead">Device Module View</h1>
<p>Note: this is a development view and is for debug purposes only.
If has been incorporated into device.html already.</p>
<ul class="mktree" id="modules">
% foreach my $item (@{$modules{root}}) {
<& SELF:dump_module, id=>$item &>
% }
</ul><hr></ul>
% foreach my $key (sort { $a <=> $b } keys %modules) {
%   if (!$printed{$key}) {
<& SELF:dump_module, id=>$key &>
%   }
% }
</ul>
<hr>
<pre>
% print Dumper(\%modules);
</pre>
<%args>
$ip         => ''   # Can be IP or hostname
</%args>
<%shared>
my %modules = {};
my %printed = {};
my $device;
</%shared>
<%init>
#XXX
use Data::Dumper;

if ($ip) {
    # Save Search Term
    my $match = $ip;
    
    # Resolve input
    my $hostname = &hostname($ip);
    my $ip = &getip($ip);

    # Check if we're an alias of another device
    my $devip = &root_device($ip);
    
    # redirect to search if no match
    $m->redirect("device_search.html?text=$match") unless $devip;

    # Grab Device INFO
    $device = sql_hash('device',
                        ['ip','extract(epoch from creation) as creation','dns',
                         'description','uptime','contact','name','location','layers',
                         'ports','mac','serial','model','ps1_type','ps2_type','ps1_status',
                         'ps2_status','fan','slots','vendor','log','os_ver','os','vtp_domain',
                         'extract(epoch from last_discover) as last_discover',
                         'extract(epoch from last_macsuck) as last_macsuck',
                         'extract(epoch from last_arpnip) as last_arpnip' ],
                        {'ip'=>$devip});

    my $modules = sql_rows('device_module',['*'],{'ip'=>$devip},0,'order by parent,pos,index') || [];
    foreach my $module (@$modules) {
        $modules{$module->{index}}{module} = $module;
        if ($module->{parent}) {
            # this is wrong.  a given parent can
            # have multiple items at a single pos value.
            # (HP may have gotten this wrong, but that's
            # reality...)
            #
            # Next wrong: some things have weird pos'
            # index |              description               |        type         | parent |  class  | pos 
            #-------+----------------------------------------+---------------------+--------+---------+-----
            #     1 | Cisco Aironet 1200 Series Access Point | cevChassisAIRAP1210 |      0 | chassis |  -1
            #     3 | PowerPC405GP Ethernet                  | cevPortFEIP         |      1 | port    |  -1
            #     2 | 802.11G Radio                          | cevPortUnknown      |      1 | port    |   0
            $module->{pos} = 0 if ($module->{pos} < 0);
            
            #$m->out(%$module."<BR>\n");
            if ($module->{pos}) {
                ${$modules{$module->{parent}}{children}{$module->{class}}}[$module->{pos}] = $module->{index};
            } else {
                push(@{$modules{$module->{parent}}{children}{$module->{class}}}, $module->{index});
            }
        } else {
            push(@{$modules{root}}, $module->{index});
        }
    }
} else {
    $m->redirect("device_search.html");
}
</%init>
<%method dump_module>
<%args>
$id
</%args>
<li>
<%perl>
  return if ($id eq 'root');
  if (!defined($modules{$id})) {
        print "can't get module $id\n";
        print Dumper($id);
        return;
  }
  $printed{$id}++;
  my $mod = $modules{$id}{module};
</%perl>
<% $mod->{description} %> (<% $mod->{name} %>)
%  if ($mod->{port}) {
(<% $mod->{port} %>)
%  }
%  for my $f (qw(fw hw sw)) {
%    if ($mod->{"${f}_ver"}) {
[<%$f%>: <%$mod->{"${f}_ver"}%>]
%    }
%  }
%  if ($mod->{serial}) {
[serial: <%$mod->{serial}%>]
%  }
/ <% $mod->{type} %> / <% $mod->{vendor} %> <% $mod->{model} %>
%  if ($mod->{fru}) {
<b>[FRU]</b>
%  }
// <i><% $mod->{class} %></i>
<!-- parent <% $mod->{parent} %> pos <% $mod->{pos} %> -->
%  #print join(" | ", keys %$mod);
%  if ($modules{$id}{children}) {
<ul>
%  my @kidtypes = keys %{$modules{$id}{children}};
%  foreach my $kidtype (@kidtypes) {
%#%    if (@kidtypes > 1) {
%#       <li><% $kidtype %>s
%#       <ul>
%#%    }
%    foreach my $kid (@{$modules{$id}{children}{$kidtype}}) {
%      if (defined($kid)) {
         <& SELF:dump_module, id=>$kid &>
%      }
%    }
%#%    if (@kidtypes > 1) {
%#       </ul></li>
%#%    }
%  }
</ul>
%  }
</li>
</%method>
%# $Id$
%# vim:syntax=mason
