#!/usr/bin/ruby

require 'fileutils'
require 'rss'

require 'debci'

repository = Debci::Repository.new

feeds_dir = File.join(Debci.config.data_basedir, 'feeds')
$site_base = Debci.config.url_base
$artifacts_url_base = Debci.config.artifacts_url_base
$distro_name = Debci.config.distro_name

FileUtils.mkdir_p(feeds_dir)

past_timestamp = Time.parse('2014-05-10T21:17:00 UTC')

# expand {SUITE} macro in URLs
def expand_url(url, suite)
  url && url.gsub('{SUITE}', suite)
end

def insert_items(news, feed)
  news.each do |status|
    feed.items.new_item do |item|
      prefix = status.package.sub(/^((lib)?.).*/, '\1')
      item.link = "#{$site_base}/data/packages/#{status.suite}/#{status.architecture}/#{prefix}/#{status.package}/#{status.run_id}.log"
      item.title = status.headline
      item.date = status.date
      item.description = [
        "<p>#{status.description}</p>",
        '<ul>',
        "<li>Version: #{status.version}</li>",
        "<li>Date: #{status.date}</li>",
        "<li>Test run duration: #{status.duration_human}</li>",
        "<li><a href=\"#{$site_base}/packages/#{prefix}/#{status.package}/#{status.suite}/#{status.architecture}\">Package history page</a></li>",
        "<li><a href=\"#{$site_base}/data/packages/#{status.suite}/#{status.architecture}/#{prefix}/#{status.package}/#{status.run_id}.log\">debci log</a></li>"]

      if $artifacts_url_base
        item.description += [
          "<li><a href=\"#{expand_url($artifacts_url_base, status.suite)}/#{status.suite}/#{status.architecture}/#{prefix}/#{status.package}/#{status.run_id}/log.gz\">autopkgtest log</a></li>",
          "<li><a href=\"#{expand_url($artifacts_url_base, status.suite)}/#{status.suite}/#{status.architecture}/#{prefix}/#{status.package}/#{status.run_id}/artifacts.tar.gz\">autopkgtest artifacts</a></li>"]
      else
        item.description += [
          "<li><a href=\"#{$site_base}/data/autopkgtest/#{status.suite}/#{status.architecture}/#{prefix}/#{status.package}/#{status.run_id}/log.gz\">autopkgtest log</a></li>",
          "<li><a href=\"#{$site_base}/data/autopkgtest/#{status.suite}/#{status.architecture}/#{prefix}/#{status.package}/#{status.run_id}/\">autopkgtest artifacts</a></li>"]
      end

      item.description = item.description.compact.join("\n")
    end
  end
end

packages = ARGV
if packages.empty?
  packages = repository.packages
end

packages.each do |pkg|
  news = repository.news_for(pkg)
  prefix = pkg.sub(/^((lib)?.).*/, '\1')
  feedfile = File.join(feeds_dir, prefix, pkg + '.xml')
  FileUtils.mkdir_p(File.dirname(feedfile))

  feed = RSS::Maker.make('atom') do |feed|
    feed.channel.author = $distro_name + ' Continuous Integration'
    feed.channel.updated = news.first && news.first.date || past_timestamp
    feed.channel.about = $site_base + '/#package/' + pkg
    feed.channel.title = pkg + ' CI news feed'
    feed.channel.description = "News for #{pkg}. Includes only state transitions (pass-fail, and fail-pass). Full history is available in the package page and in the published data files."

    insert_items(news, feed)
  end

  File.open(feedfile, 'w') do |f|
    f.write(feed.to_s.gsub('<summary>', '<summary type="html">'))
  end
end

global_news = repository.global_news(50)
global_feed = RSS::Maker.make('atom') do |feed|
  feed.channel.author = $distro_name + ' Continuous Integration'
  feed.channel.updated = global_news.first && global_news.first.date || past_timestamp
  feed.channel.about = $site_base
  feed.channel.title = $distro_name + ' Continuous Integration news'
  feed.channel.description = "News about all packages. Includes only state transitions (pass-fail, fail-pass). Full history is available in each individual package page and in their published data files."
  insert_items(global_news, feed)
end
File.open(File.join(feeds_dir, 'all-packages.xml'), 'w') do |f|
  f.write(global_feed.to_s.gsub('<summary>', '<summary type="html">'))
end
