#!/usr/bin/env ruby

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

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

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

DB[:migration_table].each do |row|
  id = row[:IDMig]
  props = ["a tm:Migration"]

  props << "tm:person person:#{row[:IDPerson]}"               unless row[:IDPerson].to_s.empty?
  props << "tm:startPlace location:#{row[:IDStartPlace]}"     unless row[:IDStartPlace].to_s.empty?
  props << "tm:destPlace location:#{row[:IDDestPlace]}"       unless row[:IDDestPlace].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:reason #{ttl_literal(row[:reason])}"           unless row[:reason].to_s.empty?
  props << "tm:reason2 #{ttl_literal(row[:reason2])}"         unless row[:reason2].to_s.empty?
  props << "tm:via #{ttl_literal(row[:via])}"                 unless row[:via].to_s.empty?

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

output.close
