[%
  buildUrl = c.uri_for('/build', build.id);
  IF step;
    stepUrl = c.uri_for('/build', build.id, 'step', step.stepnr);
    logUrl = c.uri_for('/build', build.id, 'step', step.stepnr, 'log');
    drvpath = step.drvpath;
    titleText = "Log of step ${step.stepnr} of build ${build.id} of job " _ makeNameTextForJob(build.jobset, job);
    titleLink = "Log of <a href=\"$stepUrl\">step ${step.stepnr}</a>"
              _ " of <a href=\"$buildUrl\">build ${build.id}</a>"
              _ " of job " _ linkToJob(build.jobset, job);
  ELSE;
    logUrl = c.uri_for('/build', build.id, 'log');
    drvpath = build.drvpath;
    titleText = "Log of build ${build.id} of job " _ makeNameTextForJob(build.jobset, job);
    titleLink = "Log of <a href=\"$buildUrl\">build ${build.id}</a>"
              _ " of job " _ linkToJob(build.jobset, job);
  END;
  rawUrl = logUrl _ '/raw';
%]

[% WRAPPER layout.tt titleHTML=titleLink title=titleText %]
[% PROCESS common.tt %]

<p>
  Below
  [% IF tail %]
  are the last lines of
  [% ELSE %]
  is
  [% END %]
  the build log (<a [% HTML.attributes(href => rawUrl) %]>raw</a>)
  of derivation <tt>[% drvpath | html %]</tt>.
  [% IF step && step.machine %]
    It was built on <tt>[% step.machine | html %]</tt>.
  [% END %]
  [% IF tail %]
  The <a [% HTML.attributes(href => logUrl) %]>full log</a> is also available.
  [% END %]
</p>

<div class="card bg-light"><div class="card-body"><pre class="log" id="contents">
<em>Loading...</em>
</pre></div></div>

<script type="text/javascript">
  $(document).ready(function() {
    requestPlainFile({
      url: "[% HTML.escape(log_uri) %]",
      dataType: "text",
      type: 'GET',
      success: function (log_data) {

        [% IF tail %]
        /* The server may give us a full log (e.g. if the log is in
           S3). So extract the last lines. */
        log_data = log_data.split("\n").slice(-[% HTML.escape(tail) %]).join("\n");
        [% END %]

        $("#contents").html(new ansiUp.AnsiUp().ansi_to_html(log_data));
      },
      error: function () {
        bootbox.alert("The log file is not available.");
        $("#contents").text("(Unavailable)");
      }
    });
  });
</script>

[% END %]
