#!/usr/bin/env ruby
    
require 'dbi'
require './squish'

dbdriver = 'Mysql'

    query=<<EOQ;

	SELECT ?uri, ?title, ?desc,  
	WHERE (rdf::type ?uri rss::channel)
	(rss::title ?uri ?title)
	(rss::description ?uri ?desc)
	USING 
	rdf for http://www.w3.org/1999/02/22-rdf-syntax-ns#
	rss for http://purl.org/rss/1.0/
	foaf for http://xmlns.com/foaf/0.1/  
	dc for http://purl.org/dc/elements/1.1/
EOQ

    dbi_driver = 'DBI:'+dbdriver+':rdfweb1'
    dbi_user = 'danbri'
    dbi_pass =''
    q = SquishQuery.new.parseFromText (query)

    sql = q.toSQLQuery ({'quotevars'=> 1 })

    sql.gsub!( /\.key/ , ".keyhash" )
    # also we want to put '' around user-supplied field names
	# in our SELECT clause, eg. ...AS 'desc', but lets 
	# see if postgres is happy with that.


    puts "\n#{sql}\n\n"

    h= '<?xml version="1.0" encoding="UTF-8"?>'
    h += "\n" 
    h += '<html xmlns="http://www.w3.org/1999/xhtml">'
    h +=  "<head><title>RDFWeb: RSS Channel Directory</title>\n\n"
    h += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />  </head><body>\n"
    h += "<h1>RSS 1.0 Channels:</h1>\n\n"
    h +=  "<ul>\n\n"
    DBI.connect(  dbi_driver, dbi_user ,  dbi_pass ) do | dbh |
      dbh.select_all( q.toSQLQuery  ) do | c |
        channel=ResultRow.new c
	esc_utf8 channel.desc
        esc_utf8 channel.title
        h += "<li><a href=\"../rssview/?u=#{channel.uri}\">#{channel.title}</a> <em>#{channel.desc}</em></li>";
      end
    end
    h += "</ul>"
    h += "\n\n</body>\n</html>\n\n"
    res['Content-Type'] = "text/html; charset=utf-8"
    puts h
