#!/usr/bin/env ruby

require_relative 'database'
require_relative 'vocabularies'
require_relative 'migrants'

output = File.open(File.join('data', 'personnames.ttl'), 'w')

output.puts prefixes(:rdfs, :tm, :schema, :person, :personname)
output.puts

DB[:personnames].each do |row|
  id = row[:IDPersonname]
  props = ["a tm:PersonName"]

  props << "tm:person person:#{row[:IDPerson]}"                        unless row[:IDPerson].to_s.empty?
  props << "schema:givenName #{ttl_literal(row[:personName])}"         unless row[:personName].to_s.empty?
  props << "schema:familyName #{ttl_literal(row[:personSurName])}"     unless row[:personSurName].to_s.empty?
  props << "tm:nameType #{ttl_literal(row[:Nametype])}"                unless row[:Nametype].to_s.empty?
  props << "tm:dateStartFuzzy #{ttl_literal(row[:DateStart_Fuzzy])}"   unless row[:DateStart_Fuzzy].to_s.empty?
  props << "tm:dateStartMin #{ttl_literal(row[:DateStart_Min])}"       if row[:DateStart_Min]
  props << "tm:dateStartMax #{ttl_literal(row[:DateStart_Max])}"       if row[:DateStart_Max]
  props << "tm:dateEndFuzzy #{ttl_literal(row[:DateEnd_Fuzzy])}"       unless row[:DateEnd_Fuzzy].to_s.empty?
  props << "tm:dateEndMin #{ttl_literal(row[:DateEnd_Min])}"           if row[:DateEnd_Min]
  props << "tm:dateEndMax #{ttl_literal(row[:DateEnd_Max])}"           if row[:DateEnd_Max]
  props << "rdfs:comment #{ttl_literal(row[:comment])}"                unless row[:comment].to_s.empty?

  output.puts "personname:#{id} #{props.first} ;"
  props[1..-2].each { |p| output.puts "  #{p} ;" }
  output.puts "  #{props.last} ."
  output.puts
end

output.close
