require 'rubygems'
require 'xmlsimple'
require 'erb'
require 'hpricot'
require 'open-uri'

@base_path = File.expand_path($0).gsub(/lib\/recently_modified.rb/, '')
@base_url = "http://example.com/"

template = %q{<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title>Recently modified example.com pages</title>
 	<link href="http://example.com/lib/recently_modified.xml" rel="self"/>
 	<updated><%= Time.now %></updated>
 	<author>
  	<name>Web Team</name>
   	<email>webteam@example.com</email>
	</author>
	<id>urn:uuid:60a76c80-d1e9-01d9-b91d-0003939e0af6</id>
	<% @files.each do |file| %>
	<% 
	  file.gsub!(@base_path, '')
	  info = get_modified_info(file) 
	  url = @base_url + file.gsub(@base_path, '')
	%>
	<entry>
      <title>/<%= file %></title>
      <link href="http://example.com/<%= file %>"/>
      <updated><%= Time.at(info['date'].to_i) %></updated>
      <summary>Modified by <%= info['name']%> (<%= info['email'] %>) at <%= Time.at(info['date'].to_i) %>.  This file has <%= is_valid?(url) ? "passed" : "failed" %> W3C markup validation: http://validator.w3.org/check?uri=<%= url %>.</summary>
   </entry>
	<% end %>
</feed>
}

def is_valid?(url)
  doc = Hpricot(open("http://validator.w3.org/check?uri=#{url}"))
  return (doc/"title").inner_html.gsub(/.*\[/mis, '').gsub(/\].*/mis, '') == "Valid"
end

def get_modified_info(file)
  if file.index('/')
    info_file = @base_path + file.gsub(/\/[^\/]*$/, '') + '/_notes/' + file.gsub(/^.*\//, '').strip + '.mno'
  else
    info_file = @base_path + '_notes/' + file.gsub(/^.*\//, '').strip + '.mno'
  end
  
  data = XmlSimple.xml_in(info_file)
  ret = Hash.new

  data['infoitem'].each do |info|
    ret['name'] = info['value'] if info['key'] == 'ccLastSubmitter'
    ret['email'] = info['value'] if info['key'] == 'ccLastSubmitterEmail'
    ret['date'] = info['value'] if info['key'] == 'ccLastPublishDate'
  end

  return ret
end

@files = `find #{@base_path} -mmin -60 | grep .html$`
puts ERB.new(template).result
