#!/usr/bin/env ruby

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

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

output.puts prefixes(:rdfs, :tm, :person, :location, :organisation, :relationship)
output.puts

DB[:relationship].each do |row|
  id = row[:IDRel]
  props = ["a tm:Relationship"]

  props << "tm:activePerson person:#{row[:IDPerson_active]}"               unless row[:IDPerson_active].to_s.empty?
  props << "tm:passivePerson person:#{row[:IDPerson_passive]}"             unless row[:IDPerson_passive].to_s.empty?
  props << "tm:relationshipType #{ttl_literal(row[:Relationshiptype])}"    unless row[:Relationshiptype].to_s.empty?
  props << "tm:relationshipTypePrecise #{ttl_literal(row[:relationshiptype_precise])}" unless row[:relationshiptype_precise].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 << "tm:location location:#{row[:IDLocation]}"                      unless row[:IDLocation].to_s.empty?
  props << "tm:organisation organisation:#{row[:IDOrganisation]}"          unless row[:IDOrganisation].to_s.empty?
  props << "tm:timePeriod #{ttl_literal(row[:Timeperiod])}"                unless row[:Timeperiod].to_s.empty?
  props << "rdfs:comment #{ttl_literal(row[:comment])}"                    unless row[:comment].to_s.empty?

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

output.close
