#!/bin/bash
set -e

chmod -R 777 /neo4j_import #make the dir accessible for both the python app an neo4j


#echo "Waiting for Neo4j to be ready... ..."
#python wait-for-neo4j.py

#if [ $? -ne 0 ]; then
#    echo "Failed to connect to Neo4j"
#    exit 1
#fi

echo "Running Python data processing script..."
poetry run python import_fhir_to_nx_diGraph.py

echo "Running Neo4j import..."
# Wait a bit before attempting database operations
sleep 5

while [ ! -f /neo4j_import/shell-scipt-complete ]; do
    echo "Waiting for shell-script file"
    sleep 5
done

# Create a signal file that we've prepared the data
touch /neo4j_import/ready-to-import
chmod -R 777 /neo4j_import/ready-to-import

# Wait for import to complete by monitoring a completion file
echo "Waiting for Neo4j import to complete..."
while [ ! -f /neo4j_import/import-complete ]; do
    echo "Waiting for import-complete file"
    sleep 5
done

echo "Database setup complete!"