#!/bin/sh
# import.sh: builds the graph in your own Neo4j 5.26 from the signed CSV files in this folder, with the options the
# builder's own runtime uses (prepaire-reference-kg runtime/kgruntime/importer.py IMPORT_OPTIONS). Run verify.sh first.
#   NEO4J_HOME=/path/to/neo4j-community-5.26.x ./import.sh [database]
# The database (default neo4j, the one database Neo4j Community serves) is overwritten. Stop Neo4j before running it.
# Afterwards: start Neo4j, apply constraints.cypher, then run check.cypher (README.md, "Load it into Neo4j").
set -eu
: "${NEO4J_HOME:?Set NEO4J_HOME to your Neo4j 5.26 folder}"
here=$(cd "$(dirname "$0")" && pwd)
database=${1:-neo4j}
set -- database import full "$database" "--delimiter=;" "--array-delimiter=|" "--quote='" --id-type=string \
  --ignore-empty-strings=true --skip-bad-relationships=false --skip-duplicate-nodes=false --bad-tolerance=0 --strict=true \
  --overwrite-destination=true "--report-file=$here/import.report"
for header in "$here"/csv/*-header.csv; do
  name=$(basename "$header" -header.csv)
  files=$header
  for part in "$here"/csv/"$name"-part*.csv; do files="$files,$part"; done
  # node files carry an :ID column, edge files :START_ID and :END_ID
  if head -n 1 "$header" | grep -q ':START_ID'; then set -- "$@" "--relationships=$files"; else set -- "$@" "--nodes=$files"; fi
done
exec "$NEO4J_HOME/bin/neo4j-admin" "$@"
