#!/bin/bash -e

# Generate SNB dataset.

function print_help () {
    echo "Usage: $0 [OPTION]"
    echo "Optional arguments:"
    echo -e "  -h|--help -> Prints help."
    echo -e "  --scale-factor Positive_Integer -> Defines the dataset size."
}

script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
base_dir="${script_dir}/../../.."
neo4j_dir="${base_dir}/libs/neo4j"
build_dir="${base_dir}/build"

# Add Maven to PATH
export PATH=$PATH:${script_dir}/maven/bin

# Read the arguments.
scale_factor=1
skip_generating=false
while [[ $# -gt 0 ]]
do
    case $1 in
        -h|--help)
        print_help
        exit 1
        ;;
        --scale-factor)
        scale_factor=$2
        shift
        ;;
        *)
        # unknown option
        ;;
    esac
    shift # past argument or value
done

# Prepare the folder structure.
dataset_dir="${script_dir}/datasets/scale_${scale_factor}"
if [ -d ${dataset_dir} ]; then
    rm -rf ${dataset_dir}
fi
mkdir -p ${dataset_dir}

# Define scale factor.
echo "Using scale_factor" $scale_factor
cat > ${script_dir}/ldbc_snb_datagen/params.ini <<EOF
ldbc.snb.datagen.generator.scaleFactor:snb.interactive.${scale_factor}
ldbc.snb.datagen.serializer.personSerializer:ldbc.snb.datagen.serializer.snb.interactive.CSVPersonSerializer
ldbc.snb.datagen.serializer.invariantSerializer:ldbc.snb.datagen.serializer.snb.interactive.CSVInvariantSerializer
ldbc.snb.datagen.serializer.personActivitySerializer:ldbc.snb.datagen.serializer.snb.interactive.CSVPersonActivitySerializer
EOF

# Find installed Java binary.
if [[ -d "/usr/lib/jvm/default-java/jre" ]]; then
    export JAVA_HOME=/usr/lib/jvm/default-java/jre
elif [[ -d "/usr/lib/jvm/default-runtime/" ]]; then
    export JAVA_HOME=/usr/lib/jvm/default-runtime/
else
    echo "Unable to find JRE under /usr/lib/jvm"
    exit 1
fi
echo "Using JAVA_HOME" $JAVA_HOME

# Remove old generated dataset.
rm -rf ${ldbc_snb_datagen_folder}/social_network ${ldbc_snb_datagen_folder}/substitution_parameters

# Generate the dataset.
ldbc_snb_datagen_folder=${script_dir}/ldbc_snb_datagen
cd ${ldbc_snb_datagen_folder}
# Poorly documented hadoop heapsize flag (unit is 'm'), see: hadoop/libexec/hadoop-config.sh
# https://stackoverflow.com/questions/15609909/error-java-heap-space
export HADOOP_HEAPSIZE=8192
HADOOP_HOME=${script_dir}/hadoop LDBC_SNB_DATAGEN_HOME=${ldbc_snb_datagen_folder} ./run.sh

# Copy generated dataset.
cp -r ${ldbc_snb_datagen_folder}/social_network ${dataset_dir}/
cp -r ${ldbc_snb_datagen_folder}/substitution_parameters ${dataset_dir}/

# Transform the dataset into Neo4j CSV format.
mkdir -p ${dataset_dir}/csv
cd ${script_dir}/ldbc-snb-impls/snb-interactive-neo4j
mvn exec:java \
    -Dexec.mainClass="net.ellitron.ldbcsnbimpls.interactive.neo4j.util.DataFormatConverter" \
    -Dexec.args="${ldbc_snb_datagen_folder}/social_network ${dataset_dir}/csv"

csv_dataset="
--nodes ${dataset_dir}/csv/comment_0_0.csv \
--nodes ${dataset_dir}/csv/forum_0_0.csv \
--nodes ${dataset_dir}/csv/organisation_0_0.csv \
--nodes ${dataset_dir}/csv/person_0_0.csv \
--nodes ${dataset_dir}/csv/place_0_0.csv \
--nodes ${dataset_dir}/csv/post_0_0.csv \
--nodes ${dataset_dir}/csv/tag_0_0.csv \
--nodes ${dataset_dir}/csv/tagclass_0_0.csv \
--relationships ${dataset_dir}/csv/comment_hasCreator_person_0_0.csv \
--relationships ${dataset_dir}/csv/comment_hasTag_tag_0_0.csv \
--relationships ${dataset_dir}/csv/comment_isLocatedIn_place_0_0.csv \
--relationships ${dataset_dir}/csv/comment_replyOf_comment_0_0.csv \
--relationships ${dataset_dir}/csv/comment_replyOf_post_0_0.csv \
--relationships ${dataset_dir}/csv/forum_containerOf_post_0_0.csv \
--relationships ${dataset_dir}/csv/forum_hasMember_person_0_0.csv \
--relationships ${dataset_dir}/csv/forum_hasModerator_person_0_0.csv \
--relationships ${dataset_dir}/csv/forum_hasTag_tag_0_0.csv \
--relationships ${dataset_dir}/csv/organisation_isLocatedIn_place_0_0.csv \
--relationships ${dataset_dir}/csv/person_hasInterest_tag_0_0.csv \
--relationships ${dataset_dir}/csv/person_isLocatedIn_place_0_0.csv \
--relationships ${dataset_dir}/csv/person_knows_person_0_0.csv \
--relationships ${dataset_dir}/csv/person_likes_comment_0_0.csv \
--relationships ${dataset_dir}/csv/person_likes_post_0_0.csv \
--relationships ${dataset_dir}/csv/person_studyAt_organisation_0_0.csv \
--relationships ${dataset_dir}/csv/person_workAt_organisation_0_0.csv \
--relationships ${dataset_dir}/csv/place_isPartOf_place_0_0.csv \
--relationships ${dataset_dir}/csv/post_hasCreator_person_0_0.csv \
--relationships ${dataset_dir}/csv/post_hasTag_tag_0_0.csv \
--relationships ${dataset_dir}/csv/post_isLocatedIn_place_0_0.csv \
--relationships ${dataset_dir}/csv/tag_hasType_tagclass_0_0.csv \
--relationships ${dataset_dir}/csv/tagclass_isSubclassOf_tagclass_0_0.csv"

# Convert to neo4j internal format.
neo4j_database_dir=${dataset_dir}/neo4j/databases
mkdir -p ${neo4j_database_dir}
cd ${neo4j_database_dir}
echo "Converting CSV dataset to '${neo4j_database_dir}/graph.db'"
${neo4j_dir}/bin/neo4j-import --into graph.db ${csv_dataset} --delimiter "|" --array-delimiter ";"

# Convert to memgraph internal format.
memgraph_snapshot_dir=${dataset_dir}/memgraph/snapshots
mkdir -p ${memgraph_snapshot_dir}
cd ${memgraph_snapshot_dir}
echo "Converting CSV dataset to '${memgraph_snapshot_dir}/snapshot'"
${build_dir}/tools/src/mg_import_csv --out snapshot ${csv_dataset} --csv-delimiter "|" --array-delimiter ";"

echo "Done!"
