require 'debci/app'
require 'debci/package'
require 'debci/html_helpers'

module Debci
  class NotFound < Debci::App
    include Debci::HTMLHelpers

    set :erb, escape_html: true
    set :views, "#{File.dirname(__FILE__)}/html/templates"

    not_found do
      if request.path =~ %r{^/data/autopkgtest/(\w+)/(\w+)/(\w+)/([^/]+)/(\d+)/log.gz$}
        @suite = ::Regexp.last_match(1)
        @arch = ::Regexp.last_match(2)
        @prefix = ::Regexp.last_match(3)
        @pkgname = ::Regexp.last_match(4)
        @job_id = ::Regexp.last_match(5)
        begin
          @package = Debci::Package.find_by_name(@pkgname)
          @job = @package.jobs.find(@job_id.to_i)
          # if the job exists, and we get here, by definition the job is
          # expired (because otherwise we would not be in the not found
          # handler).
          erb :log_expired
        rescue ActiveRecord::RecordNotFound
          erb :not_found
        end
      else
        erb :not_found
      end
    end
  end
end
