RUBY = RbConfig.ruby

def run(script)
  sh "#{RUBY} #{script}"
end

# ── Individual file tasks ────────────────────────────────────────────────────

file 'data/countries.ttl' => 'src/map_countries_from_location.rb' do
  run 'src/map_countries_from_location.rb'
end

# Generates data/countries_wikidata_review.ttl as a side-effect
file 'data/countries_wikidata.ttl' => ['data/countries.ttl', 'src/link_countries_to_wikidata.rb'] do
  run 'src/link_countries_to_wikidata.rb'
end

file 'data/locations.ttl' => 'src/map_locations.rb' do
  run 'src/map_locations.rb'
end

file 'data/migrations.ttl' => 'src/map_migration_table.rb' do
  run 'src/map_migration_table.rb'
end

file 'data/occupations.ttl' => 'src/map_occupations_from_person_profession.rb' do
  run 'src/map_occupations_from_person_profession.rb'
end

file 'data/organisations.ttl' => 'src/map_organisation.rb' do
  run 'src/map_organisation.rb'
end

file 'data/personnames.ttl' => 'src/map_personnames.rb' do
  run 'src/map_personnames.rb'
end

# Generates data/persons_occupations_review.ttl as a side-effect
file 'data/persons_occupations.ttl' => ['data/occupations.ttl', 'src/map_person_profession.rb'] do
  run 'src/map_person_profession.rb'
end

file 'data/persons.ttl' => 'src/map_person.rb' do
  run 'src/map_person.rb'
end

file 'data/relationships.ttl' => 'src/map_relationship.rb' do
  run 'src/map_relationship.rb'
end

file 'data/persons_religions.ttl' => 'src/map_religions.rb' do
  run 'src/map_religions.rb'
end

# Generates data/religions_wikidata_review.ttl as a side-effect
file 'data/religions_wikidata.ttl' => ['data/religions.ttl', 'src/link_religions_to_wikidata.rb'] do
  run 'src/link_religions_to_wikidata.rb'
end

file 'data/works.ttl' => 'src/map_work.rb' do
  run 'src/map_work.rb'
end

# ── Pipeline steps ───────────────────────────────────────────────────────────

file 'data/graph-01.ttl' => 'map/step-01.rb' do
  run 'map/step-01.rb'
end

UPDATE_QUERIES = FileList['updates/*.rq']

file 'data/graph-02.ttl' => ['data/graph-01.ttl', 'map/step-02.rb'] + UPDATE_QUERIES do
  run 'map/step-02.rb'
end

# ── Aggregate tasks ──────────────────────────────────────────────────────────

GENERATED = %w[
  data/countries.ttl
  data/countries_wikidata.ttl
  data/locations.ttl
  data/migrations.ttl
  data/occupations.ttl
  data/organisations.ttl
  data/personnames.ttl
  data/persons_occupations.ttl
  data/persons.ttl
  data/relationships.ttl
  data/persons_religions.ttl
  data/religions_wikidata.ttl
  data/works.ttl
].freeze

GRAPHS = %w[
  data/graph-01.ttl
  data/graph-02.ttl
].freeze

task default: GENERATED + GRAPHS

task :clean do
  review_files = %w[
    data/countries_wikidata_review.ttl
    data/religions_wikidata_review.ttl
  ]
  rm_f GENERATED + GRAPHS + review_files
end
