#!/bin/bash
set -e

# MongoDB host and port
MONGO_HOST="localhost"
MONGO_PORT="27017"

# Database name
DB_NAME="voxadata"

# Wait for MongoDB to boot
echo "Waiting for MongoDB to start..."
until mongosh --host $MONGO_HOST --port $MONGO_PORT --eval "print(\"waited for connection\")"
do
    sleep 1
done
echo "MongoDB started"

echo "Creating $DB_NAME database and importing BSON files..."

# Loop through all BSON files in the directory and import them
for bson_file in /docker-entrypoint-initdb.d/*.bson; do
    echo "Importing $bson_file into $DB_NAME..."
    mongorestore --host $MONGO_HOST --port $MONGO_PORT --db $DB_NAME --drop --verbose "$bson_file"
done

echo "Database $DB_NAME setup completed."
