#!/usr/bin/env ruby

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

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

output.puts prefixes(:wd, :rdfs, :tm, :schema, :person, :location, :xsd)
output.puts

DB[:person].each do |row|
  id = row[:IDPerson]
  props = ["a schema:Person"]

  props << "schema:givenName #{ttl_literal(row[:first_name])}" unless row[:first_name].to_s.empty?
  props << "schema:familyName #{ttl_literal(row[:family_name])}" unless row[:family_name].to_s.empty?
  props << "tm:nameType #{ttl_literal(row[:Nametype])}" unless row[:Nametype].to_s.empty?

  case row[:gender]
  when 'male'   then props << "schema:gender schema:Male"
  when 'female' then props << "schema:gender schema:Female"
  end

  props << "schema:image <#{row[:imageURL]}>" unless row[:imageURL].to_s.empty?
  props << "tm:imageSource #{ttl_literal(row[:image_source])}" unless row[:image_source].to_s.empty?
  props << "schema:hasOccupation #{ttl_literal(row[:profession])}" unless row[:profession].to_s.empty?
  props << "tm:fuzzyBirthDate #{ttl_literal(row[:fuzzybirthdate])}" unless row[:fuzzybirthdate].to_s.empty?

  props << "schema:birthDate #{ttl_literal(row[:birthdate])}" if row[:birthdate]
  props << "tm:birthDateMax #{ttl_literal(row[:birthdate_max])}" if row[:birthdate_max]

  props << "tm:birthInfo #{ttl_literal(row[:'Birth Info'])}" unless row[:'Birth Info'].to_s.empty?
  props << "schema:birthPlace location:#{row[:IDBirthPlace]}" unless row[:IDBirthPlace].to_s.empty?

  props << "tm:fuzzyDeathDate #{ttl_literal(row[:fuzzydeathdate])}" unless row[:fuzzydeathdate].to_s.empty?

  props << "schema:deathDate #{ttl_literal(row[:deathdate])}" if row[:deathdate]
  props << "tm:deathDateMax #{ttl_literal(row[:deathdate_max])}" if row[:deathdate_max]

  props << "tm:deathInfo #{ttl_literal(row[:'Death Info'])}" unless row[:'Death Info'].to_s.empty?
  props << "schema:deathPlace location:#{row[:IDDeathPlace]}" unless row[:IDDeathPlace].to_s.empty?

  props << "schema:sameAs <#{row[:Wikipedia]}>" unless row[:Wikipedia].to_s.empty?
  props << "schema:sameAs wd:#{row[:Wikidata].to_s.sub(/.*(?=Q)/, '')}" unless row[:Wikidata].to_s.empty?
  props << "tm:gnd #{ttl_literal(row[:GND])}" unless row[:GND].to_s.empty?
  props << "tm:lccn #{ttl_literal(row[:LCCN])}" unless row[:LCCN].to_s.empty?
  props << "tm:viaf #{ttl_literal(row[:VIAF])}" unless row[:VIAF].to_s.empty?
  props << "tm:cerl #{ttl_literal(row[:CERL])}" unless row[:CERL].to_s.empty?
  props << "tm:isni #{ttl_literal(row[:ISNI])}" unless row[:ISNI].to_s.empty?
  props << "tm:snac #{ttl_literal(row[:SNAC])}" unless row[:SNAC].to_s.empty?
  props << "rdfs:comment #{ttl_literal(row[:comment])}" unless row[:comment].to_s.empty?
  props << "tm:source #{ttl_literal(row[:Source])}" unless row[:Source].to_s.empty?
  props << "tm:importSource #{ttl_literal(row[:Importsource])}" unless row[:Importsource].to_s.empty?

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

output.close
