#!/usr/bin/ruby
# -*- encoding: utf-8 -*-
require 'erb'
require 'rss'
require '../../bin/rss_cdata.rb'
require '../../bin/newsparser.rb'

# Layout class
class Layout
  extend ERB::DefMethod
  def_erb_method('layout', '../../template/layout-errata.html.erb')
end

# 
urlhead="http://vinelinux.org/"
erratadir="./"
tmpdir = '../../template/'

header=File.open("../../template/header.tmpl")
footer=File.open("../../template/footer.tmpl")

curdir = File.basename(Dir.pwd)

# Create rss object
rss = RSS::Maker.make("1.0") do |maker|
  maker.channel.about = "http://vinelinux.org/errata/#{curdir}/rss"
  maker.channel.title = "エラッタ情報(#{curdir})"
  maker.channel.description = "Vine Linux #{curdir} のセキュリティ及びバグ更新情報です"
  maker.channel.link = "http://vinelinux.org/errata/#{curdir}/"
  maker.items.do_sort = true

# create errata indexhtml contents
errataindex = <<EOF
<h2>Vine Linux #{curdir} のエラッタ情報</h2>
<div class="errata">
<table>
  <tr>
   <th class="date">日付</th>
   <th class="desc">内容</th>
   <th class="target">対象</th>
  </tr>
EOF

# create all errata page
Dir.open(erratadir) { |dir|
 for filename in dir.grep(/.dat$/).sort.reverse
 begin
  errata = NewsParser.new IO.read(erratadir+filename)
  titletext = errata.item['title'].gsub("\n","").gsub(/<[^>]+>/,"").strip
  $header_title = 'Vine Linux ' + curdir + ' Errata - ' + titletext

  # index content
  errataindex += <<-EOF
  <tr>
   <td class="date">[#{errata.item['date']}]</td>
   <td class="desc"><a href="/errata/#{curdir}/#{filename.gsub(/.dat/,".html")}">
       #{errata.item['title']}</a></td>
   <td class="target">#{errata.item['target']}</td>
  </tr>
  EOF

  # rss
  rssitem = maker.items.new_item
  rssitem.link = urlhead+"errata/#{curdir}/#{filename.gsub(/.dat/,".html")}"
  rssitem.title = errata.item['title']
  rssitem.date = Time.parse(errata.item['date'].gsub(/\,/,"/"))
  rssitem.dc_date = Time.parse(errata.item['date'].gsub(/\,/,"/"))
  rssitem.content_encoded = errata.item['info']

  # errata page content
  erratahtml = <<-EOF
  <h2>#{errata.item['title']}</h2>
  <div class="ads-middle-linkunit">
<div id="google-linkads-2"></div>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- vinelinux new link-unit -->
<script>
     ad = document.getElementById('google-linkads-2');
     var adWidth = (typeof ad.getBoundingClientRect === 'function') ?
     ad.getBoundingClientRect().width : ad.offsetWidth;
     
     google_ad_client = "ca-pub-0334540465096658";
     
     if ( adWidth >= 728 )      google_ad_param = ["728", "15", "6936914939"];
     else if ( adWidth >= 468 ) google_ad_param = ["468", "15", "2367114535"];
     else if ( adWidth >= 200 ) google_ad_param = ["200", "90", "3843847731"];
     else if ( adWidth >= 180 ) google_ad_param = ["180", "90", "5320580934"];
     else if ( adWidth >= 160 ) google_ad_param = ["160", "90", "6797314134"];
     else                       google_ad_param = ["120", "90", "8274047334"];
     
     document.write (
     '<ins class="adsbygoogle" style="display:inline-block;'
     + 'width:' + google_ad_param[0] + 'px;'
     + 'height:' + google_ad_param[1] + 'px'
     + '" data-ad-client="' + google_ad_client
     + '" data-ad-slot="' + google_ad_param[2]
     + '"></ins>'
     );
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
  </div>
  <div align="right">発行日:#{errata.item['date']}</div>
  <div class="errata">
  <dl>
    <dt>対象 (Vine Linuxバージョン):</dt>
    <dd><p>#{errata.item['target']}</p></dd>
    <dt>内容:</dt>
    <dd>#{errata.item['info']}</dd>
    <dt>修正済パッケージ/ファイル:</dt>
    <dd>
<pre>
#{errata.item['update']}
</pre>
    </dd>
    <dt>入手先:</dt>
    <dd>#{'update-watch, ' if curdir != "4x"}synaptic または apt-get でアップグレードすることができます。
<pre class="screen">
# apt-get update
# apt-get upgrade
</pre>
    該当するパッケージをインストールしていない場合は、
    更新の必要はありません。<br />
    また、各ミラーサイトの
<pre>
#{errata.item['directory']}
</pre>
    からも個別に入手することができます。
    </dd>
    <dt>関連URL:</dt>
    <dd><ul>
  EOF

  if errata.item['url']
    errata.item['url'].each_line do |line|
      erratahtml += "    <li><a href=\"#{line}\">#{line}</a></li>\n"
    end
  else
    erratahtml += "    <p>なし</p>\n"
  end

  erratahtml += <<-EOF
    </ul></dd>
  </dl>
  </div>
  EOF

  # write errata page
  template = ERB.new(erratahtml)
  newfile = open(filename.gsub(/.dat/,".html"), "w")
  newfile.puts Layout.new.layout { template.result }
  newfile.close
 rescue
  puts "\nE: #{filename}, skipped."
 end
 end
}

errataindex += <<EOF
</table>
</div>
EOF

# write errata indexhtml
template = ERB.new(errataindex)
$header_title = 'Vine Linux ' + curdir + ' Errata'
errataindexhtml = open("index.html", "w")
errataindexhtml.puts Layout.new.layout { template.result }
errataindexhtml.close

end

# write errata rss
File.open("errata#{curdir}.rdf", "w") do |file|
  file.puts rss.to_s
end
