<% log_url = "/data/autopkgtest/#{ @job.suite }/#{ @job.arch }/#{ @job.package.prefix }/#{ @job.package.name }/#{ @job.run_id }/log.gz" %>
<h2>
  <%= @package.name %>
  <small>
    [ <a href=".."><%= @job.suite %>/<%= @job.arch %></a> ]
  </small>
  &mdash;
  job <%= @job.run_id %>
<a class="btn btn-info btn-lg" href="<%= log_url %>"><span class=" fa fa-download"></span> Download raw log</a>
</h2>
<% if @too_large %>
  <p class="alert alert-warning">
  This log file is too large to be displayed. Please download the raw log instead.
  </p>
<% elsif @log.sections.size == 1 %>
  <div class="log-view">
    <div class="log-section">
      <% line = 0 %>
      <% @log.sections.first.output.each do |log_line| %>
        <% line += 1 %>
        <span id="L<%= line %>" class="line-number"><a href="#L<%= line%>"><%= line %></a></span>
        <div class="log-line"><%= log_line %></div>
      <% end %>
    </div>
  </div>
<% else %>
  <div class="row">
    <div class="col-md-2 list-group" id="logview-index">
      <% i = 0 %>
      <% @log.sections.each do |section| %>
        <div class="list-group">
          <% klass = {"pass" => "list-group-item-success", "fail" => "list-group-item-danger"}[section.result] || "list-group-item-info" %>
          <div class="list-group-item <%= klass %>">
            <strong><%= section.name %></strong>
          </div>
          <% section.subsections.each do |sub| %>
            <% i += 1 %>
            <a class="list-group-item" href="#S<%= i %>"><%= sub.name %></a>
          <% end %>
        </div>
      <% end %>
    </div>
    <div class="col-md-10">
      <div class="log-view">
        <% line = 0 %>
        <% i = 0 %>
        <% @log.sections.each do |section| %>
          <% section.subsections.each do |sub| %>
            <% i += 1 %>
            <div id="S<%= i %>" class="log-divider log-divider-<%= section.result %>">
              <a href="#S<%= i %>" onclick="javascript: return toggleLogSection('#S<%= i %>')">
                <span class="log-toggle log-open">▸</span>
                <span class="log-toggle">▾</span>
                <% if section.test? %><%= section.name %>:<% end %>
                <span class="log-section-name"><%= sub.name %></span>
              </a>
            </div>
            <div class="log-section">
              <% sub.output.each do |log_line| %>
                <% line += 1 %>
                <span id="L<%= line %>" class="line-number"><a href="#L<%= line%>"><%= line %></a></span>
                <div class="log-line"><%= log_line %></div>
              <% end %>
            </div>
          <% end %>
        <% end %>
      </div>
    </div>
  </div>
  <script type="text/javascript">
    function toggleLogSection(section) {
      $(section).next(".log-section").toggle()
      $(section).find(".log-toggle").toggle()
      return false
    }
    document.addEventListener('DOMContentLoaded', function() {
      $(".log-divider").each(function() {
        var anchor = document.location.hash
        if (! $(this).find(".log-section-name").html().match(/test run/)) {
          if (anchor != "#" + $(this).attr("id")) {
            toggleLogSection(this)
          }
        }
      });
    })

  </script>
<% end %>
