#!/usr/bin/env ruby

$LOAD_PATH.unshift '../lib/'
$LOAD_PATH.unshift '../../lib/'

require 'basicrdf'
require 'squish'
require 'dbi'


def fetch_grub 
  gw = 'http://the.earth.li/~kake/cgi-bin/cgi-wiki/wiki.cgi?action=rss&feed=chef_moz&id='

  # pubs from http://grault.net/cgi-bin/grubstreet.pl?search=Category+Pubs
  pubs=  [
	'Anchor_Bankside,_SE1_9EF',
        'Calthorpe_Arms,_WC1X_8JR',
	'Audley,_W1K_2RP',
	'Crosse_Keys,_EC3V_0DR',
	'Albion,_N1_1HW',
	'Anchor_Bankside,_SE1_9EF',
	'Andover_Arms,_W6_0DL',
	'Angel,_N1_9LQ',
	'Banker,_EC4R_3TE',
	'Cittie_Of_Yorke,_WC1V_6BN',  
	'Counting_House,_EC3V_3PD'   ]
  log=''
  res=Hash.new
  res['raw']=''
  pubs.each do |pub|
    uri=gw+pub
    log += "\n\nFetching RDF for: #{gw+pub} \n"
    data = Loader.get_rdf_from_uri gw+pub, gw+pub

    res['raw']+=data.toNtriples
 
    src='http://example.com/grubst:'+pub
    sql_inserts=data.toSQLInserts ("uri=#{src}")

    # FIXME: I suspect toSQLInserts is buggy, and that
    #  I've already fixed it for foaf stuff. todo: check and find code.
    begin
      if !sql_inserts.empty?
        log += "updating query server.\n"
        DBI.connect (  'DBI:Pg:rdfweb1', 'danbri', '' ) do |dbh|
          # clean out last triples from this src
          log+= "-  #dbi.do delete from triples where assertid = 'uri=#{src}';\n"
          begin
            dbh.do "delete from triples where assertid = 'uri=#{src}';"
          rescue
            log += "DBI: Error in sql delete, msg: #{$!}\n"
          end
          log += "+"
         sql_inserts.each do |sql_insert|
            begin
              log += '+'
              dbh.do sql_insert
              # puts "DEBUG: #{sql_insert} \n"
            rescue
              # puts "DBI: Error in sql insert #{src} sql: #{sql_insert} msg: #{$!}" 
	      # this will be really verbose (lots of inserts into fields where dups not allowed)
            end
          end
          log += "\n"
        end
      else
        log+= "skipping reload (no triples).\n\n"
        STDERR.puts "No data in '#{uri}'"
      end
    rescue Exception
      puts "Caught error: #{$!}"
    end
  end
  res['log']=log
  return res
end


#############

# FIXME:
# add some 'if we're being called as a library' stuff here

# puts fetch_grub['raw']
