<div class='row'>
  <div class='col-lg-12'>
    <ol class='breadcrumb'>
      <li><a href='/packages/<%= @package.prefix %>'><%= @package.prefix %></a></li>
      <li><a href='/packages/<%= @package.prefix %>/<%= @package.name %>'><%= @package.name %></a></li>
      <li class='active'><%= @suite %>/<%= @architecture %></li>
    </ol>

    <h2>
      <%= @package.name %>
      <small>
        [ <%= @suite %>/<%= @architecture %> ]
      </small>
      <%= @package_links %>
    </h2>

    <% if @package.blacklisted? %>
      <div class='alert alert-warning'>
        This package is currently <a href="/status/blacklist/">blacklisted</a> and will not have any new test runs.
      </div>
    <% end %>

    <% if @latest && @latest.status == :fail && @latest.failmsg %>
      <div class='alert alert-warning'>
        <%= icon(@latest.extended_status) %>
        This package is failing and has <%= @latest.failmsg %>.
      </div>
    <% end %>

    <table class="table">
      <tr>
        <td><b>Version</b></td>
        <td><b>Date</b></td>
        <td><b>Trigger</b></td>
        <td><b>Duration</b></td>
        <td><b>Status</b></td>
        <td><b>Requestor</b></td>
        <td><b>Results</b></td>
        <td></td>
        <td></td>
      </tr>

      <% begin %>
        <% Array(@history).each do |test| %>
          <tr>
            <td><%= test.version %></td>
            <td><%= test.date %></td>
            <td><%= h test.trigger %></td>
            <td><%= test.duration_human %></td>
            <td>
              <%= icon(test.status) %>
              <%= test.status %>
              <% if test.status.to_s != "pass" %>
                <a href="/api/v1/retry/<%= test.run_id %>"><i class='fa fa-recycle'></i></a>
              <% end %>
            </td>
            <td><a href="/user/<%= test.requestor %>/jobs?package=<%= @package.name %>&suite[]=<%= @suite %>&arch[]=<%= @architecture %>"><%= test.requestor %></a></td>
	    <% package_dir = [test.suite, test.architecture, test.prefix, test.package].join('/') %>
            <% if test.expired? %>
              <td><span class='fa fa-trash' title='file has been removed due to data retention policy'></span></td>
            <% else %>
	      <td>
		<%= filesize(File.join(Debci.config.data_basedir, 'packages', package_dir, "#{test.run_id}.log"), "<a href=\"/data/packages/#{package_dir}/#{test.run_id}.log\">debci log</a>") %>
	      </td>
            <% end %>
            <% if @artifacts_url_base %>
              <td><a href="<%= @artifacts_url_base %>/<%= @package_dir %>/<%= test.run_id %>/log.gz">test log</a></td>
              <td><a href="<%= @artifacts_url_base %>/<%= @package_dir %>/<%= test.run_id %>/artifacts.tar.gz">artifacts</a></td>
            <% else %>
              <% if test.expired? %>
                <td><span class='fa fa-trash' title='file has been removed due to data retention policy'></span></td>
                <td><span class='fa fa-trash' title='file has been removed due to data retention policy'></span></td>
              <% else %>
		<% dirname = File.join(Debci.config.data_basedir, 'autopkgtest', @package_dir, test.run_id.to_s) %>
		<td>
		  <%= filesize(File.join(dirname,'log.gz'),"<a href=\"/data/autopkgtest/#{package_dir}/#{test.run_id}/log.gz\">test log</a> <small>(%s)</small>")%>
		</td>
		<td>
		  <%= filesize(File.join(dirname,'artifacts.tar.gz'),"<a href=\"/data/autopkgtest/#{package_dir}/#{test.run_id}/artifacts.tar.gz\">artifacts</a> <small>(%s)</small>")%>
		</td>

              <% end %>
            <% end %>
          </tr>
        <% end %>
      <% rescue JSON::ParserError %>
        </table>
        <div class='alert alert-warning' role='alert'>
          <span class='fa fa-warning'></span>
          Cannot display test history. There was an error when parsing the history data.
          <a href='/<%= @packages_dir %>/<%= @package_dir %>/history.json'>[ See raw JSON file ]</a>
        </div>
    <% end %>

    </table>

    <p>Automate:</p>

    <% automation_info = "<code># latest status of the package\n" +
                          "$ curl #{@site_url}/#{@packages_dir}/#{@package_dir}/latest.json\n\n"

        if not @artifacts_url_base
                automation_info += "# latest autopkgtest log of the package\n" +
                          "$ curl #{@site_url}/#{@packages_dir}/#{@package_dir}/latest-autopkgtest/log.gz\n\n"
        end

        automation_info += "# test run history of the package\n" +
                          "$ curl #{@site_url}/#{@packages_dir}/#{@package_dir}/history.json</code>"
    %>

    <pre><%= automation_info %></pre>

  </div>
</div>
